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

@lauf/store-follow

v2.0.0

Published

Await @lauf/store state changes and trigger business logic

Downloads

46

Readme

Track selected changes to state

Promise-oriented tracking to monitor selected parts of a @lauf/store {@link Store}. Re-runs a Selector after each change to store state, and notifies when the value returned by the Selector changes.

Read the API Reference or the reference usages below.

Usage

Follow a Selector

// given this example store
const gameStore = createStore({
  steps: 0,
  direction: null,
});

// queue any changes to `steps` to be passed to a callback
followSelector(
  gameStore,
  (state) => state.steps,
  async (steps) => {
    stepDisplay.innerText = `Completed ${steps} steps`;
  }
);

Getting Started

Install

npm install @lauf/store-edit

Advanced Usage

Explicitly handle queue.receive()

For complex examples needing access to underlying queue logic, use withSelectorQueue. It's what followSelector uses under the hood.

Sometimes you can't afford the syntactic sugar of followSelector which subscribes your callback automatically and hides the queue.receive() API that is notified of changes to your selection.

Like followSelector, withSelectorQueue also creates and subscribes a Queue to be notified every time a new value is returned, but it passes this Queue direct to your handler along with the initial selected value. It unsubscribes and disposes the queue only when your handler returns.

Example

The withSelectorQueue example below needs direct access to queue.receive() as it waits for the first event of either...

  1. game character direction changed (from users keyboard input)
  2. timer expired (the character steps every 300ms)

It therefore has to use Promise.race() to handle either the receive or the timeout, whichever comes first.

// given this example store
const gameStore = createStore({
  steps: 0,
  direction: null,
});

// track direction (set elsewhere in the app)
const lastDirection = await withSelectorQueue(
  gameStore,
  (state) => state.direction,
  async function ({ receive }, initialDirection) {
    let direction = initialDirection;
    let directionPromise = null;
    let stepPromise = null;
    while (direction !== null) {
      // loop until player stopped moving
      directionPromise = directionPromise || receive();
      stepPromise = stepPromise || sleep(STEP_MS);
      const winner: string = await Promise.race([
        directionPromise.then(() => "direction"),
        stepPromise.then(() => "step"),
      ]);
      if (winner === "direction") {
        direction = await directionPromise; // direction changed
        directionPromise = null; // dispose promise
      } else if (winner === "step") {
        stepDirection(direction); // time to step
        stepPromise = null; // dispose promise
      }
    }
  }
);