bottender-xstate

0.5.1 • Public • Published

bottender-xstate

CircleCI npm License: MIT

Finite State Machines in Bottender using xstate

Installation

npm install bottender-xstate

API Reference

Param Type Description
config XstateConfig Config to be passed to xstate.
mapContextToXstateEvent Function Mapper for create xstate event from context.
actions Object Map of named actions.
guards Object Map of named guards.
events Array<String> All events handled by the machine. (required when using * (catch-all) event)
onEvent Function Callback to be called when trigger event.
onAction Function Callback to be called when trigger action.
const bottenderXstate = require('bottender-xstate');
 
const config = {
  id: 'light',
  initial: 'green',
  states: {
    green: {
      on: {
        TIMER: { target: 'yellow' },
      },
      onEntry: 'enterGreen',
      onExit: 'leaveGreen',
    },
    yellow: {
      on: {
        TIMER: { target: 'red' },
      },
      onEntry: 'enterYellow',
      onExit: 'leaveYellow',
    },
    red: {
      on: {
        TIMER: {
          target: 'green',
          actions: ['fromRedToGreen'],
        },
      },
      onEntry: 'enterRed',
      onExit: 'leaveRed',
    },
  },
};
 
const mapContextToXstateEvent = () => 'TIMER';
 
const actions = {
  enterGreen: context => context.sendText('enter green'),
  enterYellow: context => context.sendText('enter yellow'),
  enterRed: context => context.sendText('enter red'),
  leaveGreen: context => context.sendText('leave green'),
  leaveYellow: context => context.sendText('leave yellow'),
  leaveRed: context => context.sendText('leave red'),
  fromRedToGreen: context => context.sendText('from red to green'),
};
 
bot.onEvent(
  bottenderXstate({
    config,
    mapContextToXstateEvent,
    actions,
  })
);

You can find more examples in the examples folder.

License

MIT © Yoctol

Dependents (0)

Package Sidebar

Install

npm i bottender-xstate

Weekly Downloads

6

Version

0.5.1

License

MIT

Unpacked Size

20.8 kB

Total Files

10

Last publish

Collaborators

  • chentsulin
  • etrexkuo
  • tw0517tw
  • link515
  • calvinlin