UNPKG

react-shared-states

Version:

Global state made as simple as useState, with zero config, built-in async caching, and automatic scoping.

74 lines (73 loc) 2.8 kB
import { AFunction, Prefix, SharedCreated, SharedValue } from './types'; export declare const staticStores: SharedCreated[]; export declare abstract class SharedValuesManager<T extends SharedValue, V> { data: Map<string, T>; defaultValue(): V; addListener(key: string, prefix: Prefix, listener: AFunction): void; removeListener(key: string, prefix: Prefix, listener: AFunction): void; callListeners(key: string, prefix: Prefix): void; init(key: string, prefix: Prefix, data: V, isStatic?: boolean): void; createStatic<X extends SharedCreated>(rest: Omit<X, 'key' | 'prefix'>, scopeName?: Prefix): { key: string; prefix: Prefix; } & Omit<X, "key" | "prefix">; initStatic(sharedCreated: SharedCreated): void; clearAll(withoutListeners?: boolean, withStatic?: boolean): void; clear(key: string, prefix: Prefix, withoutListeners?: boolean, withStatic?: boolean): void; get(key: string, prefix: Prefix): T | undefined; setValue(key: string, prefix: Prefix, data: V): void; has(key: string, prefix: Prefix): string | undefined; static prefix(key: string, prefix: Prefix): string; static extractPrefix(mapKey: string): string[]; useEffect(key: string, prefix: Prefix, unsub?: (() => void) | null): void; } export declare class SharedValuesApi<T extends SharedValue, V, R = T> { protected sharedData: SharedValuesManager<T, V>; constructor(sharedData: SharedValuesManager<T, V>); /** * get a value from the shared data * @param key * @param scopeName */ get<S extends string = string>(key: S, scopeName: Prefix): R; get<S extends string = string>(sharedCreated: SharedCreated): R; /** * set a value in the shared data * @param key * @param value * @param scopeName */ set<S extends string = string>(key: S, value: V, scopeName: Prefix): void; set<S extends string = string>(sharedCreated: SharedCreated, value: V): void; /** * clear all values from the shared data */ clearAll(): void; /** * clear all values from the shared data in a scope * @param scopeName */ clearScope(scopeName?: Prefix): void; /** * resolve a shared created object to a value * @param sharedCreated */ resolve(sharedCreated: SharedCreated): R; /** * clear a value from the shared data * @param key * @param scopeName */ clear(key: string, scopeName: Prefix): void; clear(sharedCreated: SharedCreated): void; /** * check if a value exists in the shared data * @param key * @param scopeName */ has(key: string, scopeName?: Prefix): boolean; /** * get all values from the shared data */ getAll(): Record<string, Record<string, any>>; }