Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
latest
dinosaurland/stateDynamic state management
This package works with Cloudflare Workers, Node.js, Deno, Bun, 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.
- 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);
- Using disposable iterators
Theusingkeyword 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; } }
- 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
Add Package
deno add jsr:@dinosaur/state
Import symbol
import * as state from "@dinosaur/state";
Import directly with a jsr specifier
import * as state from "jsr:@dinosaur/state";
Add Package
pnpm i jsr:@dinosaur/state
pnpm dlx jsr add @dinosaur/state
Import symbol
import * as state from "@dinosaur/state";
Add Package
yarn add jsr:@dinosaur/state
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";