How to use the xstate/lib/utils.flatten function in xstate

To help you get started, we’ve selected a few xstate examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github davidkpiano / xstate / packages / xstate-graph / src / graph.ts View on Github external
function findAdjacencies(state: State) {
    const { nextEvents } = state;
    const stateHash = stateSerializer(state);

    if (adjacency[stateHash]) {
      return;
    }

    adjacency[stateHash] = {};

    const potentialEvents = flatten(
      nextEvents.map(nextEvent => events[nextEvent] || [])
    ).map(event => toEventObject(event));

    for (const event of potentialEvents) {
      let nextState: State;
      try {
        nextState = node.transition(state, event);
      } catch (e) {
        throw new Error(
          `Unable to transition from state ${stateSerializer(
            state
          )} on event ${eventSerializer(event)}: ${e.message}`
        );
      }

      if (
github davidkpiano / xstate / packages / xstate-scxml / src / index.ts View on Github external
return stateNodeToSCXML(childStateNode);
  });

  const elements: XMLElement[] = [];

  const { onEntry, onExit } = stateNode;

  if (onEntry.length) {
    elements.push(actionsToSCXML('onentry', onEntry));
  }

  if (onExit.length) {
    elements.push(actionsToSCXML('onexit', onExit));
  }

  const transitionElements = flatten(
    Object.keys(stateNode.on).map(event => {
      const transitions = stateNode.on[event];

      return transitions.map(transition => transitionToSCXML(transition));
    })
  );

  elements.push(...transitionElements);
  elements.push(...childStates);

  if (stateNode.type === 'final' && stateNode.data) {
    elements.push(doneDataToSCXML(stateNode.data));
  }

  return {
    type: 'element',