Giter VIP home page Giter VIP logo

flyd-xstate's Introduction

flyd-xstate

This unifies functional reactive programming (FRP) with flyd and Harel Statecharts with xstate

machine(config)

The api to create a state machine is the same as xstate, with one extra key: eventStream, which should be a stream of event names that occur over time for the statechart.

The object returned contains two state stream properties:

  • .state: a stream of states (represented as strings or objects) -- produced by State.value from xstate
  • .stateString: a stream of states (represented as strings only) -- produced by State.toString() from xstate

Please see the test file to see more examples -- you will find the API is quite simple: push new events to your event stream and listen to new states on the state stream.

const machine = require('flyd-xstate')
const flyd = require('flyd')

const event = flyd.stream()
const lights = machine({
  eventStream: event,
  key: 'light',
  initial: 'green',
  states: {
    green: { on: { TIMER: 'yellow' } },
    yellow: { on: { TIMER: 'red' } },
    red: { on: { TIMER: 'green' } }
  }
})

t.strictEqual(lights.state(), 'green')
t.strictEqual(lights.stateString(), 'green')
event('TIMER')
t.strictEqual(lights.state(), 'yellow')
t.strictEqual(lights.stateString(), 'yellow')

Test utility

There is a simple test utility here that you can import from lib/test. It allows you to list events and test the expected states.

const testStates = require('flyd-xstate/lib/test')
const test = require('tape')
const PageChart = require('page-chart')

test('all valid paths', t => {
  const chart = PageChart()
  testStates(chart, [
    ['IMPORT', 'importing'],
    ['CLOSE', 'listing'],
    ['IMPORT', 'importing'],
    ['SAVE', 'loading'],
    ['DONE', 'listing'],
    ['REMOVE', 'confirmingRemove'],
    ['DENY', 'listing'],
    ['REMOVE', 'confirmingRemove'],
    ['CONFIRM', 'loading'],
    ['DONE', 'listing'],
    ['EDIT', 'editing.viewing'],
    ['CLOSE', 'listing'],
    ['EDIT', 'editing.viewing'],
    ['UPDATE', 'editing.loading'],
    ['DONE', 'editing.viewing'],
    ['ADD', 'editing.adding'],
    ['SAVE', 'editing.loading'],
    ['DONE', 'editing.viewing'],
    ['ADD', 'editing.adding'],
    ['CLOSE', 'editing.viewing'],
    ['CLOSE', 'listing']
  ], t)
  t.end()
})

Each pair of [EVENT_NAME, state] represents the state that the chart should be in after you fire the given event. Everything gets evaluated top-down in a big sequence.

flyd-xstate's People

Contributors

jayrbolton avatar

Stargazers

Alex Roldugin avatar Andrejs Agejevs avatar Young avatar  avatar David Khourshid avatar nichoth avatar Yutaka Houlette avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.