sokko-g/e2e/fixtures/keyed-state-probe.js
Shinuwa 22b397ebfa Document Sokko G's architecture and folders, and move JavaScript file headers to JSDoc @file blocks
Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md
(setup and step-by-step recipes) and a README in each main folder, with an
index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to
JSDoc @file blocks, define a three-level header template in website/AGENTS.md
and check the header in tests/file-headers.test.mjs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 09:42:13 +01:00

41 lines
1.3 KiB
JavaScript

/**
* @file Observe les valeurs validées et les setters différés d'un état lié à une clé.
*/
/* global sessionStorage */
import { createElement, StrictMode, useLayoutEffect } from "react";
import { createRoot } from "react-dom/client";
import { flushSync } from "react-dom";
import { useKeyedState } from "../../website/src/hooks/useKeyedState.js";
export function mountKeyedStateProbe(element) {
const root = createRoot(element);
const snapshots = [];
const setters = new Map();
let reads = 0;
function Probe({ identity }) {
const [value, setValue] = useKeyedState(identity, (key) => {
reads++;
return JSON.parse(sessionStorage.getItem(`keyed-probe:${key}`) || "[]");
});
useLayoutEffect(() => {
snapshots.push({ key: identity, value });
setters.set(identity, setValue);
});
return createElement("output", null, JSON.stringify(value));
}
function update(identity) {
snapshots.length = 0;
flushSync(() => root.render(createElement(StrictMode, null, createElement(Probe, { identity }))));
}
update("a");
return {
update,
snapshots,
get reads() { return reads; },
append: (key, value) => flushSync(() => setters.get(key)((current) => [...current, value])),
unmount: () => root.unmount()
};
}