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, Svelte, and Vanilla JS/TS applications.
19 lines (17 loc) • 793 B
TypeScript
type Subscriber<T> = (newValue: T) => void;
type Middleware<T> = (value: T, next: (newValue: T) => void) => void;
interface Chunk<T> {
/** Get the current value of the chunk. */
get: () => 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) => Chunk<D>;
/** Reset the chunk to its initial value. */
reset: () => void;
/** Destroy the chunk and all its subscribers. */
destroy: () => void;
}
export type { Chunk as C, Middleware as M };