All files / tao-routing-core/src create-use-signal-effect.js

100% Statements 11/11
100% Branches 5/5
100% Functions 3/3
100% Lines 11/11

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                                                                                                                                                      5x 6x 6x 6x   6x 7x 2x   5x 1x   4x 4x        
import { applySignal } from './apply-signal';
 
/** @typedef {import('./apply-signal').RouteSignal} RouteSignal */
/** @typedef {import('./apply-signal').SignalTarget} SignalTarget */
 
/**
 * React `useEffect`-shaped function, typed structurally so this package
 * never imports React — inject `React.useEffect` (or a compatible stand-in
 * for tests).
 *
 * @typedef {(effect: () => void | (() => void), deps?: ReadonlyArray<*>) => void} UseEffectHook
 */
 
/**
 * React `useRef`-shaped function, typed structurally so this package never
 * imports React — inject `React.useRef` (or a compatible stand-in).
 *
 * @typedef {(initialValue: *) => { current: * }} UseRefHook
 */
 
/**
 * Hook returning the ambient TAO Kernel (any {@link SignalTarget}) —
 * `useTaoContext` from `@tao.js/react`, or a compatible stand-in.
 *
 * @typedef {() => SignalTarget} UseTaoContextHook
 */
 
/**
 * Hook reading the current route-entry signal from the host router's data
 * API (must call hooks internally, e.g. wrap `useLoaderData`).
 *
 * @typedef {() => RouteSignal | null | undefined} UseSignalHook
 */
 
/**
 * Apply a route-entry signal to a Kernel-shaped target — `applySignal`'s
 * shape (injectable for tests).
 *
 * @typedef {(kernel: SignalTarget, signal: RouteSignal | null | undefined) => boolean} ApplySignalFn
 */
 
/**
 * The hook a `createUseSignalEffect` factory returns: reads the host
 * router's signal each render and applies each new signal identity to the
 * ambient Kernel exactly once.
 *
 * @typedef {() => void} UseSignalEffectHook
 */
 
/**
 * Dependencies injected into `createUseSignalEffect`.
 *
 * @typedef {object} UseSignalEffectDeps
 * @property {UseEffectHook} useEffect
 * @property {UseRefHook} useRef
 * @property {UseTaoContextHook} useTaoContext
 * @property {UseSignalHook} useSignal `() => signal` (must call hooks internally)
 * @property {ApplySignalFn} [apply]
 */
 
/**
 * Factory for a React hook that applies a signal from a host-router data API.
 *
 * Inject React / router / TAO hooks so this package stays free of React imports.
 *
 * @param {UseSignalEffectDeps} deps
 * @returns {UseSignalEffectHook}
 */
export function createUseSignalEffect({
  useEffect,
  useRef,
  useTaoContext,
  useSignal,
  apply = applySignal,
}) {
  return function useSignalEffect() {
    const TAO = useTaoContext();
    const signal = useSignal();
    const applied = useRef(null);
 
    useEffect(() => {
      if (signal == null) {
        return;
      }
      if (applied.current === signal) {
        return;
      }
      applied.current = signal;
      apply(TAO, signal);
    }, [TAO, signal]);
  };
}