All files / react-tao/src hooks.js

100% Statements 23/23
100% Branches 10/10
100% Functions 10/10
100% Lines 20/20

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                              117x 117x                                 6x       6x 6x 6x 9x 6x 9x                                       2x                                             2x                                               2x                                 22x 22x 3x   19x 2x   17x 19x 15x        
import React from 'react';
 
import { Context } from './Provider';
import { useDataLayers } from './DataLayerContext';
import { getPermutations } from './helpers';
 
/** @typedef {import('@tao.js/core').Kernel} Kernel */
/** @typedef {import('@tao.js/core').Handler} Handler */
/** @typedef {import('./helpers').TrigramProps} TrigramProps */
 
/**
 * The Kernel provided by the nearest `TaoProvider`.
 * @returns {Kernel}
 */
export function useTaoContext() {
  const { TAO } = React.useContext(Context);
  return TAO;
}
 
/**
 * Shared add/remove effect behind the phase-specific handler hooks.
 * @param {'Inline'|'Async'|'Intercept'} handlerType
 * @param {TrigramProps} trigramProps
 * @param {Handler} handler
 * @param {import('react').DependencyList} [dependencies]
 * @returns {void}
 */
function useTaoEffect(
  handlerType,
  { t, term, a, action, o, orient },
  handler,
  dependencies,
) {
  const [addingHandler, removingHandler] = [
    `add${handlerType}Handler`,
    `remove${handlerType}Handler`,
  ];
  const TAO = useTaoContext();
  const permutations = getPermutations({ t, term, a, action, o, orient });
  React.useEffect(() => {
    permutations.forEach((trigram) => TAO[addingHandler](trigram, handler));
    return () => {
      permutations.forEach((trigram) => TAO[removingHandler](trigram, handler));
    };
  }, dependencies);
}
 
/**
 * Register `handler` as an inline-phase handler for every permutation of the
 * trigram while mounted.
 * @param {TrigramProps} trigramProps - trigram(s) to match (array parts
 *        multi-match; missing parts are wildcards)
 * @param {Handler} handler - `(tao, data) => …`; may return an AppCtx to chain
 * @param {import('react').DependencyList} [dependencies] - effect deps
 *        controlling re-subscription (omit to resubscribe every render)
 * @returns {void}
 */
export function useTaoInlineHandler(
  { t, term, a, action, o, orient },
  handler,
  dependencies,
) {
  useTaoEffect(
    'Inline',
    { t, term, a, action, o, orient },
    handler,
    dependencies,
  );
}
 
/**
 * Register `handler` as an async-phase handler for every permutation of the
 * trigram while mounted.
 * @param {TrigramProps} trigramProps - trigram(s) to match (array parts
 *        multi-match; missing parts are wildcards)
 * @param {Handler} handler - `(tao, data) => …`; may return an AppCtx to chain
 * @param {import('react').DependencyList} [dependencies] - effect deps
 *        controlling re-subscription (omit to resubscribe every render)
 * @returns {void}
 */
export function useTaoAsyncHandler(
  { t, term, a, action, o, orient },
  handler,
  dependencies,
) {
  useTaoEffect(
    'Async',
    { t, term, a, action, o, orient },
    handler,
    dependencies,
  );
}
 
/**
 * Register `handler` as an intercept-phase handler for every permutation of
 * the trigram while mounted (truthy return halts the dispatch; returning an
 * AppCtx replaces it).
 * @param {TrigramProps} trigramProps - trigram(s) to match (array parts
 *        multi-match; missing parts are wildcards)
 * @param {Handler} handler - `(tao, data) => …`
 * @param {import('react').DependencyList} [dependencies] - effect deps
 *        controlling re-subscription (omit to resubscribe every render)
 * @returns {void}
 */
export function useTaoInterceptHandler(
  { t, term, a, action, o, orient },
  handler,
  dependencies,
) {
  useTaoEffect(
    'Intercept',
    { t, term, a, action, o, orient },
    handler,
    dependencies,
  );
}
 
/**
 * Tree-scoped named data from ancestor DataHandlers (nearest wins — an inner
 * DataHandler shadows an outer one with the same name).
 * @param {string} [name] - the DataHandler `name` to look up; omitted or
 *        empty returns the nearest DataHandler's value
 * @returns {*} the matching DataHandler's current data, or `undefined` when
 *        no ancestor DataHandler matches
 */
export function useTaoData(name) {
  const layers = useDataLayers();
  if (!layers || !layers.length) {
    return;
  }
  if (name == null || name === '') {
    return layers[layers.length - 1].value;
  }
  for (let i = layers.length - 1; i >= 0; i -= 1) {
    if (layers[i].name === name) {
      return layers[i].value;
    }
  }
}