@tanstack/persister
Version:
Utilities for persisting state to local storage, session storage, indexedDB, and more.
34 lines (33 loc) • 963 B
TypeScript
/**
* Abstract class that defines the contract for a state persister implementation.
* A persister is responsible for loading and saving state to a storage medium.
*
* @example
* ```ts
* class MyPersister extends Persister<MyState> {
* constructor() {
* super(key)
* }
*
* loadState(): MyState | undefined {
* // Load state from storage
* return state
* }
*
* saveState(, state: MyState): void {
* // Save state to storage
* }
*
* clearState(useDefaultState?: boolean): void {
* // Clear state from storage or set the default state if provided and specified to be used
* }
* }
* ```
*/
export declare abstract class Persister<TState, TSelected extends Partial<TState> = TState> {
readonly key: string;
constructor(key: string);
abstract loadState: () => TSelected | undefined;
abstract saveState: (state: TState) => void;
abstract clearState: (useDefaultState?: boolean) => void;
}