0.5.1 • Published 5 years ago

bottender-xstate v0.5.1

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

bottender-xstate

CircleCI npm License: MIT

Finite State Machines in Bottender using xstate

Installation

npm install bottender-xstate

API Reference

ParamTypeDescription
configXstateConfigConfig to be passed to xstate.
mapContextToXstateEventFunctionMapper for create xstate event from context.
actionsObjectMap of named actions.
guardsObjectMap of named guards.
eventsArray<String>All events handled by the machine. (required when using * (catch-all) event)
onEventFunctionCallback to be called when trigger event.
onActionFunctionCallback 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

0.5.1

5 years ago

0.5.0

6 years ago

0.4.1

6 years ago

0.4.1-events

6 years ago

0.4.0

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago