How to use the xstate/lib/interpreter.interpret 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 LevelbossMike / ember-statecharts / tests / dummy / app / components / viz / statechart-viz / component.js View on Github external
interpretMachine(machine) {
    // parse machine actions // get actions from them and stub them out
    const actionNames = Object.keys(machine.options.actions || {});

    const stubbedActions = actionNames.reduce((acc, name) => { 
      acc[name] = this.onActionTriggered.bind(this, name);
      return acc;
    }, {})

    const interpreter = interpret(this.machine.withConfig({
      actions: stubbedActions
    })).onTransition(nextState => {
      this.set('currentState', nextState);
    })

    return interpreter.start();
  },
github davidkpiano / xstate / examples / src / App.tsx View on Github external
() =>
      interpret(machine)
        .onTransition(state => {
          console.log('STATE:', state);
          // @ts-ignore
          setCurrent(state);
        })
        .onEvent(e => console.log('EVENT:', e))
        .start(),
    [machine]
github carloslfu / use-machine / src / index.ts View on Github external
function getService() {
    const service = serviceRef.current;
    if (service !== null) {
      return service;
    }
    const newService = interpret(machine);
    // workaround https://github.com/DefinitelyTyped/DefinitelyTyped/issues/31065
    ; (serviceRef.current as any) = newService;
    newService.onTransition(state => setStateSchema(state));
    newService.onChange(context => setContext(context));
    // call init after the above callbacks are set so any immediate actions
    // are reflected in react's state
    newService.init();
    return newService;
  }
github davidkpiano / xviz / src / StateChart.tsx View on Github external
state: StateChartState = (() => {
    const machine = toMachine(this.props.machine);
    return {
      current: machine.initialState,
      preview: undefined,
      previewEvent: undefined,
      view: "definition", // or 'state'
      machine: machine,
      code:
        typeof this.props.machine === "string"
          ? this.props.machine
          : `Machine(${JSON.stringify(machine.definition, null, 2)})`,
      toggledStates: {}
    };
  })();
  service = interpret(this.state.machine).onTransition(current => {
    this.setState({ current }, () => {
      if (this.state.previewEvent) {
        this.setState({
          preview: this.service.nextState(this.state.previewEvent)
        });
      }
    });
  });
  svgRef = React.createRef();
  componentDidMount() {
    this.service.start();
  }
  renderView() {
    const { view, current, machine, code } = this.state;

    switch (view) {
github davidkpiano / xstate / docs / src / components / stateChart.jsx View on Github external
}

const StyledStateChart = styled.div`
  display: inline-grid;
  grid-template-columns: 50% 50%;
  grid-template-rows: auto;
  font-family: sans-serif;
  font-size: 12px;
`;

export class StateChart extends React.Component {
  state = {
    current: this.props.machine.initialState,
    preview: undefined
  };
  interpreter = interpret(this.props.machine).onTransition(current => {
    this.setState({ current });
  });
  componentDidMount() {
    console.log(this.interpreter);
    this.interpreter.start();
  }
  render() {
    const { machine } = this.props;
    const { current, preview } = this.state;

    const stateNodes = machine.getStateNodes(current);
    const events = new Set();

    stateNodes.forEach(stateNode => {
      const potentialEvents = Object.keys(stateNode.on);
github bradwoods / react-xstate-js / src / Machine / index.tsx View on Github external
import * as React from 'react';
import {
  Machine as XstateMachine, State
} from 'xstate';
import { 
  interpret
} from 'xstate/lib/interpreter';
import {
  IState, IProps,
} from '../types'

class Machine extends React.Component {

  private machine = XstateMachine(this.props.config, this.props.options || {}, this.props.savedContext)

  service = interpret(this.machine)

  readonly state: IState = {
    machineStateNode: this.machine.initialState,
  }

  componentDidMount() {
    const { savedState } = this.props

    this.service = interpret(this.machine)
      .onTransition(nextState => {
        this.setState({ machineStateNode: nextState });
      });

    if (savedState) {
      const restoredState = State.create(savedState)
      this.service.start(restoredState)
github bradwoods / react-xstate-js / src / Machine / index.tsx View on Github external
componentDidMount() {
    const { savedState } = this.props

    this.service = interpret(this.machine)
      .onTransition(nextState => {
        this.setState({ machineStateNode: nextState });
      });

    if (savedState) {
      const restoredState = State.create(savedState)
      this.service.start(restoredState)
    }
    else this.service.start();
  }