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

usestatetransition

v0.3.3

Published

Other flavor of a state machine hook

Downloads

6

Readme

useStateTransition

If you look for a classic state machine, check cassiozen/useStateMachine.

Problem

One of my project is an app that can open various editors. Each editor can have different implementation, but needs to follow the same lifecycle. I wanted to provide a neat small hook that would handle it, so specific editors don't have too keep too much knowledge about the host app.

While doing it turned out I need some sort of state machine. The difference was that I need it to be focused more on transition between specific states.

Other nice projects implementing state machine hook were following enter/leave version of the pattern, that was focused on the state, rather than transition.

Example

Check the tests example.

const { state, dispatch } = useStateTransition<EditorState>({
    initial: EditorState.INIT,
    flows: [
      {
        from: EditorState.INIT,
        to: EditorState.READY,
        on: (_, setState) => {
          setState(scenario === "new" ? EditorState.NEW : EditorState.LOAD);
        },
      },
      {
        from: [EditorState.NEW, EditorState.LOAD],
        to: EditorState.OPEN,
      },
    ],
  });

API

const { state, dispatch, error } = useStateTransition<StateType>(options)

state

Current state of provided StateType. The first value comes from initial field in options.

dispatch

type Dispatch = (requestedState: StateType, data?: unknown) => void;

You use it to trigger the transition. Transition to the same state won't trigger any updates. If the request create unknown transition error will be populated.

Conditional dispatch

You can pass a function to dispatch to decide which state you dispatch to based on the current state. It's useful when you dispatch comes from useCallback which would cache the initial state


dispatchFn((currentState, data) => {
  if (currentState = 1) {
    return {
      to: 2,
      data // you can decide to pass the data
    }
  }
  return {
      to: 3
      // or to filter out the data
  }
}

error

Display the latest error. At the moment it reports only about unknown transitions.

options

You need to define the initialValue of the state and the flows.

{
    initialValue: StateType,
    flows: Flow[]
}

Flow

{
    from: StateType | StateType[],
    to: State | StateType[],
    on?: (requestedState: StateType, setState: (state: StateType) => void, data?: unknown) => void;
}
  • from defines the state you want to transition from; you can specify one or many states.
  • to defines the state you want to transition to; you can specify oen or many states.
  • on (optional)
    • if absent the state machine will just allow transition
    • if present it works like a guard, in the end you can specify which state you want to set. It won't trigger another guard or test, it will just update the sate.
    • you have access to data passed, so you can update your other hooks.
    • it happens in useEffect

License

MIT