npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

estado

v0.7.1

Published

Simple JavaScript Finite State Machines.

Downloads

53

Readme

Estado

Simple, stateless JavaScript finite-state machines.

What is it? Estado is a tiny, framework-agnostic JS library for representing finite-state machines and hierarchical state machines, or Harel statecharts. Its main use is as a pure (extended) transition function of the form (state, action) -> state.

Why? (Article coming soon!) TL;DR: Finite state machines are extremely useful for representing the various states your application can be in, and how each state transitions to another state when an action is performed. Also, declaring your state machine as data (Estado language parses to pure JSON, and is SCXML-compatible) means you can use your state machine, well, anywhere. Any language.

(Example coming soon!)

Getting Started

  1. Install via NPM: npm install estado --save
  2. Import the state machine creator into your project:
import { machine } from 'estado';

let lightMachine = machine`
  green -> yellow (TIMER)
  yellow -> red (TIMER)
  red -> green (TIMER)
`;

// Pure, stateless transition functions
let currentState = lightMachine.transition('green', 'TIMER');
// => 'yellow'

let newState = lightMachine.transition(currentState, 'TIMER');
// => 'red'

// Initial state
let initialState = lightMachine.transition();
// => 'green'

The Estado language

Estado allows you to parse an easy-to-learn DSL for declaratively writing finite state machines.

####States

A state is just an alphanumeric string (underscores are allowed) without quotes or spaces: some_valid_state. Final states are appended with an exclamation point: someFinalState!.

By default, the first state declared in a state group is an initial state.

####Transitions

A transition (edge) between states is denoted with an arrow: ->. A state can transition to itself with a reverse arrow: <-. In the example below, state1 transitions to state2 on the FOO event. state2 transitions to itself on the BAR event.

state1 -> state2 (FOO)
state2 <- (BAR)

####Actions

An action is also an alphanumeric string (underscores allowed), just like states. They are contained in parentheses after a transition: state1 -> state2 (SOME_EVENT), or after a self-transition: state3 <- (AN_EVENT). Actions are optional (but encouraged for proper state machine design).

####Nested States

States can be hierarchical (nested) by including them inside brackets after a state declaration. They can be deeply nested an infinite amount of levels. This is useful for implementing statecharts.

state1 {
  nestedState1 -> nestedState2 (FOO)
  nestedState2!
} -> state2 (BAR)
  -> state3 (BAZ)
state2!
state3!

Here's a more pragmatic example:

green -> yellow (TIMER)
yellow -> red (TIMER)
red {
  walk -> countdown (COUNTDOWN_START)
  countdown -> stop (COUNTDOWN_STOP)
  stop!
} -> green (TIMER)

When you enter the red state from yellow, you immediately go into red.walk (which allows pedestrians to walk). Upon reaching red.stop (which disallows pedestrians from walking), actions are handled from the parent red state, so the TIMER going off would transition it back to the green state.

####Formatting / Best Practices

  • Indent transitions on a new line for each transition.
  • Always declare all states used in the state machine (be explicit!)
  • Keep actions on the same line as their transition.
  • Indent nested states on a new line for each nested state.

###AST / State Machine Schema Read here if you're curious to the AST and State Machine Schema that this language produces.

API

machine(data, options = {})

Creates a new Machine() instance with the specified data (see the schema above) and options (optional).

  • data: (object | string) The definition of the state machine.
  • options: (object) Machine-specific options:
    • deterministic: (boolean) Specifies whether the machine is deterministic or nondeterministic (default: true)

Machine.transition(state = null, action = null)

Returns the next state, given a current state and an action. If no state nor action is provided, the initial state is returned.

Note: This is a pure function, and does not maintain internal state.

  • state: (string) The current state ID.
  • action: (string | Action) The action that triggers the transition from the state to the next state.

Example:

lightMachine.transition();
// => 'green'

lightMachine.transition('green', 'TIMER');
// => 'yellow'

lightMachine.transition('yellow', { type: 'TIMER' });
// => 'red'

lightMachine.transition('yellow');
// => 'yellow'