All files / react-classic/src action.ts

100% Statements 58/58
100% Branches 28/28
100% Functions 8/8
100% Lines 58/58

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 1071x 1x 1x   1x                                                         1x 19x   19x 19x 19x 1x 1x 19x 19x 19x   19x 19x   19x 19x 8x 8x 19x 19x 19x   19x 19x 15x 15x 19x 21x   21x 21x 21x 21x 19x 3x 3x 3x 3x 19x 19x                             1x 4x 4x 4x 4x 7x 7x 4x 3x 3x 3x 4x 2x 2x 2x 4x 4x 4x  
import { useEffect, useMemo, useRef, useState } from 'react';
import { useMicrotask } from './hooks.js';
import { CLEANUP_DEBOUNCE_TIME } from './constant.js';
import type { Action, ActionRef } from './types.js';
import { createObserver, type StateUnsubscribe } from '@anchorlib/core';
 
/**
 * Custom hook that manages an action with cleanup capabilities.
 *
 * This hook maintains a reference to a value and runs an action function whenever
 * the value changes. It automatically handles cleanup of the previous action
 * before running a new one, and also cleans up when the component unmounts.
 *
 * @template T - The type of the value being managed
 * @param action - A function that takes the current value and returns a cleanup function
 * @returns An object with getter and setter for the current value
 */
export function useAction<T>(action: Action<T>): ActionRef<T>;
 
/**
 * Custom hook that manages an action with cleanup capabilities.
 *
 * This hook maintains a reference to a value and runs an action function whenever
 * the value changes. It automatically handles cleanup of the previous action
 * before running a new one, and also cleans up when the component unmounts.
 *
 * @template T - The type of the value being managed
 * @param init - The initial value, can be null
 * @param action - A function that takes the current value and returns a cleanup function
 * @returns An object with getter and setter for the current value
 */
export function useAction<T>(init: T, action: Action<T>): ActionRef<T>;
 
export function useAction<T>(init: T | Action<T>, action?: Action<T>): ActionRef<T> {
  if (typeof init === 'function') action = init as Action<T>;
 
  const [cleanup, cancelCleanup] = useMicrotask(CLEANUP_DEBOUNCE_TIME);
  const [observer] = useState(() => {
    return createObserver(() => {
      actionRef.destroy?.();
      actionRef.destroy = (action as Action<T>)(actionRef.current) as StateUnsubscribe;
    });
  });
  const actionRef = useRef(typeof init === 'function' ? null : init) as ActionRef<T>;
 
  useEffect(() => {
    cancelCleanup();
 
    return () => {
      cleanup(() => {
        observer.destroy();
        actionRef.destroy?.();
      });
    };
  }, []);
 
  return {
    get current() {
      return actionRef.current;
    },
    set current(value: T) {
      if (value === actionRef.current) return;
 
      actionRef.destroy?.();
      actionRef.current = value;
      actionRef.destroy = observer.run(() => (action as Action<T>)(actionRef.current)) as StateUnsubscribe;
    },
    destroy() {
      actionRef.current = null as T;
      observer.destroy();
      actionRef.destroy?.();
    },
  };
}
 
/**
 * Custom hook that combines multiple action references into a single action reference.
 *
 * This hook allows you to synchronize multiple action references so that when the
 * combined action's value is updated, all individual actions are updated with the
 * same value. It also handles cleanup for all actions when the combined action is destroyed.
 *
 * Use cases: Apply class binding, style binding, and event listeners to the same element.
 *
 * @template T - The type of the value being managed
 * @param actions - An array of action references to be combined
 * @returns A single action reference that controls all provided actions
 */
export function useActions<T>(...actions: (ActionRef<T> | undefined)[]): ActionRef<T> {
  const selfRef = useRef<T>(null);
  return useMemo(() => {
    return {
      get current() {
        return selfRef.current as T;
      },
      set current(value: T) {
        selfRef.current = value;
        actions.filter((action) => typeof action === 'object').forEach((action) => (action.current = value));
      },
      destroy() {
        selfRef.current = null;
        actions.filter((action) => typeof action === 'object').forEach((action) => action.destroy?.());
      },
    };
  }, actions);
}