statelet
Version:
Dead simple state management built on kvkit - composable, reactive, shareable. Supports Standard Schema (Valibot, Zod, ArkType).
41 lines (40 loc) • 1.11 kB
TypeScript
/**
* The simplest possible state container - just get, set, subscribe
*/
export interface State<T> {
get(): T;
set(value: T | ((prev: T) => T)): void;
subscribe(callback: (value: T) => void): () => void;
}
/**
* Create a reactive state container
*/
export declare function state<T>(initial: T): State<T>;
/**
* Enhancer function type - takes a state and returns an enhanced state
*/
export type Enhancer<T, U = T> = (base: State<T>) => State<U>;
/**
* Compose multiple enhancers into a single state
*
* @example
* const myState = compose(
* state({ count: 0 }),
* withLocalStorage('count'),
* withEffect(console.log)
* );
*/
export declare function compose<T>(base: State<T>, ...enhancers: Enhancer<any>[]): State<T>;
/**
* Curried version of compose for functional style
*
* @example
* const createCounter = enhance(
* withLocalStorage('count'),
* withEffect(console.log)
* );
*
* const counter = createCounter(state({ count: 0 }));
*/
export declare function enhance<T>(...enhancers: Enhancer<any>[]): (base: State<T>) => State<T>;
export * from './enhancers';