All files / solid/src/storage persistent.ts

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

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  1x 1x                               1x 3x 3x 3x 3x 3x   3x 3x 3x   3x 3x  
import type { LinkableSchema, ObjLike, StateOptions } from '@anchorlib/core';
import { persistent } from '@anchorlib/storage';
import { onCleanup } from 'solid-js';
 
/**
 * @deprecated Use `persistent()` instead.
 * Creates a persistent state reference that automatically cleans up when the component unmounts.
 *
 * This function wraps the core `persistent` storage mechanism with Solid.js lifecycle management,
 * ensuring that persistent state is properly cleaned up when the component using it is destroyed.
 *
 * @template T - The type of the state object, must extend ObjLike
 * @template S - The schema type for the state, defaults to LinkableSchema
 * @param name - A unique identifier for the persistent state
 * @param init - The initial value for the state
 * @param options - Optional configuration for state behavior and schema validation
 * @returns A reactive state object that persists across sessions
 */
export function persistentRef<T extends ObjLike, S extends LinkableSchema = LinkableSchema>(
  name: string,
  init: T,
  options?: StateOptions<S>
): T {
  const state = persistent(name, init, options);
 
  onCleanup(() => {
    persistent.leave(state);
  });
 
  return state;
}