Parallel States

In a heirarchy of states, parallel states are parents. They contain atomic , compound or other parallel states.

Usage

Use parallel(...) to create state configuration and resolveState(...) to initialize it.

import { atomic, parallel, emitEvent, resolveState } from 'hine';
let health = 10;
let position = 10;
const playerConfig = parallel('player', {
children: [
atomic('health', {
on: { advance: () => health++ },
}),
atomic('position', {
on: { advance: () => position++ },
}),
],
});
const playerState = resolveState(playerConfig);
emitEvent(playerState, 'advance');

In this example, playerState is set to increment both health and position by 1 each time an 'advance' event is emitted.

Active Children

Active states are those that can receive and handle emitted events at a particular time. All children in a parallel state are active.

const activeChildCount = playerState.activeChildren.length;
const childCount = playerState.children.size;
// Always `true` for parallel states
console.log(activeChildCount === childCount);

Hooks

Parallel states accept hooks such as afterEntry and beforeExit . Hooks respond to life-cycle events of a state.

import { atomic, parallel, emitEvent, resolveState } from 'hine';
let health;
let position;
const initializeHealth = () => (health = 10);
const initializePosition = () => (position = 10);
const playerConfig = parallel('player', {
children: [
atomic('health', {
hooks: {
afterEntry: initializeHealth,
},
on: { advance: () => health++ },
}),
atomic('position', {
hooks: {
afterEntry: initializePosition,
},
on: { advance: () => position++ },
}),
],
});
const playerState = resolveState(playerConfig);
emitEvent(playerState, 'advance');

When playerState is resolved, it will transition to the 'health' and 'position' states simulatenously and initialize both health and position to 10 .