stunk
Version:
Stunk is a lightweight, framework-agnostic state management library for JavaScript and TypeScript. It uses chunk-based state units for efficient updates, reactivity, and performance optimization in React, Vue(WIP), Svelte(Coming soon), and Vanilla JS/TS a
24 lines (22 loc) • 1.02 kB
TypeScript
type Subscriber<T> = (newValue: T) => void;
type Middleware<T> = (value: T) => T | undefined;
interface Chunk<T> {
/** Get the current value of the chunk. */
get: () => T;
/** Peek at the current value without tracking dependencies. */
peek: () => T;
/** Set a new value for the chunk & Update existing value efficiently. */
set: (newValueOrUpdater: T | ((currentValue: T) => T)) => void;
/** Subscribe to changes in the chunk. Returns an unsubscribe function. */
subscribe: (callback: Subscriber<T>) => () => void;
/** Create a derived chunk based on this chunk's value. */
derive: <D>(fn: (value: T) => D) => ReadOnlyChunk<D>;
/** Reset the chunk to its initial value. */
reset: () => void;
/** Destroy the chunk and all its subscribers. */
destroy: () => void;
}
interface ReadOnlyChunk<T> extends Omit<Chunk<T>, 'set' | 'reset'> {
derive: <D>(fn: (value: T) => D) => ReadOnlyChunk<D>;
}
export type { Chunk as C, Middleware as M, ReadOnlyChunk as R };