UNPKG

react-pouch

Version:

The simplest state management library ever. No providers, no flux, no context hell. Just plug-and-play state for React & React Native with 100% test coverage.

119 lines (103 loc) 4.6 kB
interface PluginHooks<T> { initialize?: (value: T) => T; setup?: (pouch: Pouch<T>) => void; onSet?: (newValue: T, oldValue: T) => T | void; } interface Plugin<T = any, TAugmentation = {}> extends PluginHooks<T> { readonly __type?: TAugmentation; } type SimplePlugin<T> = Plugin<T>; interface Pouch<T> { get(): T; set(value: T | ((current: T) => T)): void; subscribe(listener: () => void): () => void; use(): T; [key: string]: any; } type PouchWithPlugins<T, TPlugins extends readonly Plugin<T, any>[]> = Pouch<T> & UnionToIntersection<InferPluginAugmentations<TPlugins>>; type InferPluginAugmentations<TPlugins extends readonly Plugin<any, any>[]> = TPlugins extends readonly [infer First, ...infer Rest] ? First extends Plugin<any, infer TAug> ? Rest extends readonly Plugin<any, any>[] ? TAug & InferPluginAugmentations<Rest> : TAug : {} : {}; type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never; type Store<T> = Pouch<T>; declare function pouch<T>(initialValue: T): Pouch<T>; declare function pouch<T, TPlugins extends readonly Plugin<T, any>[]>(initialValue: T, plugins: TPlugins): PouchWithPlugins<T, TPlugins>; declare function store<T>(initialValue: T): Pouch<T>; declare function store<T>(initialValue: T, plugins: Plugin<T, any>[]): Pouch<T>; declare function store<T, TPlugins extends readonly Plugin<T, any>[]>(initialValue: T, plugins: TPlugins): PouchWithPlugins<T, TPlugins>; declare function withPlugins<T, TPlugins extends readonly Plugin<T, any>[]>(initialValue: T, plugins: TPlugins): PouchWithPlugins<T, TPlugins>; declare function usePouch<T>(pouchInstance: Pouch<T>): T; declare function useStore<T>(storeInstance: Pouch<T>): T; interface PersistOptions { storage?: "localStorage" | "sessionStorage"; serialize?: (value: any) => string; deserialize?: (value: string) => any; } declare function persist<T>(key: string, options?: PersistOptions): SimplePlugin<T>; interface RNPersistAugmentation { getStorageInfo(): { key: string; available: boolean; type: string; }; clearStorage(): Promise<void>; } interface RNPersistOptions { serialize?: (value: any) => string; deserialize?: (str: string) => any; asyncStorage?: { getItem(key: string): Promise<string | null>; setItem(key: string, value: string): Promise<void>; removeItem(key: string): Promise<void>; }; } declare function rnPersist<T>(key: string, options?: RNPersistOptions): Plugin<T, RNPersistAugmentation>; type Validator<T> = (value: T) => { isValid: boolean; error?: string; }; declare function validate<T>(validator: Validator<T>): SimplePlugin<T>; interface LoggerOptions { collapsed?: boolean; timestamp?: boolean; } declare function logger<T>(name: string, options?: LoggerOptions): SimplePlugin<T>; interface ComputedAugmentation<C> { computed(): C; } declare function computed<T, C>(computeFn: (value: T) => C): Plugin<T, ComputedAugmentation<C>>; interface SyncOptions { debounce?: number; onError?: (error: Error) => void; headers?: Record<string, string>; credentials?: "include" | "omit" | "same-origin"; method?: string; mode?: "cors" | "no-cors" | "same-origin"; [key: string]: any; } declare function sync<T>(url: string, options?: SyncOptions): SimplePlugin<T>; interface HistoryAugmentation<T> { undo(): void; redo(): void; canUndo(): boolean; canRedo(): boolean; history: { past: T[]; future: T[]; }; } declare function history<T>(maxSize?: number): Plugin<T, HistoryAugmentation<T>>; declare function encrypt<T>(secretKey: string): SimplePlugin<T>; declare function throttle<T>(ms: number): SimplePlugin<T>; declare function debounce<T>(ms: number): SimplePlugin<T>; interface SchemaDefinition { [key: string]: string | SchemaDefinition; } declare function schema<T>(schemaDefinition: SchemaDefinition): SimplePlugin<T>; interface AnalyticsOptions { trackInitial?: boolean; includeTimestamp?: boolean; sanitize?: (value: any) => any; } declare function analytics<T>(eventName: string, options?: AnalyticsOptions): SimplePlugin<T>; type Middleware<T> = (value: T, oldValue: T) => T; declare function middleware<T>(...middlewares: Middleware<T>[]): SimplePlugin<T>; export { Plugin, PluginHooks, Pouch, SimplePlugin, Store, analytics, computed, debounce, encrypt, history, logger, middleware, persist, pouch, rnPersist, schema, store, sync, throttle, usePouch, useStore, validate, withPlugins };