Skip to main content
Home

Built and signed on GitHub Actions

Dynamic state management

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
11 months ago (0.2.2)

@dinosaur/state

A simple state management library for TypeScript.

Usage

Creating a new state

import { State } from "@dinosaur/state";

const counter = new State(0);
const name = new State<string | null>(null);

Changing the state

counter.value++;
name.value = "John Doe";

Handling state changes

There are multiple ways to listen for state changes.

  1. Using listeners
    The listener will be called whenever the state changes. You can also remove the listener when you no longer need it.
const counterListener = (value: number) => {
    console.log(`Counter: ${value}`);
    if (value === 10) {
        counter.removeListener(counterListener);
    }
};
counter.addListener(counterListener);
  1. Using disposable iterators
    The using keyword ensures that the iterator is properly disposed when the block is exited.
using counterValues = counter.watch();
for await (const value of counterValues) {
    console.log(`Counter: ${value}`);
    if (value === 10) {
        break;
    }
}
  1. Waiting for one change
const nextValue = await counter.next();
console.log(`Counter: ${nextValue}`);

Deriving a state

This is useful for creating derived states. The derived state will automatically update when the original state changes.

const displayName = name.derive((name) => name ?? "Anonymous");

Merging states

Create states that depend on multiple other states. The merged state will automatically update when any of the original states change.

const a = new State(1);
const b = new State(2);
const sum = State
    .merge({ a, b })
    .derive(({ a, b }) => a + b);
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@dinosaur/state

Import symbol

import * as state from "@dinosaur/state";
or

Import directly with a jsr specifier

import * as state from "jsr:@dinosaur/state";

Add Package

pnpm i jsr:@dinosaur/state
or (using pnpm 10.8 or older)
pnpm dlx jsr add @dinosaur/state

Import symbol

import * as state from "@dinosaur/state";

Add Package

yarn add jsr:@dinosaur/state
or (using Yarn 4.8 or older)
yarn dlx jsr add @dinosaur/state

Import symbol

import * as state from "@dinosaur/state";

Add Package

vlt install jsr:@dinosaur/state

Import symbol

import * as state from "@dinosaur/state";

Add Package

npx jsr add @dinosaur/state

Import symbol

import * as state from "@dinosaur/state";

Add Package

bunx jsr add @dinosaur/state

Import symbol

import * as state from "@dinosaur/state";