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

@litforge/state

v0.1.22

Published

Core state management library for LitForge projects. Provides a flexible event-sourcing system with persistence, snapshots, and Redux DevTools integration.

Downloads

551

Readme

@litforge/state

Core state management library for LitForge projects. Provides a flexible event-sourcing system with persistence, snapshots, and Redux DevTools integration.

Overview

@litforge/state is a framework-agnostic state management library that uses an event-sourcing pattern. It tracks all state changes as events, supports snapshots for performance, and provides persistence mechanisms for browser and Node.js environments.

Key Features

  • Event Sourcing: All state changes are tracked as immutable events
  • Persistence: Built-in support for browser (IndexedDB/localStorage) and Node.js (file system)
  • Snapshots: Create state snapshots for efficient replay
  • Redux DevTools: Full integration with Redux DevTools extension
  • TypeScript: Fully typed with TypeScript support
  • Framework Agnostic: Works with any framework or vanilla JavaScript

Installation

npm install @litforge/state
# or
pnpm add @litforge/state
# or
yarn add @litforge/state

Basic Usage

Creating a State System

import { createStateSystem, BrowserPersistor } from '@litforge/state';

const stateSystem = createStateSystem({
  persistor: new BrowserPersistor(), // Optional: defaults to BrowserPersistor
});

// Load persisted state
await stateSystem.loadFromPersistor();

Pushing Events

// Push an event
const event = stateSystem.push({
  type: 'USER_LOGIN',
  payload: { userId: '123', username: 'john' },
  meta: { source: 'login-form' }
});

Creating Snapshots

const currentState = { user: { id: '123' }, todos: [] };
const snapshot = stateSystem.snapshot(currentState);

Subscribing to Events

const unsubscribe = stateSystem.subscribe((event) => {
  console.log('New event:', event);
});

// Later, unsubscribe
unsubscribe();

Replaying Events

await stateSystem.replay(
  targetIndex,
  (event, index) => {
    // Apply event to your state
    applyEventToState(event);
  },
  (snapshot) => {
    // Restore from snapshot (optional)
    restoreState(snapshot.state);
  }
);

Export/Import

// Export state
const json = stateSystem.export();

// Import state
await stateSystem.import(json);

Singleton Pattern

For convenience, you can use the singleton pattern:

import { initStateSystem, getStateSystem } from '@litforge/state';

// Initialize (typically in your app setup)
await initStateSystem({
  connectDevtools: true,
  getState: () => yourCurrentState,
  restoreState: (state) => { /* restore state */ },
  name: 'MyApp'
});

// Get the instance anywhere
const stateSystem = getStateSystem();

Persistors

BrowserPersistor

Uses IndexedDB with localStorage fallback:

import { BrowserPersistor } from '@litforge/state';

const persistor = new BrowserPersistor();

NodeFilePersistor

For Node.js environments:

import { NodeFilePersistor } from '@litforge/state';

const persistor = new NodeFilePersistor('./state_store.json');

Custom Persistor

Implement the Persistor interface:

import type { Persistor, Persisted } from '@litforge/state';

class MyCustomPersistor implements Persistor {
  async save(data: Persisted): Promise<void> {
    // Your save logic
  }
  
  async load(): Promise<Persisted | null> {
    // Your load logic
  }
}

Redux DevTools Integration

import { connectReduxDevtools } from '@litforge/state';

const cleanup = connectReduxDevtools(
  stateSystem,
  () => yourCurrentState,
  {
    restoreState: (state) => { /* restore */ },
    name: 'MyApp'
  }
);

Types

import type {
  Event,
  Snapshot,
  Persistor,
  Subscriber,
  Persisted
} from '@litforge/state';

API Reference

ActionLog

Main class for managing events and snapshots.

  • push(event): Add a new event
  • snapshot(state): Create a state snapshot
  • getEvents(from, to): Get events in a range
  • subscribe(fn): Subscribe to new events
  • replay(index, applyEvent, restoreSnapshot): Replay events
  • export(): Export state as JSON
  • import(json): Import state from JSON
  • loadFromPersistor(): Load persisted state

License

Part of the LitForge component library.