All files / solid/src ref.ts

100% Statements 33/33
100% Branches 9/9
100% Functions 6/6
100% Lines 33/33

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 981x 1x 1x                                                                                 1x 7x 7x 7x 3x 3x 1x 1x 3x 4x 4x 2x 2x 4x 1x 1x 4x   7x   7x 7x 7x 7x   7x 7x                       1x 3x 3x                     1x 5x 5x  
import { anchor } from '@anchorlib/core';
import { onCleanup } from 'solid-js';
import { REF_REGISTRY } from './reactive.js';
import type { ConstantRef, VariableRef } from './types.js';
 
/**
 * @deprecated Use 'mutable()' instead.
 * Creates a reactive variable reference that can be used in Solid components.
 * This function creates a reactive state container that integrates with Solid's reactivity system
 * and Anchor's state management. The returned reference object provides getter and setter access
 * to the underlying reactive value.
 *
 * @typeParam T - The type of the value being stored
 * @param init - The initial value for the reactive variable
 * @returns A VariableRef object with getter and setter for the reactive value
 */
export function variableRef<T>(init: T): VariableRef<T>;
 
/**
 * @deprecated Use 'mutable()' instead.
 * Creates a constant reactive reference that can be used in Solid components.
 * This overload creates a read-only reactive reference that integrates with Solid's reactivity system.
 * The returned reference object only provides a getter for the underlying reactive value.
 *
 * @typeParam T - The type of the value being stored
 * @param init - The initial value for the reactive variable
 * @param constant - A literal true value to indicate this should be a constant reference
 * @returns A ConstantRef object with only a getter for the reactive value
 */
export function variableRef<T>(init: T, constant: true): ConstantRef<T>;
 
/**
 * @deprecated Use 'mutable()' instead.
 * Creates a reactive reference that can be used in Solid components.
 * This function creates a reactive state container that integrates with Solid's reactivity system
 * and Anchor's state management. Depending on the constant parameter, it returns either a
 * mutable reference (with getter and setter) or an immutable reference (with only getter).
 *
 * @typeParam T - The type of the value being stored
 * @param init - The initial value for the reactive variable
 * @param constant - Optional flag to indicate if this should be a constant (read-only) reference
 * @returns Either a VariableRef (if constant is false/undefined) or ConstantRef (if constant is true)
 */
export function variableRef<T>(init: T, constant?: boolean): VariableRef<T> {
  const state = anchor({ value: init }, { recursive: true });
  const stateRef =
    constant === true
      ? {
          get value() {
            return state.value;
          },
        }
      : {
          get value() {
            return state.value;
          },
          set value(value: T) {
            state.value = value;
          },
        };
 
  REF_REGISTRY.add(stateRef);
 
  onCleanup(() => {
    anchor.destroy(state);
    REF_REGISTRY.delete(stateRef);
  });
 
  return stateRef;
}
 
/**
 * @deprecated Use 'immutable()' instead.
 * Creates a constant reactive reference that can be used in Solid components.
 * This is a convenience function that creates a read-only reactive reference by calling
 * variableRef with the constant flag set to true.
 *
 * @typeParam T - The type of the value being stored
 * @param init - The initial value for the reactive variable
 * @returns A ConstantRef object with only a getter for the reactive value
 */
export function constantRef<T>(init: T): ConstantRef<T> {
  return variableRef(init, true);
}
 
/**
 * @deprecated Use 'isValueRef()' instead.
 * Checks if a given value is a reactive reference created by variableRef or constantRef.
 * This function can be used to determine if an object is a reactive reference that
 * integrates with the Solid and Anchor reactivity systems.
 *
 * @param state - The value to check
 * @returns true if the value is a reactive reference, false otherwise
 */
export function isRef(state: unknown): state is VariableRef<unknown> | ConstantRef<unknown> {
  return REF_REGISTRY.has(state as VariableRef<unknown>);
}