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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@staga/core

v1.0.0

Published

A TypeScript library for managing state transactions with saga pattern, rollbacks, and middleware support

Readme

Staga

TypeScript-first transactions and state orchestration with automatic rollback, retries, typed events, and reactive selectors. Now powered by a modern state core (statekit) with Signals and Streams.

Highlights

  • 🔄 Transactions with automatic rollback and per-step compensation
  • 🔁 Retries and ⏱️ timeouts per step
  • 🧩 Middleware (logging, timing, persistence)
  • 🧠 Reactive selectors (Signals) and event Streams
  • ⏪ Undo/Redo and snapshots in StateManager
  • 📡 Typed event API (onSagaEvent, onAnyEvent) and Streams (onEventStream)
  • 🧪 Event recording and replay

Install

npm install staga

Quick start

import { SagaManager } from 'staga';

type AppState = { users: string[]; count: number };

const saga = SagaManager.create<AppState>({ users: [], count: 0 });

// 1) Reactive selector (value-based subscription)
const count$ = saga.select(s => s.count);
const offCount = count$.subscribe(count => console.log('count =', count));

// 2) Typed event subscription
const offStart = saga.onSagaEvent('transaction:start', (e) => {
  console.log('TX started:', e.transactionName);
});

// 3) Transaction with compensation
const addUser = saga
  .createTransaction<{ name: string }>('add-user')
  .addStep('validate', (state, payload) => {
    if (!payload.name?.trim()) throw new Error('Name is required');
  })
  .addStep(
    'append-user',
    (state, payload) => {
      // mutate via StateManager
      saga.stateManager.setState({
        ...saga.getState(),
        users: [...saga.getState().users, payload.name],
        count: saga.getState().count + 1,
      });
    },
    // compensation on rollback
    (state, payload) => {
      const s = saga.getState();
      const users = s.users.slice();
      const idx = users.lastIndexOf(payload.name);
      if (idx >= 0) users.splice(idx, 1);
      saga.stateManager.setState({ ...s, users, count: s.count - 1 });
    },
    { retries: 2, timeout: 2000 }
  );

await addUser.run({ name: 'Ada' });

Core concepts

StateManager

import { StateManager } from 'staga';

const sm = new StateManager({ count: 0 });

sm.setState({ count: 1 });
sm.undo();
sm.redo();
sm.createSnapshot();
sm.rollbackToLastSnapshot();

// Reactive selector (value-based)
const count$ = sm.select(s => s.count);
const off = count$.subscribe(v => console.log(v));

Transactions

const transfer = saga
  .createTransaction<{ amount: number; from: string; to: string }>('transfer')
  .addStep('debit', (state, p) => {
    // ...debit logic
  }, (state, p) => {
    // ...compensate debit
  })
  .addStep('credit', (state, p) => {
    // ...credit logic
  }, (state, p) => {
    // ...compensate credit
  }, { retries: 3, timeout: 5000 });

await transfer.run({ amount: 100, from: 'A', to: 'B' });

Events (typed) and Streams

// Typed event listeners
const offAny = saga.onAnyEvent(e => console.log(e.type));
const offFail = saga.onSagaEvent('transaction:fail', e => console.error(e.error));

// Stream API (temporal)
const startStream = saga.onEventStream('transaction:start');
const offStream = startStream.subscribe(e => console.log('stream start', e.transactionName));

Middleware

import { createLoggingMiddleware, createTimingMiddleware, createPersistenceMiddleware } from 'staga';

saga.use(createLoggingMiddleware());
saga.use(createTimingMiddleware());
saga.use(createPersistenceMiddleware('app-state'));

Event recording and replay

saga.startRecording();
// ...run transactions & emit events
saga.stopRecording();
await saga.startReplay();

Demos

Build and open the comprehensive demo:

npm run build
# then open demo/index.html in a browser

The demo includes:

  • State selectors via Signals
  • Typed events and Streams
  • An AI agent powered by transactions (with retries/timeouts and compensation)
  • A multi-step tool orchestration pipeline with rollback

License

MIT