All files / storage/src types.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73                                                                                                                                                 
import type { KeyLike, LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';
import type { SessionStorage } from './session.js';
 
export type StorageEvent = {
  type: 'set' | 'assign' | 'delete' | 'clear';
  name: KeyLike;
  value?: unknown;
};
export type StorageSubscriber = (event: StorageEvent) => void;
 
export interface SessionFn {
  /**
   * Creates a reactive session object that automatically syncs with sessionStorage.
   * The session object will persist data across page reloads within the same browser session.
   *
   * @template T - The type of the initial data object
   * @template S - The schema type for anchor options
   *
   * @param name - Unique identifier for the session storage instance
   * @param init - Initial data to populate the session storage
   * @param options - Optional anchor configuration options
   * @param storageClass - Custom storage class to use (defaults to SessionStorage)
   *
   * @returns A reactive proxy object that syncs with sessionStorage
   */
  <T extends ObjLike, S extends LinkableSchema = LinkableSchema>(
    name: string,
    init: T,
    options?: StateOptions<S>,
    storageClass?: typeof SessionStorage
  ): T;
 
  /**
   * Disconnects a reactive session object from sessionStorage synchronization.
   *
   * @template T - The type of the session object
   * @param state - The reactive session object to disconnect
   *
   * @example
   * ```typescript
   * const userSession = session('user', { id: 1, name: 'John' });
   * session.leave(userSession);
   * // userSession is no longer synced with sessionStorage
   * ```
   */
  leave<T extends ObjLike>(state: T): void;
}
 
/**
 * Interface for the persistent function that provides methods for creating and managing persistent storage.
 */
export interface PersistentFn {
  /**
   * Creates a reactive persistent object that syncs with local storage.
   *
   * @template T - The type of the initial data object
   * @template S - The type of the linkable schema
   * @param {string} name - The unique name for the persistent storage instance
   * @param {T} init - The initial data to populate the storage with
   * @param {StateOptions<S>} [options] - Optional configuration options for the storage
   * @returns {T} A reactive object that persists data to localStorage
   */
  <T extends ObjLike, S extends LinkableSchema = LinkableSchema>(name: string, init: T, options?: StateOptions<S>): T;
 
  /**
   * Disconnects a reactive persistent object from localStorage synchronization.
   *
   * @template T - The type of the object to disconnect
   * @param {T} state - The reactive object to stop syncing with localStorage
   */
  leave<T extends ObjLike>(state: T): void;
}