All files / react-tao/src DataHandler.js

100% Statements 6/6
100% Branches 0/0
100% Functions 2/2
100% Lines 6/6

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                                                                                                      17x 17x             17x 17x         17x               5x      
import React, { useContext, useMemo } from 'react';
 
import { DataLayerContext } from './DataLayerContext';
import useTaoDataState from './useTaoDataState';
 
/** @typedef {import('./helpers').TrigramPart} TrigramPart */
/**
 * @template [S=any]
 * @typedef {import('./useTaoDataState').TaoDataHandler<S>} TaoDataHandler
 */
 
/**
 * Props for {@link DataHandler}. Trigram parts (`t`/`term`, `a`/`action`,
 * `o`/`orient`) accept single values or arrays (multi-match); missing parts
 * are wildcards.
 * @typedef {Object} DataHandlerProps
 * @property {string} name - tree-scoped lookup key for `useTaoData(name)`
 *           in descendants (inner DataHandlers shadow outer ones)
 * @property {TaoDataHandler<*>} [handler] - derives the next data per
 *           matching AppCon (`(tao, data, set, current) => …`); omitted =
 *           the AppCon data replaces the value
 * @property {*} [default] - initial data or a lazy initializer function
 *           (defaults to `{}`)
 * @property {import('react').ReactNode} [children]
 * @property {TrigramPart} [term] - the term: the domain thing
 * @property {TrigramPart} [action] - the action: the operation on the term
 * @property {TrigramPart} [orient] - the orient: perspective / role / surface
 * @property {TrigramPart} [t] - the term (short key)
 * @property {TrigramPart} [a] - the action (short key)
 * @property {TrigramPart} [o] - the orient (short key)
 */
 
/**
 * Subscribes to TAO trigrams and exposes named state to descendants by
 * pushing onto the tree-scoped data layer stack (ancestor walk — read with
 * `useTaoData(name)`).
 * @param {DataHandlerProps} props
 * @returns {import('react').ReactElement}
 */
function DataHandler({
  name,
  handler,
  default: defaultValue,
  children,
  term,
  action,
  orient,
  t,
  a,
  o,
}) {
  const parentLayers = useContext(DataLayerContext);
  const localData = useTaoDataState(
    { term, action, orient, t, a, o },
    handler,
    defaultValue,
  );
 
  // Stryker disable all: useMemo deps / object literals — contents asserted; memo identity not observable
  const nextLayers = useMemo(
    () => [...parentLayers, { name, value: localData }],
    [parentLayers, name, localData],
  );
  // Stryker restore all
 
  return (
    <DataLayerContext.Provider value={nextLayers}>
      {children}
    </DataLayerContext.Provider>
  );
}
 
// Stryker disable next-line StringLiteral: displayName is DX-only
DataHandler.displayName = 'DataHandler';
 
export default DataHandler;