All files / react-classic/src/storage persistent.ts

100% Statements 12/12
100% Branches 2/2
100% Functions 1/1
100% Lines 12/12

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    1x 1x                           1x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';
import type { ConstantState } from '../index.js';
import { useConstant } from '../index.js';
import { persistent } from '@anchorlib/storage';
 
/**
 * A React hook that creates a persistent state variable using the Anchor storage system.
 * This hook provides a way to store and retrieve state that persists across application sessions.
 *
 * @template T - The type of the initial value object
 * @template S - The schema type for the persistent state, defaults to LinkableSchema
 *
 * @param name - A unique identifier for the persistent state variable
 * @param init - The initial value for the persistent state
 * @param options - Optional configuration for the state management
 * @returns {ConstantState<T>} A constant state object containing the current value.
 */
export function usePersistent<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(
  name: string,
  init: T,
  options?: StateOptions<S>
): ConstantState<T> {
  const [state] = useConstant(() => {
    return persistent(name, init, options);
  }, [name, init, options]);
  return [state.value, state];
}