UNPKG

@sovgut/state

Version:

<p align="center"> <b>A lightweight, type-safe, and reactive state management library for modern web applications</b> </p>

77 lines (76 loc) 2.95 kB
import { Observer } from '../Observer/Observer.ts'; /** * Configuration options for retrieving values from state * @template T - The expected type of the value */ type Options<T = unknown> = { /** If true, throws an error when the key doesn't exist */ strict?: boolean; /** Default value to return when the key doesn't exist or has an empty value */ fallback?: T; /** Cast the value to a specific type */ cast?: 'string' | 'number' | 'boolean' | 'bigint'; }; /** * Session storage state management class that persists data using the browser's sessionStorage API. * Data persists only for the duration of the page session (until the tab is closed). * Extends Observer to provide event emission capabilities. */ export declare class SessionState extends Observer { /** * Retrieves a value from session storage with strict type checking * @template T - The expected type of the value * @param key - The key to retrieve * @param options - Options with strict mode enabled * @returns The value associated with the key * @throws {StateDoesNotExist} When the key doesn't exist * @throws {StateInvalidCast} When casting fails in strict mode */ static get<T = unknown>(key: string, options: Options<T> & { strict: true; }): T; /** * Retrieves a value from session storage with a fallback * @template T - The expected type of the value * @param key - The key to retrieve * @param options - Options with a fallback value * @returns The value associated with the key or the fallback */ static get<T = unknown>(key: string, options: Options<T> & { fallback: T; }): T; /** * Retrieves a value from session storage * @template T - The expected type of the value * @param key - The key to retrieve * @param options - Optional configuration * @returns The value associated with the key or undefined */ static get<T = unknown>(key: string, options?: Options<T>): T | undefined; /** * Stores a value in session storage * @template T - The type of the value to store * @param key - The key to store the value under * @param value - The value to store (will be JSON stringified) * @emits Will emit an event with the key and new value */ static set<T = unknown>(key: string, value: T): void; /** * Removes a value from session storage * @param key - The key to remove * @emits Will emit an event with the key and null value */ static remove(key: string): void; /** * Clears all values from session storage * @emits Will emit an event for each removed key with null value */ static clear(): void; /** * Checks if a key exists in session storage * @param key - The key to check * @returns True if the key exists, false otherwise */ static has(key: string): boolean; } export {};