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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | 75x 15x 15x 26x 4x 22x 22x 22x 22x 15x 28x 28x 14x 28x 15x 28x 22x 28x 28x 28x 28x 22x 15x 15x 15x 13x 15x 15x 15x 15x 28x 15x 15x 15x 15x 22x 23x 22x 22x 15x 15x 15x 15x 2x 1x 1x 15x 14x 1x 15x 15x 15x 22x 23x 28x 28x 26x 28x 49x 6x 6x 43x 43x 43x 29x 14x 14x 4x | import React, {
Children,
cloneElement,
isValidElement,
useCallback,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import cartesian from 'cartesian';
import { normalizeClean, handlerHash, serializeTrigrams } from './helpers';
import { useTaoContext } from './hooks';
import { SwitchContext } from './SwitchContext';
import RenderHandler from './RenderHandler';
/** @typedef {import('@tao.js/core').Trigram} Trigram */
/** @typedef {import('./helpers').TrigramPart} TrigramPart */
/** @typedef {import('./helpers').NormalizedTrigram} NormalizedTrigram */
/**
* Props for {@link SwitchHandler}. Trigram parts (`t`/`term`, `a`/`action`,
* `o`/`orient`) are defaults merged under each RenderHandler child's own
* trigram parts; values may be arrays (multi-match), missing parts are
* wildcards.
* @typedef {Object} SwitchHandlerProps
* @property {TrigramPart} [term] - default term for RenderHandler children
* @property {TrigramPart} [action] - default action for RenderHandler children
* @property {TrigramPart} [orient] - default orient for RenderHandler children
* @property {TrigramPart} [t] - default term (short key)
* @property {TrigramPart} [a] - default action (short key)
* @property {TrigramPart} [o] - default orient (short key)
* @property {boolean} [debug=false] - log subscription/selection diagnostics
* @property {import('react').ReactNode} children - RenderHandler children to
* switch between (non-RenderHandler children always render)
*/
/**
* Whether a child is a RenderHandler element — directly, or via the
* `isTaoRenderHandler` static marker for wrapping components.
* @param {import('react').ReactNode} child
* @returns {child is import('react').ReactElement<any>}
*/
// Stryker disable all: type identity checks; proxy vs RenderHandler covered; ||/&& mutants equivalent under mixed children
function isRenderHandlerElement(child) {
return (
isValidElement(child) &&
!!child.type &&
(child.type === RenderHandler ||
!!(/** @type {*} */ (child.type).isTaoRenderHandler))
);
}
// Stryker restore all
/**
* Build the match table from RenderHandler children: one entry per child
* with its identity `matchKey` and the concrete trigram permutations it
* subscribes to (child trigram parts override the SwitchHandler defaults).
* @param {import('react').ReactNode} children
* @param {NormalizedTrigram} defaults
* @returns {Array<{matchKey: string, permutations: Trigram[], child: import('react').ReactElement<any>}>}
*/
function buildMatchTable(children, defaults) {
const entries = [];
Children.forEach(children, (child) => {
if (!isRenderHandlerElement(child)) {
return;
}
const childTrigram = normalizeClean(child.props);
const matchKey = handlerHash(childTrigram);
const permutations = cartesian({ ...defaults, ...childTrigram });
entries.push({ matchKey, permutations, child });
});
return entries;
}
/**
* Renders only the RenderHandler children whose trigrams matched the most
* recent AppCon wave (one Kernel dispatch; all children matching that same
* signal render, others unmount). Non-RenderHandler children pass through.
* @param {SwitchHandlerProps} props
* @returns {import('react').ReactElement}
*/
function SwitchHandler({
term,
action,
orient,
t,
a,
o,
// Stryker disable next-line BooleanLiteral: debug defaults false; logging is optional
debug = false,
children,
}) {
const TAO = useTaoContext();
// Stryker disable all: React hook dependency arrays — behavioral resubscribe covered; ArrayDeclaration equiv under perTest
const defaults = useMemo(
() => normalizeClean({ term, action, orient, t, a, o }),
[term, action, orient, t, a, o],
);
const matchTable = useMemo(
() => buildMatchTable(children, defaults),
[children, defaults],
);
const subTable = useMemo(
() =>
matchTable.map(({ matchKey, permutations }) => ({
matchKey,
permutations,
})),
[matchTable],
);
// Stryker restore all
const subTableKey = serializeTrigrams(subTable);
// Stryker disable next-line ObjectLiteral: initial chosen snapshot shape
const [chosen, setChosen] = useState(() => ({
matchKeys: new Set(),
tao: undefined,
data: undefined,
}));
// Wave: one Kernel AppCon dispatch; accumulate all matchKeys for that signal.
// Stryker disable next-line ObjectLiteral: wave accumulator seed
const waveRef = useRef({ waveKey: null, acc: null });
// Stryker disable all: attachMatch callback deps / inline handler body side effects
const attachMatch = useCallback(
(matchKey) => (tao, data) => {
const waveKey = `${tao.t}|${tao.a}|${tao.o}`;
debug &&
console.log('SwitchHandler::handleSwitch:', {
tao,
data,
matchKey,
waveKey,
});
if (waveRef.current.waveKey !== waveKey) {
waveRef.current = { waveKey, acc: new Set() };
}
waveRef.current.acc.add(matchKey);
const matchKeys = waveRef.current.acc;
setChosen({ matchKeys, tao, data });
debug &&
console.log('SwitchHandler::handleSwitch::set state with:', {
matchKeys,
tao,
data,
});
},
[debug],
);
// Stryker restore all
useEffect(() => {
// Stryker disable all: optional debug logging
debug &&
console.log('SwitchHandler::subscribe::props:', {
term,
action,
orient,
t,
a,
o,
debug,
});
debug && console.log('SwitchHandler::subscribe::subTable:', subTable);
// Stryker restore all
// Stryker disable all: subscribe/prune/cleanup; orient resubscribe + prune tests cover behavior; hook-dep mutants equiv
const attached = [];
for (const { matchKey, permutations } of subTable) {
const handler = attachMatch(matchKey);
permutations.forEach((trigram) => TAO.addInlineHandler(trigram, handler));
attached.push({ permutations, handler });
}
const allowed = new Set(subTable.map((entry) => entry.matchKey));
setChosen((prev) => {
let changed = false;
const next = new Set();
prev.matchKeys.forEach((key) => {
if (allowed.has(key)) {
next.add(key);
} else {
changed = true;
}
});
if (!changed && next.size === prev.matchKeys.size) {
return prev;
}
return { ...prev, matchKeys: next };
});
debug && console.log('SwitchHandler::subscribe::complete:', { attached });
return () => {
attached.forEach(({ permutations, handler }) => {
permutations.forEach((trigram) =>
TAO.removeInlineHandler(trigram, handler),
);
});
};
// eslint-disable-next-line -- react-hooks/exhaustive-deps (plugin not in flat config; deps list is intentional)
}, [TAO, subTableKey, attachMatch, debug]);
// Stryker restore all
// Stryker disable all: optional debug logging
debug && console.log('SwitchHandler::render::state:', chosen);
// Stryker restore all
// Stryker disable all: switch context memo deps
const switchValue = useMemo(
() => ({
defaults,
signal: { tao: chosen.tao, data: chosen.data },
}),
[defaults, chosen.tao, chosen.data],
);
// Stryker restore all
return (
<SwitchContext.Provider value={switchValue}>
{Children.map(children, (child) => {
if (!isRenderHandlerElement(child)) {
// Stryker disable all: optional debug logging
debug && console.log('SwitchHandler::render:returning child');
// Stryker restore all
return child;
}
// Stryker disable all: optional debug logging
debug && console.log('SwitchHandler::render:testing child');
// Stryker restore all
const matchKey = handlerHash(normalizeClean(child.props));
if (!chosen.matchKeys.has(matchKey)) {
return null;
}
// Stryker disable all: optional debug logging
debug && console.log('SwitchHandler::render:cloning child');
// Stryker restore all
// shouldRender: matching AppCon already fired; freshly mounted
// RenderHandlers would otherwise miss it and stay blank.
return cloneElement(
/** @type {import('react').ReactElement<any>} */ (child),
{
term,
action,
orient,
t,
a,
o,
.../** @type {Object<string, *>} */ (child.props),
shouldRender: true,
initialTao: chosen.tao,
initialData: chosen.data,
},
);
})}
</SwitchContext.Provider>
);
}
SwitchHandler.displayName = 'SwitchHandler';
export default SwitchHandler;
|