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

@welingtonms/use-automaton

v0.3.2

Published

React hook that implements the basic mechanism of an automaton.

Downloads

7

Readme

use-automaton

Coverage Status

React hook that implements the basic mechanism of an automaton. It can be used to implement a more generic reducer-like logic.

Whenever you have a number of states whose transition from one to another are based on specific tokens, just like our know friend automaton, that's where this hook comes in handy.

Instalation

You can add this hook as a dependency by running npm install @cheesebit/use-automaton or yarn add @cheesebit/use-automaton.

Props

- states

This object will represent the states of your automaton, allong with its transitions.

Your states prop, should look something like this.

const states = {
  // 'state-1' is the state identifier
  'state-1': {
    // here we defined the transitions we allow for this step
    on: {
      // when transition receives 'some-token', automaton will transition to 'state-2'
      'some-token': 'state-2',
    },
  },
  // you can have as many states you wish
};

Important to notice here that you can use token to send the automaton to a dead-end state (where there is no transition from) if you want it to remain like that; once at this state, there is no way to get out of it.

- initialCurrent

Initial state from where we will start this automaton.

Usage guide

export default function App() {
  const states = {
    'state-1': {
      on: {
        'token-a': 'state-2',
        'token-b': 'state-3',
      },
    },
    'state-2': {
      on: {
        'token-c': 'state-3',
      },
    },
    'state-3': {
      on: {
        'token-d': 'state-1',
        'token-e': 'dead-end',
      },
    },
  };
  const { transition, current } = useAutomaton(states, 'state-1');
  const [token, setToken] = React.useState('');

  return (
    <div className="App">
      <h1>useAutomaton</h1>
      <div className="flex flex-col justify-start p-4">
        <p className="mt-2">
          Current: <code>{current}</code>
        </p>
        <label className="flex flex-row items-center">
          Type the transition token
          <input
            type="text"
            className="mx-2 border px-4 py-2"
            value={token}
            onChange={e => {
              const {
                target: { value },
              } = e;

              setToken(value);
            }}
          />
          <button
            type="button"
            className="bg-blue-500 px-4 py-2 text-white"
            onClick={() => {
              transition(token);
              setToken('');
            }}
          >
            Transition
          </button>
        </label>
      </div>
    </div>
  );
}