UNPKG

killa

Version:

State management for Vanilla and React

61 lines (60 loc) 1.83 kB
import { type Store } from 'killa/core'; export interface StoreWithPersist { $$persist: symbol; name: string; destroy: () => void; rehydrate: () => void; hydrated: () => boolean; } declare module '../core' { interface Store { persist?: StoreWithPersist; } } /** Killa Storage API interface. */ export interface CustomStorage<T> { /** * Returns the current value associated with the given key, * or null if the given key does not exist. **/ getItem: (name: string) => T | null; /** * Sets the value of the pair identified by key to value, creating a * new key/value pair if none existed for key previously. */ setItem: (name: string, value: unknown) => void; /** * Removes the key/value pair with the given key, if a key/value pair with * the given key exists. */ removeItem: (name: string) => void; } export interface PersistConfig<T> { /** Storage name (unique) */ name?: string; /** * @default () => localStorage */ storage?: CustomStorage<T> | (() => CustomStorage<T>); merge?: (state: T, persistedState: T) => T; /** * Enable Revalidate mode * @default true */ revalidate?: boolean; /** * Timeout to trigger the revalidate event in milliseconds * @default 200 */ revalidateTimeout?: number; /** * Encrypt store using btoa and atob * @default false */ encrypted?: boolean; } export declare const normalizeStorage: <T>(initializerStorage: () => CustomStorage<T>, { encrypted }?: { encrypted?: boolean | undefined; }) => CustomStorage<T> | null; export declare const initRevalidateOnFocus: (listener: () => void) => () => void; export declare const persist: <T>(config: PersistConfig<T>) => (store: Store<T>) => void;