@sandstack/neuron
Version:
Neuron is a lightweight framework agnostic global state manager for Javascript apps, with React support
61 lines (60 loc) • 2.45 kB
TypeScript
import { NeuronKey, IModule } from "../core";
/**
* The `Persist` function initializes a persistence module for neuron states.
* It allows states to be saved to and retrieved from browser storage or a custom storage mechanism.
*
* @param options - Configuration options for the `Persist` module.
* @returns A `Module` instance that handles state persistence.
*/
export declare const Persist: (options?: PersistOptions) => IModule;
/**
* Interface defining the options for the `Persist` module.
* Ensures `storageType` and `storage` cannot be used together.
*/
export type PersistOptions = {
/** Custom name for the module. */
name?: string;
/** storage key to be used Neuron state.
* This only needs to be used when a Neuron state does not have its own key and the module is instantiated on an individual Neuron instance.
*/
storageKey?: string;
storage?: never;
storageType?: never;
} | {
/** Custom name for the module. */
name?: string;
/** storage key to be used Neuron state.
* This only needs to be used when a Neuron state does not have its own key and the module is instantiated on an individual Neuron instance.
*/
storageKey?: string;
/** Browser storage type to use: `localStorage` or `sessionStorage`. Defaults to "local". */
storageType: "session" | "local";
/** Custom storage handlers are not allowed when `storageType` is defined. */
storage?: never;
} | {
/** Custom name for the module. */
name?: string;
/** storage key to be used Neuron state.
* This only needs to be used when a Neuron state does not have its own key and the module is instantiated on an individual Neuron instance.
*/
storageKey?: string;
/** Browser storage type is not allowed when `storage` is defined. */
storageType?: never;
/**
* Custom storage handlers for saving and retrieving state.
*/
storage: {
/**
* A custom function to retrieve an item from storage.
* @param key - The key of the item to retrieve.
* @returns The retrieved state or `null` if not found.
*/
getItem?: (key: NeuronKey) => unknown | null;
/**
* A custom function to save an item to storage.
* @param key - The key of the item to save.
* @param newState - The new state to save.
*/
setItem?: (key: NeuronKey, newState: unknown) => void;
};
};