@buoy-gg/zustand 7.0.38 → 7.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/lib/commonjs/index.js +105 -1
  2. package/lib/commonjs/preset.js +102 -1
  3. package/lib/commonjs/zustand/components/ZustandActionButton.js +118 -1
  4. package/lib/commonjs/zustand/components/ZustandDetailViewToggle.js +48 -1
  5. package/lib/commonjs/zustand/components/ZustandEventFilterView.js +43 -1
  6. package/lib/commonjs/zustand/components/ZustandIcon.js +18 -1
  7. package/lib/commonjs/zustand/components/ZustandModal.js +772 -1
  8. package/lib/commonjs/zustand/components/ZustandStateChangeItem.js +117 -1
  9. package/lib/commonjs/zustand/components/ZustandStateDetailContent.js +372 -1
  10. package/lib/commonjs/zustand/components/ZustandStateInfoView.js +488 -1
  11. package/lib/commonjs/zustand/components/ZustandStoreBrowser.js +204 -1
  12. package/lib/commonjs/zustand/components/ZustandStoreDetailContent.js +471 -1
  13. package/lib/commonjs/zustand/components/ZustandStoreEditorScreen.js +393 -1
  14. package/lib/commonjs/zustand/hooks/useZustandStateChanges.js +74 -1
  15. package/lib/commonjs/zustand/sync/zustandSyncAdapter.js +382 -1
  16. package/lib/commonjs/zustand/utils/buoyZustandMiddleware.js +284 -1
  17. package/lib/commonjs/zustand/utils/editableState.js +146 -1
  18. package/lib/commonjs/zustand/utils/snapshotProvider.js +209 -1
  19. package/lib/commonjs/zustand/utils/testIDs.js +82 -1
  20. package/lib/commonjs/zustand/utils/zustandStateStore.js +379 -1
  21. package/lib/module/index.js +90 -1
  22. package/lib/module/preset.js +98 -1
  23. package/lib/module/zustand/components/ZustandActionButton.js +114 -1
  24. package/lib/module/zustand/components/ZustandDetailViewToggle.js +43 -1
  25. package/lib/module/zustand/components/ZustandEventFilterView.js +39 -1
  26. package/lib/module/zustand/components/ZustandIcon.js +11 -1
  27. package/lib/module/zustand/components/ZustandModal.js +767 -1
  28. package/lib/module/zustand/components/ZustandStateChangeItem.js +113 -1
  29. package/lib/module/zustand/components/ZustandStateDetailContent.js +368 -1
  30. package/lib/module/zustand/components/ZustandStateInfoView.js +483 -1
  31. package/lib/module/zustand/components/ZustandStoreBrowser.js +200 -1
  32. package/lib/module/zustand/components/ZustandStoreDetailContent.js +469 -1
  33. package/lib/module/zustand/components/ZustandStoreEditorScreen.js +389 -1
  34. package/lib/module/zustand/hooks/useZustandStateChanges.js +70 -1
  35. package/lib/module/zustand/sync/zustandSyncAdapter.js +380 -1
  36. package/lib/module/zustand/utils/buoyZustandMiddleware.js +278 -1
  37. package/lib/module/zustand/utils/editableState.js +138 -1
  38. package/lib/module/zustand/utils/snapshotProvider.js +209 -1
  39. package/lib/module/zustand/utils/testIDs.js +74 -1
  40. package/lib/module/zustand/utils/zustandStateStore.js +375 -1
  41. package/lib/typescript/index.d.ts.map +1 -0
  42. package/lib/typescript/preset.d.ts.map +1 -0
  43. package/lib/typescript/zustand/components/ZustandActionButton.d.ts.map +1 -0
  44. package/lib/typescript/zustand/components/ZustandDetailViewToggle.d.ts.map +1 -0
  45. package/lib/typescript/zustand/components/ZustandEventFilterView.d.ts.map +1 -0
  46. package/lib/typescript/zustand/components/ZustandIcon.d.ts.map +1 -0
  47. package/lib/typescript/zustand/components/ZustandModal.d.ts.map +1 -0
  48. package/lib/typescript/zustand/components/ZustandStateChangeItem.d.ts.map +1 -0
  49. package/lib/typescript/zustand/components/ZustandStateDetailContent.d.ts.map +1 -0
  50. package/lib/typescript/zustand/components/ZustandStateInfoView.d.ts.map +1 -0
  51. package/lib/typescript/zustand/components/ZustandStoreBrowser.d.ts.map +1 -0
  52. package/lib/typescript/zustand/components/ZustandStoreDetailContent.d.ts.map +1 -0
  53. package/lib/typescript/zustand/components/ZustandStoreEditorScreen.d.ts.map +1 -0
  54. package/lib/typescript/zustand/hooks/useZustandStateChanges.d.ts.map +1 -0
  55. package/lib/typescript/zustand/sync/zustandSyncAdapter.d.ts.map +1 -0
  56. package/lib/typescript/zustand/types/index.d.ts.map +1 -0
  57. package/lib/typescript/zustand/utils/buoyZustandMiddleware.d.ts.map +1 -0
  58. package/lib/typescript/zustand/utils/editableState.d.ts.map +1 -0
  59. package/lib/typescript/zustand/utils/snapshotProvider.d.ts.map +1 -0
  60. package/lib/typescript/zustand/utils/testIDs.d.ts.map +1 -0
  61. package/lib/typescript/zustand/utils/zustandStateStore.d.ts.map +1 -0
  62. package/package.json +3 -3
@@ -1 +1,146 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.STRIP_MAX_DEPTH=void 0,exports.computeMergePartial=computeMergePartial,exports.editableStateOf=editableStateOf,exports.isPlainObjectLike=isPlainObjectLike,exports.stripFunctions=stripFunctions;const STRIP_MAX_DEPTH=exports.STRIP_MAX_DEPTH=8;function isPlainObjectLike(e){if("object"!=typeof e||null===e||Array.isArray(e))return!1;const t=Object.getPrototypeOf(e);return t===Object.prototype||null===t}function stripFunctions(e,t,n,i,r){if("function"==typeof e)return void n.push(t);if("object"!=typeof e||null===e||r>=STRIP_MAX_DEPTH)return e;if(i.has(e))return;if(i.add(e),Array.isArray(e))return e.map((e,s)=>"function"==typeof e?(n.push(`${t}[${s}]`),null):stripFunctions(e,`${t}[${s}]`,n,i,r+1));if(!isPlainObjectLike(e))return e;const s={};for(const[o,a]of Object.entries(e))"function"!=typeof a?s[o]=stripFunctions(a,t?`${t}.${o}`:o,n,i,r+1):n.push(t?`${t}.${o}`:o);return s}function editableStateOf(e){if(isPlainObjectLike(e)&&!0===e.__buoyStateOnDevice)return{data:null,fnPaths:[],blockedReason:"Store state hasn't synced from the device yet."};if(!isPlainObjectLike(e))return{data:null,fnPaths:[],blockedReason:"Only stores whose state is a plain object can be edited — a merge write has nothing to address in "+(Array.isArray(e)?"an array":"a "+typeof e)+"."};const t=[],n=stripFunctions(e,"",t,new WeakSet,0),i=t.filter(e=>e.includes(".")||e.includes("["));return i.length>0?{data:null,fnPaths:t,blockedReason:`State keeps a function inside "${i[0]}" — a merge write of that key would drop it, so this store can't be edited here.`}:{data:n,fnPaths:t,blockedReason:null}}function computeMergePartial(e,t){if(!isPlainObjectLike(t))throw new Error("The store's state must stay an object.");const n=Object.keys(e).filter(e=>!(e in t));if(n.length>0)throw new Error(`Can't remove top-level ${1===n.length?"key":"keys"} ${n.map(e=>`"${e}"`).join(", ")} — zustand merge writes can't delete. Edit the value instead.`);const i={};for(const n of Object.keys(t))n in e&&JSON.stringify(t[n])===JSON.stringify(e[n])||(i[n]=t[n]);return i}
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.STRIP_MAX_DEPTH = void 0;
7
+ exports.computeMergePartial = computeMergePartial;
8
+ exports.editableStateOf = editableStateOf;
9
+ exports.isPlainObjectLike = isPlainObjectLike;
10
+ exports.stripFunctions = stripFunctions;
11
+ /**
12
+ * What of a zustand store's state can be EDITED, and how an edit is written.
13
+ *
14
+ * The two zustand facts every rule here follows from:
15
+ *
16
+ * 1. Stores conventionally keep their action functions IN state
17
+ * (`increment: () => …`). Functions can't be typed into a tree editor and
18
+ * don't survive serialization, so the editor works on a function-free view
19
+ * of state.
20
+ * 2. `setState` is either a TOP-LEVEL SHALLOW MERGE (`Object.assign({}, state,
21
+ * partial)`) or a full replace that deletes every key not present — actions
22
+ * included. So the editor writes merge-only, always: every edit, however
23
+ * deep, is expressed as "replace the top-level key(s) that contain it"
24
+ * ({@link computeMergePartial}), and the two edits a merge cannot express —
25
+ * removing or renaming a top-level key — are refused rather than silently
26
+ * not happening.
27
+ *
28
+ * `stripFunctions` lives here (the Time Machine snapshot provider imports it)
29
+ * because capture and editing need the identical answer to "what is this
30
+ * store's data".
31
+ */
32
+
33
+ /** Bounds every recursive walk over store state. */
34
+ const STRIP_MAX_DEPTH = exports.STRIP_MAX_DEPTH = 8;
35
+ function isPlainObjectLike(v) {
36
+ // Only objects whose own enumerable keys ARE their data. A Map, Set, Date,
37
+ // RegExp or typed array has none, so walking it with Object.entries turns
38
+ // it into `{}` — which is how a restored store once got `{}` where the app
39
+ // kept a Map and crashed on `.values()`. Those go through as-is; the vault's
40
+ // superjson knows how to carry them.
41
+ if (typeof v !== "object" || v === null || Array.isArray(v)) return false;
42
+ const proto = Object.getPrototypeOf(v);
43
+ return proto === Object.prototype || proto === null;
44
+ }
45
+
46
+ /** Clone `value` without functions, recording their paths. */
47
+ function stripFunctions(value, path, fnPaths, seen, depth) {
48
+ if (typeof value === "function") {
49
+ fnPaths.push(path);
50
+ return undefined;
51
+ }
52
+ if (typeof value !== "object" || value === null || depth >= STRIP_MAX_DEPTH) {
53
+ return value;
54
+ }
55
+ if (seen.has(value)) return undefined; // cycle — drop the back-reference
56
+ seen.add(value);
57
+ if (Array.isArray(value)) {
58
+ return value.map((item, i) => typeof item === "function" ? (fnPaths.push(`${path}[${i}]`), null) : stripFunctions(item, `${path}[${i}]`, fnPaths, seen, depth + 1));
59
+ }
60
+ if (!isPlainObjectLike(value)) return value; // Date/Map/Set/etc. — superjson handles
61
+ const out = {};
62
+ for (const [k, v] of Object.entries(value)) {
63
+ if (typeof v === "function") {
64
+ fnPaths.push(path ? `${path}.${k}` : k);
65
+ continue;
66
+ }
67
+ out[k] = stripFunctions(v, path ? `${path}.${k}` : k, fnPaths, seen, depth + 1);
68
+ }
69
+ return out;
70
+ }
71
+ /**
72
+ * The editor's view of one store's state, with the reasons it may refuse.
73
+ *
74
+ * - A remote mirror that hasn't hydrated yet holds a marker, not state — the
75
+ * real value is one `getStoreState` round trip away, so "wait" rather than
76
+ * "can't".
77
+ * - Non-object state (`create(() => 5)`) has no top-level keys, so a merge
78
+ * write cannot address any part of it. Editing it would require a replace,
79
+ * which is the destructive path this editor exists to avoid.
80
+ * - A function nested BELOW the top level (rare — the convention keeps actions
81
+ * at the top) sits inside the very key an edit would rewrite, and a merge
82
+ * write of that key would silently drop it. Refusing the whole store is
83
+ * blunt but honest; per-key locks can come later if this ever bites.
84
+ */
85
+ function editableStateOf(state) {
86
+ if (isPlainObjectLike(state) && state.__buoyStateOnDevice === true) {
87
+ return {
88
+ data: null,
89
+ fnPaths: [],
90
+ blockedReason: "Store state hasn't synced from the device yet."
91
+ };
92
+ }
93
+ if (!isPlainObjectLike(state)) {
94
+ return {
95
+ data: null,
96
+ fnPaths: [],
97
+ blockedReason: "Only stores whose state is a plain object can be edited — " + "a merge write has nothing to address in " + (Array.isArray(state) ? "an array" : `a ${typeof state}`) + "."
98
+ };
99
+ }
100
+ const fnPaths = [];
101
+ const data = stripFunctions(state, "", fnPaths, new WeakSet(), 0);
102
+ const nested = fnPaths.filter(p => p.includes(".") || p.includes("["));
103
+ if (nested.length > 0) {
104
+ return {
105
+ data: null,
106
+ fnPaths,
107
+ blockedReason: `State keeps a function inside "${nested[0]}" — a merge write of ` + "that key would drop it, so this store can't be edited here."
108
+ };
109
+ }
110
+ return {
111
+ data,
112
+ fnPaths,
113
+ blockedReason: null
114
+ };
115
+ }
116
+
117
+ /**
118
+ * Diff an edited draft against the state it was seeded from, as a `setState`
119
+ * MERGE partial: only the top-level keys whose contents changed.
120
+ *
121
+ * Diffed against the SEED, not the live state at save time, on purpose — the
122
+ * partial then carries exactly what the user changed, so a key the app wrote
123
+ * while the editor was open and the user never touched keeps the app's value
124
+ * (per-key last-write-wins, the same contract the draft's own dirtiness uses).
125
+ *
126
+ * Throws on the two edits a merge cannot express, naming the key: a draft
127
+ * that dropped a top-level key (which is also what a raw-JSON rename produces
128
+ * — the old key dropped, a new one added), because `setState(partial, false)`
129
+ * would leave it exactly as it was while the editor showed it gone.
130
+ */
131
+ function computeMergePartial(seed, draft) {
132
+ if (!isPlainObjectLike(draft)) {
133
+ throw new Error("The store's state must stay an object.");
134
+ }
135
+ const dropped = Object.keys(seed).filter(k => !(k in draft));
136
+ if (dropped.length > 0) {
137
+ throw new Error(`Can't remove top-level ${dropped.length === 1 ? "key" : "keys"} ` + `${dropped.map(k => `"${k}"`).join(", ")} — zustand merge writes ` + "can't delete. Edit the value instead.");
138
+ }
139
+ const partial = {};
140
+ for (const key of Object.keys(draft)) {
141
+ if (!(key in seed) || JSON.stringify(draft[key]) !== JSON.stringify(seed[key])) {
142
+ partial[key] = draft[key];
143
+ }
144
+ }
145
+ return partial;
146
+ }
@@ -1 +1,209 @@
1
- "use strict";var _sharedUi=require("@buoy-gg/shared-ui"),_zustandStateStore=require("./zustandStateStore"),_editableState=require("./editableState");const MAX_DEPTH=_editableState.STRIP_MAX_DEPTH;function graftFunctions(t,e,s,a){if((0,_editableState.isPlainObjectLike)(e)&&(0,_editableState.isPlainObjectLike)(t)&&!(a>=MAX_DEPTH||s.has(e))){s.add(e);for(const[n,r]of Object.entries(e))if("function"==typeof r)t[n]=r;else if((0,_editableState.isPlainObjectLike)(r)){if(!(0,_editableState.isPlainObjectLike)(t[n])){if(!containsFunction(r,new WeakSet,a+1))continue;t[n]={}}graftFunctions(t[n],r,s,a+1)}}}function containsFunction(t,e,s){if(s>=MAX_DEPTH||e.has(t))return!1;e.add(t);for(const a of Object.values(t)){if("function"==typeof a)return!0;if((0,_editableState.isPlainObjectLike)(a)&&containsFunction(a,e,s+1))return!0}return!1}function status(){return 0===_zustandStateStore.zustandStateStore.getStores().length?{canCapture:!1,canRestore:!1,detail:"No Zustand stores registered — pass `zustandStores` to FloatingDevTools or add the buoyDevTools middleware"}:{canCapture:!0,canRestore:!0}}async function capture(){const t=[],e={};for(const s of _zustandStateStore.zustandStateStore.getStores()){const a=[];let n;try{n=(0,_editableState.stripFunctions)(s.api.getState(),"",a,new WeakSet,0)}catch(e){t.push(`store "${s.name}": capture failed (${String(e)})`);continue}e[s.name]={state:n,fnPaths:a}}return{data:{stores:e},warnings:t}}async function restore(t,e){const s=t,a=(0,_sharedUi.createRestoreScope)(e),n=(0,_sharedUi.emptyRestoreResult)();if(!s?.stores)return(0,_sharedUi.failedRestore)("empty zustand snapshot");for(const[t,e]of Object.entries(s.stores)){if(!a.allows(t))continue;const s=_zustandStateStore.zustandStateStore.getStore(t);if(s)try{const a=e.state;graftFunctions(a,s.api.getState(),new WeakSet,0),s.api.setState(a,!0),s.api.getState()===a?n.applied+=1:(n.warnings.push(`store "${t}": setState did not take effect`),n.ok=!1)}catch(e){n.skipped.push({id:t,reason:String(e)}),n.ok=!1}else n.skipped.push({id:t,reason:"store not registered (renamed, or created after snapshot?)"})}const r=a.excludedWarning("store(s)");return r&&n.warnings.push(r),n}async function previewItems(t,e){const s=t?.stores??{},a=void 0===e,n={};if(a)for(const t of _zustandStateStore.zustandStateStore.getStores()){const e=[];try{n[t.name]={state:(0,_editableState.stripFunctions)(t.api.getState(),"",e,new WeakSet,0),fnPaths:e}}catch{}}else Object.assign(n,e?.stores??{});const r=[],o=[];let i=0;for(const t of(0,_sharedUi.unionKeys)(n,s)){const e=t in n;t in s?(i+=s[t].fnPaths?.length??0,!a||_zustandStateStore.zustandStateStore.getStore(t)?r.push((0,_sharedUi.classifyPreviewItem)({key:t,label:t,oldValue:n[t]?.state,newValue:s[t].state,inOld:e,inNew:!0},{tooLargeReason:"store too large to render a diff — restore still applies it"})):r.push({key:t,label:t,verdict:"wont-apply",reason:"store not registered (renamed, or created after snapshot?)"})):a?o.push(`store "${t}" not in snapshot — untouched`):r.push({key:t,label:t,verdict:"removed",oldValue:n[t].state})}return i>0&&o.push(`${i} store action function${1===i?"":"s"} preserved on restore (not diffed)`),{items:r,notes:o}}(0,_sharedUi.registerStateSnapshotProvider)({id:"zustand",label:"Zustand",status:status,capture:capture,restore:restore,previewItems:previewItems});
1
+ "use strict";
2
+
3
+ var _sharedUi = require("@buoy-gg/shared-ui");
4
+ var _zustandStateStore = require("./zustandStateStore");
5
+ var _editableState = require("./editableState");
6
+ /**
7
+ * Time Machine snapshot provider for Zustand.
8
+ *
9
+ * Registered into @buoy-gg/shared-ui's stateSnapshotRegistry at module init;
10
+ * consumed by @buoy-gg/time-machine. Neither package imports the other.
11
+ *
12
+ * The Zustand-specific hazard: stores conventionally keep their action
13
+ * functions IN state (`increment: () => ...`). Functions don't survive
14
+ * serialization, and a full `setState(snapshot, true)` replace would wipe
15
+ * them, breaking every `useStore(s => s.increment)` call site. So capture
16
+ * strips functions (recording where they were), and restore re-grafts the
17
+ * LIVE store's functions onto the snapshot before replacing.
18
+ */
19
+
20
+ // The function-stripping walk is shared with the store editor (editableState
21
+ // .ts) — capture and editing must agree on what a store's "data" is.
22
+ const MAX_DEPTH = _editableState.STRIP_MAX_DEPTH;
23
+ /**
24
+ * Copy every function-valued property from the LIVE state onto the restored
25
+ * snapshot (creating containers as needed), so store actions keep working
26
+ * after a full replace.
27
+ */
28
+ function graftFunctions(target, live, seen, depth) {
29
+ if (!(0, _editableState.isPlainObjectLike)(live) || !(0, _editableState.isPlainObjectLike)(target)) return;
30
+ if (depth >= MAX_DEPTH || seen.has(live)) return;
31
+ seen.add(live);
32
+ for (const [k, v] of Object.entries(live)) {
33
+ if (typeof v === "function") {
34
+ target[k] = v;
35
+ } else if ((0, _editableState.isPlainObjectLike)(v)) {
36
+ if (!(0, _editableState.isPlainObjectLike)(target[k])) {
37
+ // Branch missing from the snapshot — only recreate it if it holds
38
+ // functions somewhere; otherwise the snapshot's absence wins.
39
+ if (!containsFunction(v, new WeakSet(), depth + 1)) continue;
40
+ target[k] = {};
41
+ }
42
+ graftFunctions(target[k], v, seen, depth + 1);
43
+ }
44
+ }
45
+ }
46
+ function containsFunction(value, seen, depth) {
47
+ if (depth >= MAX_DEPTH || seen.has(value)) return false;
48
+ seen.add(value);
49
+ for (const v of Object.values(value)) {
50
+ if (typeof v === "function") return true;
51
+ if ((0, _editableState.isPlainObjectLike)(v) && containsFunction(v, seen, depth + 1)) return true;
52
+ }
53
+ return false;
54
+ }
55
+ function status() {
56
+ const count = _zustandStateStore.zustandStateStore.getStores().length;
57
+ if (count === 0) {
58
+ return {
59
+ canCapture: false,
60
+ canRestore: false,
61
+ detail: "No Zustand stores registered — pass `zustandStores` to FloatingDevTools or add the buoyDevTools middleware"
62
+ };
63
+ }
64
+ return {
65
+ canCapture: true,
66
+ canRestore: true
67
+ };
68
+ }
69
+ async function capture() {
70
+ const warnings = [];
71
+ const stores = {};
72
+ for (const info of _zustandStateStore.zustandStateStore.getStores()) {
73
+ const fnPaths = [];
74
+ let state;
75
+ try {
76
+ state = (0, _editableState.stripFunctions)(info.api.getState(), "", fnPaths, new WeakSet(), 0);
77
+ } catch (e) {
78
+ warnings.push(`store "${info.name}": capture failed (${String(e)})`);
79
+ continue;
80
+ }
81
+ stores[info.name] = {
82
+ state,
83
+ fnPaths
84
+ };
85
+ }
86
+ return {
87
+ data: {
88
+ stores
89
+ },
90
+ warnings
91
+ };
92
+ }
93
+ async function restore(data, options) {
94
+ const snapshot = data;
95
+ const scope = (0, _sharedUi.createRestoreScope)(options);
96
+ const result = (0, _sharedUi.emptyRestoreResult)();
97
+ if (!snapshot?.stores) {
98
+ return (0, _sharedUi.failedRestore)("empty zustand snapshot");
99
+ }
100
+ for (const [name, envelope] of Object.entries(snapshot.stores)) {
101
+ if (!scope.allows(name)) continue;
102
+ const info = _zustandStateStore.zustandStateStore.getStore(name);
103
+ if (!info) {
104
+ result.skipped.push({
105
+ id: name,
106
+ reason: "store not registered (renamed, or created after snapshot?)"
107
+ });
108
+ continue;
109
+ }
110
+ try {
111
+ const next = envelope.state;
112
+ graftFunctions(next, info.api.getState(), new WeakSet(), 0);
113
+ info.api.setState(next, true);
114
+ if (info.api.getState() === next) {
115
+ result.applied += 1;
116
+ } else {
117
+ result.warnings.push(`store "${name}": setState did not take effect`);
118
+ result.ok = false;
119
+ }
120
+ } catch (e) {
121
+ result.skipped.push({
122
+ id: name,
123
+ reason: String(e)
124
+ });
125
+ result.ok = false;
126
+ }
127
+ }
128
+ const excludedWarning = scope.excludedWarning("store(s)");
129
+ if (excludedWarning) result.warnings.push(excludedWarning);
130
+ return result;
131
+ }
132
+
133
+ /**
134
+ * Per-store restore preview. Both sides are function-stripped through the
135
+ * SAME depth-8 normalizer as capture — diffing snapshot-vs-RAW-live would
136
+ * report every stripped function and every beyond-depth subtree as removed.
137
+ */
138
+ async function previewItems(target, base) {
139
+ const targetStores = target?.stores ?? {};
140
+ const liveMode = base === undefined;
141
+ const baseStores = {};
142
+ if (liveMode) {
143
+ for (const info of _zustandStateStore.zustandStateStore.getStores()) {
144
+ const fnPaths = [];
145
+ try {
146
+ baseStores[info.name] = {
147
+ state: (0, _editableState.stripFunctions)(info.api.getState(), "", fnPaths, new WeakSet(), 0),
148
+ fnPaths
149
+ };
150
+ } catch {
151
+ // unreadable live store — treated as absent
152
+ }
153
+ }
154
+ } else {
155
+ Object.assign(baseStores, base?.stores ?? {});
156
+ }
157
+ const items = [];
158
+ const notes = [];
159
+ let preservedFns = 0;
160
+ for (const name of (0, _sharedUi.unionKeys)(baseStores, targetStores)) {
161
+ const inTarget = name in targetStores;
162
+ const inBase = name in baseStores;
163
+ if (!inTarget) {
164
+ // Restore never touches stores the snapshot doesn't know about.
165
+ if (liveMode) notes.push(`store "${name}" not in snapshot — untouched`);else items.push({
166
+ key: name,
167
+ label: name,
168
+ verdict: "removed",
169
+ oldValue: baseStores[name].state
170
+ });
171
+ continue;
172
+ }
173
+ preservedFns += targetStores[name].fnPaths?.length ?? 0;
174
+ if (liveMode && !_zustandStateStore.zustandStateStore.getStore(name)) {
175
+ items.push({
176
+ key: name,
177
+ label: name,
178
+ verdict: "wont-apply",
179
+ reason: "store not registered (renamed, or created after snapshot?)"
180
+ });
181
+ continue;
182
+ }
183
+ items.push((0, _sharedUi.classifyPreviewItem)({
184
+ key: name,
185
+ label: name,
186
+ oldValue: baseStores[name]?.state,
187
+ newValue: targetStores[name].state,
188
+ inOld: inBase,
189
+ inNew: true
190
+ }, {
191
+ tooLargeReason: "store too large to render a diff — restore still applies it"
192
+ }));
193
+ }
194
+ if (preservedFns > 0) {
195
+ notes.push(`${preservedFns} store action function${preservedFns === 1 ? "" : "s"} preserved on restore (not diffed)`);
196
+ }
197
+ return {
198
+ items,
199
+ notes
200
+ };
201
+ }
202
+ (0, _sharedUi.registerStateSnapshotProvider)({
203
+ id: "zustand",
204
+ label: "Zustand",
205
+ status,
206
+ capture,
207
+ restore,
208
+ previewItems
209
+ });
@@ -1 +1,82 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ZUSTAND_TEST_ID=void 0,exports.buildChangeRowTestIDs=buildChangeRowTestIDs,exports.changeRowTestID=changeRowTestID,exports.slugForTestID=slugForTestID,exports.storeRowTestID=storeRowTestID;const ZUSTAND_TEST_ID=exports.ZUSTAND_TEST_ID="buoy-zustand";function slugForTestID(e){return String(e??"").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unknown"}function changeRowTestID(e,t){return`${ZUSTAND_TEST_ID}-row-${slugForTestID(e)}-${t}`}function storeRowTestID(e){return`${ZUSTAND_TEST_ID}-store-${slugForTestID(e)}`}function buildChangeRowTestIDs(e){const t=new Map,s=new Map;for(const o of e){const e=(t.get(o.storeName)??0)+1;t.set(o.storeName,e),s.set(o.id,changeRowTestID(o.storeName,e))}return s}
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.ZUSTAND_TEST_ID = void 0;
7
+ exports.buildChangeRowTestIDs = buildChangeRowTestIDs;
8
+ exports.changeRowTestID = changeRowTestID;
9
+ exports.slugForTestID = slugForTestID;
10
+ exports.storeRowTestID = storeRowTestID;
11
+ /**
12
+ * Zustand tool test handles.
13
+ *
14
+ * Every tappable and every value the e2e suite reads back is addressed
15
+ * `buoy-zustand-<thing>`. Two rules in here need explaining.
16
+ *
17
+ * **A change row's handle carries an ORDINAL, and it counts per STORE.** The
18
+ * Events tool can address a row by what it is about, because an event is a
19
+ * thing that happened to a named subject — one key, one URL, one atom. A
20
+ * zustand state change is not: a store that a screen writes to fifty times
21
+ * produces fifty separate, equally real rows. Addressing them by store name
22
+ * alone would give fifty views one handle, and Detox fails a *matcher* that
23
+ * hits more than one element — so a suite would break on `toExist()`, before
24
+ * any assertion, with an error that says nothing about duplicate changes.
25
+ *
26
+ * So the handle is `buoy-zustand-row-<slug(storeName)>-<n>`, where n counts
27
+ * back from the newest row of that store (1 = newest). Deliberately relative:
28
+ * a state change has no identity a host could know in advance — `id` is a
29
+ * timestamp plus a per-process counter whose value depends on how many
30
+ * changes the app happened to make before the test looked. What a test CAN
31
+ * know is "the set I just fired is the newest change to that store". Read and
32
+ * tap in one beat: a row's ordinal moves the instant another change lands.
33
+ *
34
+ * **A store row's handle does NOT.** A store is registered once under a name
35
+ * that is unique by construction (`zustandStateStore.registerStore` returns
36
+ * early on a name it already holds), so `buoy-zustand-store-<slug(name)>` is
37
+ * as stable as the store itself — and unlike a change, it is a thing the host
38
+ * knows the name of before the app has booted.
39
+ *
40
+ * The suite mirrors these rules in CommonJS (`example-dev-build/utils/
41
+ * zustandBench.js`) and asserts the two agree — Detox runs in Node on the host
42
+ * and cannot import TypeScript.
43
+ */
44
+
45
+ /** Prefix every handle in this tool shares. */
46
+ const ZUSTAND_TEST_ID = exports.ZUSTAND_TEST_ID = "buoy-zustand";
47
+
48
+ /** `zbenchWatch` → `zbenchwatch`. Shared with every Buoy tool. */
49
+ function slugForTestID(value) {
50
+ return String(value ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "unknown";
51
+ }
52
+
53
+ /**
54
+ * Handle for one change row: `buoy-zustand-row-zbenchwatch-1`.
55
+ *
56
+ * `ordinal` is 1-based and counts back from the newest row of this store.
57
+ */
58
+ function changeRowTestID(storeName, ordinal) {
59
+ return `${ZUSTAND_TEST_ID}-row-${slugForTestID(storeName)}-${ordinal}`;
60
+ }
61
+
62
+ /** Handle for one store row in the browser: `buoy-zustand-store-zbenchwatch`. */
63
+ function storeRowTestID(storeName) {
64
+ return `${ZUSTAND_TEST_ID}-store-${slugForTestID(storeName)}`;
65
+ }
66
+
67
+ /**
68
+ * Row handle per change id for one rendered list, newest-first.
69
+ *
70
+ * Built once for the whole list rather than per row, because an ordinal is a
71
+ * fact about the list and a row cannot compute its own.
72
+ */
73
+ function buildChangeRowTestIDs(changes) {
74
+ const seen = new Map();
75
+ const handles = new Map();
76
+ for (const change of changes) {
77
+ const ordinal = (seen.get(change.storeName) ?? 0) + 1;
78
+ seen.set(change.storeName, ordinal);
79
+ handles.set(change.id, changeRowTestID(change.storeName, ordinal));
80
+ }
81
+ return handles;
82
+ }