• Stars
    star
    170
  • Rank 223,357 (Top 5 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿšฆ- Small Finite State Machines

nanostate

npm version build status downloads js-standard-style

Small Finite State Machines. Great data structure to make code more readable, maintainable and easier to debug.

Usage

var nanostate = require('nanostate')

var machine = nanostate('green', {
  green: { timer: 'yellow' },
  yellow: { timer: 'red' },
  red: { timer: 'green' }
})

machine.emit('timer')
console.log(machine.state)
// => 'yellow'

machine.emit('timer')
console.log(machine.state)
// => 'red'

machine.emit('timer')
console.log(machine.state)
// => 'green'

Hierarchical

Let's implement a traffic light that flashes red whenever there's a power outage. Instead of adding a powerOutage event to each normal state, we introduce a hierarchy which allows any normal state to emit the powerOutage event to change the state to flashingRed.

var nanostate = require('nanostate')

var machine = nanostate('green', {
  green: { timer: 'yellow' },
  yellow: { timer: 'red' },
  red: { timer: 'green' }
})

machine.event('powerOutage', nanostate('flashingRed', {
  flashingRed: { powerRestored: 'green' }
}))

machine.emit('timer')
console.log(machine.state)
// => 'yellow'

machine.emit('powerOutage')
console.log(machine.state)
// => 'flashingRed'

machine.emit('powerRestored')
console.log(machine.state)
// => 'green'

History (to be implemented)

Implementers note: keep track of the last state a machine was in before exiting to the next machine. That way if '$history' is called, it can be merged into the previous machine.

TODO: figure out how it works if machines are combined in a non-linear fashion.

var nanostate = require('nanostate')

var machine = nanostate('cash', {
  cash: { check: 'check' },
  check: { cash: 'cash' }
})

machine.join('next', nanostate('review', {
  review: { previous: '$history' }
}))

Parallel

Sometimes there's multiple parallel states that need expressing; nanostate.parallel helps with that. For example when editing text, a particular piece of text might be bold, italic and underlined at the same time. The trick is that all of these states operate in parallel

var nanostate = require('nanostate')

var machine = nanostate.parallel({
  bold: nanostate('off', {
    on: { 'toggle': 'off' },
    off: { 'toggle': 'on' },
  }),
  underline: nanostate('off', {
    on: { 'toggle': 'off' },
    off: { 'toggle': 'on' },
  }),
  italics: nanostate('off', {
    on: { 'toggle': 'off' },
    off: { 'toggle': 'on' },
  }),
  list: nanostate('none', {
    none: { bullets: 'bullets', numbers: 'numbers' },
    bullets: { none: 'none', numbers: 'numbers' },
    numbers: { bullets: 'bullets', none: 'none' }
  })
})

machine.emit('bold:toggle')
console.log(machine.state)
// => {
//   bold: 'on',
//   italics: 'off',
//   underline: 'off',
//   list: 'none'
// }

Nanocomponent

Usage in combination with nanocomponent to create stateful UI components.

var Nanocomponent = require('nanocomponent')
var nanostate = require('nanostate')

module.exports = class Component extends Nanocomponent {
  constructor (name, state, emit) {
    super(name, state, emit)

    this.state = {
      data: {},
      input: ''
    }

    this.machine = nanostate('idle', {
      idle: { click: 'loading' },
      loading: { resolve: 'data', reject: 'error' },
      data: { click: 'loading' },
      error: { click: 'loading' }
    })

    this.machine.on('loading', () => this.searchRepositories())
  }

  createElement () {
    var buttonText = {
      idle: 'Fetch Github',
      loading: 'Loadingโ€ฆ',
      error: 'Github fail. Retry?',
      data: 'Fetch Again?'
    }[this.machine.state]

    return html`
      <div>
        <input
          type="text"
          value=${this.state.input}
          onChange=${e => (this.state.input = e.target.value) && this.rerender()}
        >
        <button
          onClick=${() => this.machine.emit('click')}
          disabled=${this.machine.state === 'loading'}
        >
          ${buttonText}
        </button>
        ${data && html`<div>${JSON.stringify(data, null, 2)}</div>`}
        ${this.machine.state === 'error' && html`<h1>Error</h1>`}
      </div>
    `
  }

  searchRepositories () {
    fetch(`${ROOT_URL}/${this.state.input}`)
      .then(res => res.json())
      .then(res => {
         this.state.data = res.data
         this.machine.emit('resolve')
       })
      .catch(err => this.machine.emit('reject'))
  }
}

API

machine = nanostate(initialState, transitions)

Create a new instance of Nanostate. Takes the name of the initial state, and a mapping of states and their corresponding transitions. A state mapping is defined as { 1: { 2: 3 }}, where 1 is the state's name, 2 is an event name it accepts, and 3 is the new state after the event has been emitted.

machine.emit(event)

Move from the current state to a new state. Will throw if an invalid command is passed.

machine.on(state, cb)

Trigger a callback when a certain state is entered. Useful to trigger side effects upon state change.

state = machine.state

Return the current state.

machine.event(eventName, machine) (to be implemented)

Add another machine to the transition graph. The first argument is an event name, which can be transitioned to from all other states in the graph.

machine = nanostate.parallel(machines) (to be implemented)

Combine several state machines into a single machine. Takes an object of state machines, where the key is used to prefix the events for the state machine.

Say we have two state machine: 'foo' and 'bar'. 'foo' has an event called 'beep'. When combined through .parallel(), the event on the combined machine would be 'foo:beep'.

Installation

$ npm install nanostate

See Also

License

Apache-2.0

More Repositories

1

choo

๐Ÿš‚๐Ÿš‹ - sturdy 4kb frontend framework
JavaScript
6,776
star
2

bankai

๐Ÿš‰ - friendly web compiler
JavaScript
1,088
star
3

hyperx

๐Ÿท - tagged template string virtual dom builder
JavaScript
1,010
star
4

nanomorph

๐Ÿš… - Hyper fast diffing algorithm for real DOM nodes
JavaScript
726
star
5

nanohtml

๐Ÿ‰ HTML template strings for the Browser with support for Server Side Rendering in Node.
JavaScript
687
star
6

nanographql

Tiny graphQL client library
JavaScript
421
star
7

nanocomponent

๐Ÿšƒ - create performant HTML components
JavaScript
366
star
8

wayfarer

๐Ÿ‘“ composable trie based router
JavaScript
332
star
9

choo-handbook

๐Ÿš‚โœ‹๐Ÿ“– - Learn the choo framework through a set of exercises
HTML
268
star
10

nanobus

๐ŸšŽ - Tiny message bus
JavaScript
225
star
11

awesome-choo

๐ŸŒ… Awesome things related with choo framework
197
star
12

create-choo-app

๐Ÿšž - create a fresh choo application
JavaScript
181
star
13

nanorouter

๐Ÿ›ค - Small frontend router
JavaScript
116
star
14

nanocomponent-adapters

๐Ÿ”Œ - Convert a nanocomponent to a component for your favourite API or library (web components, (p)react, angular)
JavaScript
96
star
15

choop

๐Ÿš‚โš›๏ธ - choo architecture for preact
JavaScript
93
star
16

on-idle

๐Ÿ˜ด - Detect when the browser is idle
JavaScript
82
star
17

nanologger

๐Ÿ“œ - Cute browser logs
JavaScript
80
star
18

nanoanimation

๐Ÿ‘จโ€๐ŸŽจ - Safety wrapper around the Web Animation API
JavaScript
72
star
19

nanoraf

๐ŸŽž - Only call RAF when needed
JavaScript
71
star
20

choo-devtools

๐Ÿ’ผ - Expose a choo instance on the window
JavaScript
53
star
21

nanoquery

๐Ÿ“‡ - Tiny querystring module
JavaScript
49
star
22

nanotask

Microtask queue scheduler for the browser
JavaScript
47
star
23

choo-log

๐Ÿ“ƒ - Development logger for choo
JavaScript
47
star
24

nanoscheduler

Schedule work to be completed when the user agent is idle.
JavaScript
46
star
25

website

๐Ÿš‡ - Hyper Train Transfer Protocol (HTTP)
JavaScript
46
star
26

nanohref

โ›“ - Tiny href click handler library
JavaScript
41
star
27

nanotick

process.nextTick() batching utility
JavaScript
37
star
28

choo-store

Lightweight state structure for choo apps.
JavaScript
37
star
29

nanotiming

โฒ - Small timing library
JavaScript
35
star
30

create-choo-electron

:electron: - Create a fresh Choo Electron application
JavaScript
29
star
31

object-change-callsite

Determine the callsite of an object change using Proxies
JavaScript
27
star
32

choo-reload

โ›ฝ๏ธ - Livereloading package for choo
JavaScript
27
star
33

on-performance

Listen for performance timeline events
JavaScript
26
star
34

nanobeacon

Small navigator.sendBeacon wrapper
JavaScript
25
star
35

choo-service-worker

๐Ÿ‘ท - Service worker loader for choo
JavaScript
24
star
36

choo-scaffold

๐Ÿ— - Scaffold out files for a Choo project
JavaScript
24
star
37

choo-notification

Web Notification plugin for Choo
JavaScript
22
star
38

nanobounce

Smol debounce package
JavaScript
19
star
39

choo-choo

๐ŸŽ“ learn choo from the command line!
JavaScript
19
star
40

nanomount

Mount a DOM tree on a target node
JavaScript
19
star
41

choo-redirect

๐ŸŽฌ - Redirect a view to another view
JavaScript
19
star
42

persist-storage

๐Ÿ—„ - Enable persistent storage in the browser
JavaScript
19
star
43

nanohistory

Small browser history library
JavaScript
14
star
44

choo-hooks

๐ŸŽฃ - Hook into Choo's events and timings
JavaScript
12
star
45

nanolocation

๐Ÿ“- Small window.location library
JavaScript
10
star
46

discuss

๐ŸŽญ โ€“ Discuss project organization, initiatives, and anything else!
8
star
47

nanocache

Cache Nanocomponents.
JavaScript
7
star
48

bankai-website

JavaScript
6
star
49

choo-umd

๐Ÿ™ˆ - umd build for choo framework
HTML
3
star