UNPKG

nucleux

Version:

Simple, atomic hub for all your React application's state management needs. No providers, no boilerplate, just state that works.

45 lines (44 loc) 1.57 kB
interface ReadOnlyAtomInterface<V> { readonly value: V; readonly initialValue: V; subscribe: (callback: (value: V, previousValue?: V) => void, immediate?: boolean) => string; unsubscribe: (subId: string) => boolean; } interface AtomInterface<V> extends ReadOnlyAtomInterface<V> { value: V; } type AnyFunction = (...args: any[]) => any; type MaybePromisify<T> = T | Promise<T>; type PromisifyMethods<T> = { [K in keyof T]: T[K] extends AnyFunction ? (...args: Parameters<T[K]>) => MaybePromisify<ReturnType<T[K]>> : T[K]; }; export type SupportedStorage = PromisifyMethods<Pick<Storage, 'getItem' | 'setItem'>>; export interface AtomMemoizationOptions<V> { type: 'shallow' | 'deep' | 'custom'; compare?: (a: V, b: V) => boolean; } export interface AtomPersistenceOptions { persistKey: string; storage?: SupportedStorage; } export interface AtomOptions<V> { persistence?: AtomPersistenceOptions; memoization?: AtomMemoizationOptions<V>; } declare class Atom<V> implements AtomInterface<V> { private _value; private _initialValue; private subscribers; private persistence?; private memoization?; constructor(initialValue: V, options?: AtomOptions<V>); private hydrate; private shouldSkipUpdate; private notifySubscribers; get initialValue(): V; get value(): V; set value(newValue: V); subscribe(callback: (value: V, previousValue?: V) => void, immediate?: boolean): string; unsubscribe(subId: string): boolean; } export { Atom, AtomInterface, ReadOnlyAtomInterface };