nucleux
Version:
Simple, atomic hub for all your React application's state management needs. No providers, no boilerplate, just state that works.
31 lines (30 loc) • 1.08 kB
TypeScript
interface ReadOnlyValueInterface<V> {
readonly value: V;
subscribe: (callback: (value: V) => void) => string;
unsubscribe: (subId: string) => boolean;
}
interface ValueInterface<V> extends ReadOnlyValueInterface<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 type ValueOptions = {
storage: SupportedStorage;
};
declare class Value<V> implements ValueInterface<V> {
private _value;
private subscribers;
private persistKey?;
private storage?;
constructor(initialValue: V, persistKey?: string, options?: ValueOptions);
private hydrate;
get value(): V;
set value(newValue: V);
subscribe(callback: (value: V) => void): string;
unsubscribe(subId: string): boolean;
}
export { ReadOnlyValueInterface, Value, ValueInterface };