Launch Week Day 1: Socket for Jira Is Now Available.Learn More
Socket
Book a DemoSign in
Socket

micro-machine

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

micro-machine

Minimal state machine implementation.

latest
Source
npmnpm
Version
0.6.0
Version published
Maintainers
1
Created
Source

Micro Machine

Build Status

Minimal state machine implementation.

Heavily inspired by soveran/micromachine.

Installation

npm install micro-machine

Usage

var Machine = require('micro-machine')
  , machine = new Machine('pending')

machine.transitionsFor.confirm = { pending: 'confirmed' }
machine.transitionsFor.reset = { confirmed: 'pending' }

machine.trigger('confirm')
console.log(machine.state) // 'confirmed'
machine.trigger('reset')
console.log(machine.state) // 'pending'

Callbacks

You can also define callbacks that will be invoked after the specified transition.

var Machine = require('micro-machine')
  , machine = new Machine('pending')
  
machine.transitionsFor.confirm = { pending: 'confirmed' }
machine.transitionsFor.reset = { confirmed: 'pending' }

var state

/* Use 'any' to define callback for any transition. */
machine.on('any',  function(machine){
  state = machine.state
})

machine.on('reset', function() { console.log('resetting...') })

machine.trigger('confirm')
console.log(state)  // 'confirmed'

machine.trigger('reset') // 'resetting...'

Development

Run tests with

npm test

or build it with

npm run build

Unlicense

This repository and its contents belong to the public domain.

It has been released under the UNLICENSE.

Keywords

state

FAQs

Package last updated on 22 Sep 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts