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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | 1x 1x 1x 1x 1x 1x 89x 89x 89x 89x 89x 89x 89x 81x 81x 81x 81x 89x 89x 16x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 14x 16x 89x 89x 89x 89x 89x 89x 89x 89x 63x 63x 89x 89x 131x 131x 89x 9x 9x 89x 89x 8x 7x 7x 2x 2x 2x 8x 1x 1x 1x 1x 1x 1x 1x 1x 89x 81x 81x 89x 89x 81x 81x 81x 76x 76x 81x 81x 89x 89x 11x 11x 78x 78x 1x 7x 7x 1x 10x 10x 21x 21x 6x 1x 29x 29x 1x 24x 14x 14x | import { useCallback, useEffect, useRef, useState } from 'react';
import { anchor, captureStack, softClone, softEqual } from '@anchorlib/core';
import { useMicrotask } from './hooks.js';
import type { ConstantRef, RefInitializer, RefUpdater, StateRef, VariableRef } from './types.js';
import { CLEANUP_DEBOUNCE_TIME } from './constant.js';
const REF_REGISTRY = new WeakMap();
export function useVariable<T>(init: T): [VariableRef<T>, RefUpdater<T>];
export function useVariable<T>(init: T, constant: true): [ConstantRef<T>];
export function useVariable<T>(init: RefInitializer<T>, deps: unknown[]): [VariableRef<T>, RefUpdater<T>];
export function useVariable<T>(init: RefInitializer<T>, deps: unknown[], constant: true): [ConstantRef<T>];
export function useVariable<T>(
init: T | RefInitializer<T>,
constantDeps?: boolean | unknown[],
constant?: boolean
): [VariableRef<T>, RefUpdater<T>] | [ConstantRef<T>] {
if (constantDeps === true) constant = constantDeps;
const [cleanup, cancelCleanup] = useMicrotask(CLEANUP_DEBOUNCE_TIME);
const [state] = useState<StateRef<T>>(() => {
return anchor({
value: typeof init === 'function' ? (init as RefInitializer<T>)() : init,
constant,
});
});
const update = useCallback((value: T) => {
if (initRef.constant === true) {
captureStack.violation.general(
'Assignment violation detected:',
'Attempted to modify the value of a constant.',
new Error('Constant is read-only'),
[
'Constant value cannot be changed after created.',
'- Constant only updated when its dependency changed.',
'- Use variable if you need to update its value later.',
],
Object.getOwnPropertyDescriptors(stateRef).value.set
);
return;
}
if (value === state.value) return;
state.value = typeof initRef.init === 'function' ? (init as RefInitializer<T>)(value) : value;
}, []);
const initRef = useRef({
init,
deps: [] as unknown[],
stable: false,
constant,
}).current;
if (typeof init === 'function' && Array.isArray(constantDeps) && !initRef.deps.length) {
initRef.deps = softClone(constantDeps);
}
const stateRef = useRef({
get value() {
return state.value;
},
set value(value: T) {
update(value);
},
}).current;
if (initRef.stable) {
if (typeof initRef.init === 'function') {
const newDeps = getNextDeps(initRef.deps, softClone(Array.isArray(constantDeps) ? constantDeps : []));
if (newDeps) {
state.value = (init as RefInitializer<T>)(state.value);
initRef.deps = newDeps;
}
} else {
if (initRef.constant !== constant) {
initRef.constant = constant;
}
if (initRef.init !== init) {
state.value = init as T;
initRef.init = init;
}
}
} else {
initRef.stable = true;
}
REF_REGISTRY.set(stateRef, state);
useEffect(() => {
cancelCleanup();
return () => {
cleanup(() => {
anchor.destroy(state);
REF_REGISTRY.delete(stateRef);
});
};
}, []);
if (constant === true) {
return [stateRef];
}
return [stateRef, update];
}
/**
* Creates a constant reference that never changes its value.
*
* @template T - The type of the constant value
* @param init - The initial value or initializer function
* @returns A tuple containing the constant reference
*/
export function useConstant<T>(init: T): [ConstantRef<T>];
/**
* Creates a constant reference that only updates when dependencies change.
*
* @template T - The type of the constant value
* @param init - The initializer function that computes the constant value
* @param deps - Dependency array that determines when the constant should be recalculated
* @returns A tuple containing the constant reference
*/
export function useConstant<T>(init: RefInitializer<T>, deps: unknown[]): [ConstantRef<T>];
/**
* Implementation of useConstant that creates a constant reference.
*
* @template T - The type of the constant value
* @param init - The initial value or initializer function
* @param deps - Optional dependency array for computed constants
* @returns A tuple containing the constant reference
*/
export function useConstant<T>(init: T | RefInitializer<T>, deps?: unknown[]): [ConstantRef<T>] {
return useVariable(init as RefInitializer<T>, deps as unknown[], true);
}
/**
* Determines whether an update is needed by comparing previous and next dependency arrays.
*
* @param prev - The previous dependency array
* @param next - The next dependency array
* @returns The next array if an update is needed, otherwise undefined
*/
export function getNextDeps(prev: unknown[], next: unknown[]): unknown[] | undefined {
if (prev.length !== next.length) return next;
for (let i = 0; i < prev.length; i++) {
if (!softEqual(prev[i], next[i], true)) return next;
}
}
/**
* Checks if a value is a reference (either variable or constant).
*
* @param value - The value to check
* @returns True if the value is a reference, false otherwise
*/
export function isRef(value: unknown): value is VariableRef<unknown> | ConstantRef<unknown> {
return REF_REGISTRY.has(value as WeakKey);
}
/**
* Retrieves the internal state reference for a given reference.
*
* @template T - The type of the reference value
* @param value - The reference (variable or constant) to get the state for
* @returns The internal state reference if the input is a valid reference, otherwise returns the input as-is
*/
export function getRefState<T>(value: T): StateRef<T> {
if (REF_REGISTRY.has(value as WeakKey)) return REF_REGISTRY.get(value as WeakKey);
return value as StateRef<T>;
}
|