Git Product home page Git Product logo

asyncmachine's Introduction

AsyncMachine is a relational state machine made for declarative flow control. It supports multiple states simultaneously, executes methods based on a dependency graph and provides an event emitter.

It allows for easy:

  • state management
  • parallel tasks
  • loose coupling
  • resource allocation / disposal
  • exception handling
  • fault tolerance
  • method cancellation

It supports forming a network of state machines and can also be used as a simple state management library. Gzipped code is 7.5kb.

Install

npm i asyncmachine

Documentation

Components:

  • states
  • transitions
  • relations
  • clocks
  • pipes
  • queues

Features:

Examples

Dry Wet

This basic examples makes use of: states, transitions, relations and synchronous mutations.

import { machine } from 'asyncmachine'
// define
const state = {
  // state Wet when activated, requires state Water to be active
  Wet: {
    require: ['Water']
  },
  // state Dry when activated, will drop (de-activate) state Wet
  Dry: {
    drop: ['Wet']
  },
  // state Water when activated, will add (activate) state Wet and
  // drop (de-activate) state Dry
  Water: {
    add: ['Wet'],
    drop: ['Dry']
  }
}
// initialize
const example = machine(state)
// initially the machine has no active states
example.is() // -> []
// activate state Dry
example.add('Dry')
example.is() // -> [ 'Dry' ]
// activate state Water, which will resolve the relations:
// 1. Water activates Wet
// 2. Wet requires Water
// 3. Dry de-activates Wet
// 4. Water de-activates Dry
// 5. Water activates Wet
example.add('Water')
example.is() // -> [ 'Wet', 'Water' ]

example

Negotiation

Presents how the state negotiation works.

Async Dialog

Presents the following concepts: automatic states, synchronous mutations, delayed mutations and loose coupling.

Exception State

A simple fault tolerance (retrying) using the Exception state.

Piping

Shows how pipes forward states between machines.

Transitions

Shows various types of transition handlers and the way params get passed to them.

TodoMVC and React

Classic TodoMCV example using AsyncMachine as the controller and React as the view.

State streams with RxJS

Observe state changes and navigate through specific paths with RxJS, then feed the result back as a state.

  • Comming soon!

Restaurant

A complex example showing how to solve the producer / consumer problem using AsyncMachine.

inspector view

TaskBot

For a real world example check TaskBot - a real-time sync engine for Google APIs.

Preview

Use cases

  • state management
  • synchronizing async actions
  • solving non-linear problems
  • fault tolerance
  • resource allocation / disposal
  • avoiding race conditions
  • thread pools
  • sync engines

License

MIT

asyncmachine's People

Contributors

tobiaszcudnik avatar

Watchers

 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.