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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@effectionx/state-api

v0.1.0

Published

Reactive state container with typed reducers and middleware for Effection

Readme

State API

Reactive state container with typed reducers and middleware for Effection.


Usage

Basic state

import { run, each, spawn } from "effection";
import { useState } from "@effectionx/state-api";

await run(function* () {
  const counter = yield* useState(0);

  yield* counter.set(42);
  yield* counter.update((n) => n + 1);
  const value = yield* counter.get(); // 43
});

Typed reducers

Define named state transitions that are type-safe and interceptable.

import { run } from "effection";
import { useState } from "@effectionx/state-api";

interface Todo {
  id: number;
  text: string;
  done: boolean;
}

await run(function* () {
  const todos = yield* useState([] as Todo[], {
    add: (state, text: string) => [
      ...state,
      { id: state.length, text, done: false },
    ],
    toggle: (state, id: number) =>
      state.map((t) => (t.id === id ? { ...t, done: !t.done } : t)),
    remove: (state, id: number) => state.filter((t) => t.id !== id),
  });

  yield* todos.add("buy milk"); // returns the new state
  yield* todos.toggle(0);
  yield* todos.remove(0);

  // built-in operations still available
  yield* todos.set([]);
  yield* todos.update((s) => [...s]);
  const snapshot = yield* todos.get();
});

Each reducer is a function (state, ...args) => newState. The state argument is injected automatically; callers pass only the remaining arguments.

Stream subscription

State<T> implements Stream<T, void>, so you can subscribe to changes. The following continues from the todos example above:

// inside run(function* () { ... })
yield* spawn(function* () {
  for (const snapshot of yield* each(todos)) {
    console.log("todos changed:", snapshot);
    yield* each.next();
  }
});

Middleware

Every operation (set, update, get, and all reducer actions) can be intercepted with middleware via around():

// inside run(function* () { ... })

// log every state change
yield* todos.around({
  *set([value], next) {
    console.log("replacing state:", value);
    return yield* next(value);
  },
  *add([text], next) {
    console.log("adding todo:", text);
    return yield* next(text);
  },
});

// validate state transitions
yield* counter.around({
  *set([value], next) {
    if (value < 0) throw new Error("counter cannot be negative");
    return yield* next(value);
  },
});

// modify arguments
yield* counter.around({
  *update([fn], next) {
    return yield* next((n) => Math.max(0, fn(n)));
  },
});

Middleware is scoped: it applies only within the current Effection scope and is automatically removed when the scope exits.