How to use the xstate.Machine 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 isaacplmann / sturdy-uis / src / machines / fetch.ts View on Github external
rejected: {};
  };
}

// The events that the machine handles
type ResolveEvent = { type: 'RESOLVE'; results: any[] };
type RejectEvent = { type: 'REJECT'; message: string };
type FetchEvents = { type: 'FETCH' } | ResolveEvent | RejectEvent;

// The context (extended state) of the machine
interface FetchContext {
  results?: any[];
  message?: string;
}

export const fetchMachine = Machine(
  {
    id: 'fetch',
    initial: 'idle',
    context: {},
    states: {
      idle: {
        on: { FETCH: 'pending' }
      },
      pending: {
        invoke: {
          src: 'fetchData',
          onDone: { target: 'fulfilled', actions: 'setResults' },
          onError: { target: 'rejected', actions: 'setMessage' }
        }
      },
      fulfilled: {
github chungchiehlun / redux-statechart / src / __tests__ / index.js View on Github external
pending: {
      on: {
        RESOLVE: "resolved",
        REJECT: "rejected"
      }
    },
    resolved: {
      type: "final"
    },
    rejected: {
      type: "final"
    }
  }
});

const starWarsMachine = Machine({
  id: "starWars",
  initial: "idle",
  states: {
    idle: {
      on: {
        REQUEST: {
          target: "pending",
          actions: ["alertStartingFirstRequest"]
        }
      },
      onExit: "alertMayTheForceBeWithYou"
    },
    pending: {
      on: {
        SUCCESS: "fulfilled",
        FAILURE: "rejected"
github alfreema / vue-state-machine / dist / vue-state-machine.cjs.js View on Github external
var addMachine = function addMachine(storeName, machine) {
    console.log('vsm-plugin:addMachine():storeName=' + storeName);
    return store.dispatch({
      type: moduleName + '/addMachine',
      machineName: storeName,
      machine: xstate.Machine(machine)
    });
  };
github kyleshevlin / intro-to-state-machines-and-xstate-course / lessons / how-to-invoke-callbacks / before.js View on Github external
const { Machine } = require('xstate')

const echoMachine = Machine({
  id: 'echo',
  initial: 'listening',
  states: {
    listening: {
      on: {
        SPEAK: {},
        ECHO: {
          actions: () => {
            console.log('echo, echo')
          },
        },
      },
    },
  },
})
github kyleshevlin / intro-to-state-machines-and-xstate-course / lessons / replacing-enumerated-states-with-a-state-machine / after.js View on Github external
import { Machine } from 'xstate'

const lightBulbMachine = Machine({
  id: 'lightBulb',
  initial: 'unlit',
  states: {
    lit: {
      on: {
        BREAK: 'broken',
        TOGGLE: 'unlit'
      }
    },
    unlit: {
      on: {
        BREAK: 'broken',
        TOGGLE: 'lit'
      }
    },
    broken: {}
github coodoo / xstate-examples / crud-v2-optimistic-update / src / fsm / machine.js View on Github external
import { Machine } from 'xstate'
import * as actions from './actions'
import * as services from './services'
import * as guards from './guards'
import { fsm } from './fsm'

export const machine = Machine(
	fsm,
	{
		actions,
		services,
		guards,
	},
)
github wonderunit / storyboarder / src / js / xr / src / machines / uiMachine.js View on Github external
const { Machine, assign } = require('xstate')

const { log } = require('../components/Log')

const machine = Machine({
  id: 'ui',
  strict: true,
  type: 'parallel',
  context: {
    selection: null,
    locked: false
  },
  states: {
    controls: {
      initial: 'home',
      states: {
        home: {
          on: {
            'GO_ADD': 'add',
            'TOGGLE_SETTINGS': 'settings',
            'GO_PROPERTIES': 'properties'
github stefanoslig / angular-xstate / src / app / auth / +xstate / auth-machine.service.ts View on Github external
assignUser: assign((_, event) => ({
        user: event.userInfo
      })),
      assignErrors: assign((_, event) => ({
        errors: Object.keys(event.errors || {}).map(
          key => `${key} ${event.errors[key]}`
        )
      })),
      loginSuccess: (ctx, _) => {
        localStorage.setItem('jwtToken', ctx.user.token);
        this.router.navigateByUrl('');
      }
    }
  };

  private _authMachine = Machine(
    authMachineConfig
  ).withConfig(this.authMachineOptions);
  private service = interpret(this._authMachine, { devTools: true }).start();

  authState$ = fromEventPattern<[State, EventObject]>(
    handler => {
      return this.service.onTransition(handler);
    },
    (_, service) => service.stop()
  ).pipe(map(([state, _]) => state));

  send(event: AuthEvent) {
    this.service.send(event);
  }

  constructor(private authService: AuthService, private router: Router) {}
github kyleshevlin / intro-to-state-machines-and-xstate-course / lessons / how-to-use-context / after.js View on Github external
const { assign, interpret, Machine } = require('xstate')

const multiColoredBulbMachine = Machine(
  {
    id: 'multiColoredBulb',
    initial: 'unlit',
    context: {
      color: '#fff',
    },
    states: {
      lit: {
        on: {
          BREAK: 'broken',
          TOGGLE: 'unlit',
          CHANGE_COLOR: {
            actions: ['changeColor'],
          },
        },
      },