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

strong-reducer

v0.4.5

Published

A useReducer hook for React that's easy to use with Typescript.

Downloads

142

Readme

strong-reducer

A useReducer hook for React that's easy to use with Typescript.

Important Concurrency Note:

strong-reducer supports synchronous, async (Promise), and async iterable (async function*) reducers. However, only one dispatch can be active at a time, this prevents race conditions between dispatches. If you send a dispatch while another dispatch is being awaited, then that previous dispatch will be aborted.

What does this mean for...

  • synchronous reducers: a synchronous reducer cannot be canceled because there's no chance of concurrency. However, it will abort a running asynchronous reducer.
  • async (Promise) based reducers: the return value will be discarded if aborted.
  • async iterable (async function*) based reducers: the yielded values will be discarded if aborted.
type States = "idle" | "submitting" | "success" | "error";

const [state, { submit }] = useStrongReducer<States>("idle")({
  submit: (value: string) =>
    async function* () {
      yield "submitting";
      try {
        await apiClient.submitValue(value);
        yield "success";
      } catch (err) {
        yield "error";
      }
      await sleep(700);
      yield "idle";
    },
});

// click this a few times,
// it will not revert to idle 700ms after the FIRST click if there were subsequent clicks
// it will only revert back to idle 700ms after the most recent click
return (
  <button onClick={() => submit(someValue)}>
    {
      {
        idle: "Submit",
        submitting: "Processing...",
        success: "Succeeded!",
        error: "Error!",
      }[state]
    }
  </button>
);

Install

npm i strong-reducer

or

yarn add strong-reducer

Use

const [countBy, setCountBy] = React.useState(5);

const [count, useDispatcher] = useStrongReducerWithProps(
  0, // <------------------------------------- initial state
  countBy // <-------------------------------- reducer props
)({
  increase: // <------------------------------ dispatcher method name
    () =>  // <------------------------------- dispatcher method args (none)
      countBy => // <------------------------- reducer props
        count => count + countBy, // <-------- state updater
  
  set: // <----------------------------------- dispatcher method name
    (newCount: number) => // <---------------- dispatcher method args
      () => // <------------------------------ reducer props (omitted)
        newCount, // <------------------------ new state
  
  setAsync: // <------------------------------ dispatcher method name
    (newCount: number) => // <---------------- dispatcher method args
      () =>   // <---------------------------- reducer props (omitted)
        Promise.resolve(newCount), // <------- new state (async)
  
  increaseAsync: // <------------------------- dispatcher method name
    () => // <-------------------------------- dispatcher method args (none)
      countBy =>   // <----------------------- reducer props
        Promise.resolve( // <-------------------------------------------
          oldCount => oldCount + countBy // <- new state (async, updater)
        ), // <---------------------------------------------------------
  
  reload: // <-------------------------------- dispatcher method name
    () => // <-------------------------------- dispatcher method args (none)
      async () =>   // <---------------------- reducer props (omitted)
        await fetchFromServer(), // <--------- async fetch
  
  setAsyncIter: // <-------------------------- dispatcher method name
    () => // <-------------------------------- dispatcher method args
      async function* (
        props,
        dispatch // <------------------------- dispatch: { props, state, abort }
      ) {
        yield 1; // <------------------------- new state (async)
        await sleep(500);
        yield 2; // <------------------------- new state (async)
        await sleep(500);
        yield 3; // <------------------------- new state (async)
      },
});

return (
  <>
    <div>{ count }</div>
    <input type="number" onChange={ev => setCountBy(Number(ev.target.value))} />
    <button onClick={dispatcher.increase}>Increase</button>
    <button onClick={() => dispatcher.set(10)}>Set to 10</button>
    <button onClick={() => dispatcher.setAsync(10)}>Set to 10 async</button>
    <button onClick={dispatcher.increaseAsync}>Increase Async</button>
  </>
);