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

use-state-machine

v3.0.5

Published

Use Finite State Machines with React Hooks

Downloads

46

Readme

npm bundle size Build Status JavaScript Style Guide

use-state-machine

Use Finite State Machines with React Hooks

Installation

$ npm install --save use-state-machine

Example

// H2O.js
import React from 'react'
import { useStateMachine } from 'use-state-machine'
import H2OState from './H2O.state'

function H2O () {
  const [current, transition] = useStateMachine(H2OState)
  return (
    <div>
      <p>Your H2O is in a {current.state} state.</p>
      <p>The temperature of your H2O is {current.value}.</p>
      <button
        disabled={!transition.toLiquid}
        onClick={() => transition.toLiquid()}>
        To Liquid
      </button>
      <button
        disabled={!transition.toSolid}
        onClick={() => transition.freeze()}>
        To Solid
      </button>
      <button
        disabled={!transition.toGas}
        onClick={() => transition.boil()}>
        To Gas
      </button>
    </div>
  )
}
// H2O.state.js
import { StateMachine } from 'use-state-machine'

export default new StateMachine({
  initial: 'liquid',
  liquid: {
    freeze: 'solid',
    boil: 'gas',
    value: '60F'
  },
  solid: {
    melt: 'liquid',
    value: '32F'
  },
  gas: {
    chill: 'liquid'
    value: '212F'
  }
})

API

useStateMachine

import { useStateMachine } from 'use-state-machine'

useStateMachine(machine: Object | StateMachine) -> [Object, Object]

useStateMachine takes a JavaScript object or StateMachine Object as an argument and returns an array consisting of a current object and a transition object.

current -> Object

The current state consists of two properties: state and value. state returns the string representing the current state. value returns the value (object or primitive) of the current state if one exists and returns undefined if not.

const [ current ] = useStateMachine(H2OState)

current.state //-> 'liquid'
current.value //-> '60F'

transition -> Object

transition is an object with a collection of functions allowing the developer to avoid transitioning using the string names. In the example above, when in the liquid state, two passive and two active functions exist on transition. The passive functions are transition.toSolid, transition.toGas. The two active functions are transition.freeze and transition.boil. All state specific functions on transition accept a single value argument.

If the value argument is an Object, the state's value and value argument will be merged. If the the state's value is not an Object, the state's value will be replaced with the value argument. If the state's value is a primitive and the value argument is an object, the state's value will be set to the value argument including a property named value set to the state's previous primitive value.

const [ current, transition ] = useStateMachine(H2OState)
transition.freeze()

current.state //-> 'solid'
current.value //-> '32F'

transition.melt()

current.state //-> 'liquid'

transition.toGas()

StateMachine

import { StateMachine } from 'use-state-machine'

new StateMachine(states: Object) -> StateMachine

To create an instance of a StateMachine pass a 'states' object. A valid 'states' object must have, at a minimum, a single state. And an initial property which is set to a valid state property.

There are two types of StateMachine definitions: "active" and passive. If the definition includes names for each valid transition it is an "active" definition and the transition property will include "active" functions (like freeze() and boild()). An example of an "active" definition is:

new StateMachine({
  initial: 'liquid',
  liquid: {
    freeze: 'solid',
    boil: 'gas',
    value: '60F'
  },
  solid: {
    melt: 'liquid',
    value: '32F'
  },
  gas: {
    chill: 'liquid'
    value: '212F'
  }
})

A "passive" definition uses the to property on each state indicating one or more valid states the current state can transition to. For a "passive" definition, the transition property will only include "passive" functions (like toSolid and toGas). An example of an "passive" definition is:

new StateMachine({
  initial: 'liquid',
  liquid: {
    to: ['solid', 'gas']
    value: '60F'
  },
  solid: {
    to: 'liquid'
    value: '32F'
  },
  gas: {
    to: 'liquid'
    value: '212F'
  }
})

<StateMachine>.state -> String

Return the string name of the StateMachine state.

<StateMachine>.value -> Any

value returns the value (object or primitive) of the current state if one exists and returns undefined if not.

<StateMachine>.transition -> Object

transition is an object with a collection of functions allowing the developer to avoid transitioning using the string names. In the example above, when in the liquid state, two passive and two active functions exist on transition. The passive functions are transition.toSolid, transition.toGas. The two active functions are transition.freeze and transition.boil. All state specific functions on transition accept a single value argument.

If the value argument is an Object, the state's value and value argument will be merged. If the the state's value is not an Object, the state's value will be replaced with the value argument. If the state's value is a primitive and the value argument is an object, the state's value will be set to the value argument including a property named value set to the state's previous primitive value.

<StateMachine>.onTransition(callback: Function) -> unsubscribe: Function

When a StateMachine object transitions from one state to another all callbacks passed to the onTransition function are evaluated with the StateMachine object passed as the only argument to the callback. onTransition returns a function that unsubscribes the callback when executed.

Maintainers

  • Mark Stahl

License

MIT