@buoy-gg/jotai 7.0.41 → 7.0.43

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 (54) hide show
  1. package/README.md +11 -80
  2. package/lib/commonjs/index.js +1 -112
  3. package/lib/commonjs/jotai/components/JotaiAtomBrowser.js +1 -199
  4. package/lib/commonjs/jotai/components/JotaiAtomChangeItem.js +1 -95
  5. package/lib/commonjs/jotai/components/JotaiAtomDetailContent.js +1 -715
  6. package/lib/commonjs/jotai/components/JotaiAtomValueDetailContent.js +1 -444
  7. package/lib/commonjs/jotai/components/JotaiEventFilterView.js +1 -43
  8. package/lib/commonjs/jotai/components/JotaiIcon.js +1 -18
  9. package/lib/commonjs/jotai/components/JotaiModal.js +1 -743
  10. package/lib/commonjs/jotai/hooks/useJotaiAtomChanges.js +1 -69
  11. package/lib/commonjs/jotai/sync/jotaiSyncAdapter.js +1 -306
  12. package/lib/commonjs/jotai/types/index.js +1 -1
  13. package/lib/commonjs/jotai/utils/jotaiStateStore.js +1 -321
  14. package/lib/commonjs/jotai/utils/snapshotProvider.js +1 -215
  15. package/lib/commonjs/jotai/utils/testIDs.js +1 -90
  16. package/lib/commonjs/jotai/utils/watchAtoms.js +1 -143
  17. package/lib/commonjs/jotai/utils/writeSemantics.js +1 -47
  18. package/lib/commonjs/preset.js +1 -90
  19. package/lib/module/index.js +1 -85
  20. package/lib/module/jotai/components/JotaiAtomBrowser.js +1 -195
  21. package/lib/module/jotai/components/JotaiAtomChangeItem.js +1 -91
  22. package/lib/module/jotai/components/JotaiAtomDetailContent.js +1 -710
  23. package/lib/module/jotai/components/JotaiAtomValueDetailContent.js +1 -442
  24. package/lib/module/jotai/components/JotaiEventFilterView.js +1 -39
  25. package/lib/module/jotai/components/JotaiIcon.js +1 -11
  26. package/lib/module/jotai/components/JotaiModal.js +1 -738
  27. package/lib/module/jotai/hooks/useJotaiAtomChanges.js +1 -65
  28. package/lib/module/jotai/sync/jotaiSyncAdapter.js +1 -303
  29. package/lib/module/jotai/types/index.js +1 -1
  30. package/lib/module/jotai/utils/jotaiStateStore.js +1 -317
  31. package/lib/module/jotai/utils/snapshotProvider.js +1 -214
  32. package/lib/module/jotai/utils/testIDs.js +1 -82
  33. package/lib/module/jotai/utils/watchAtoms.js +1 -138
  34. package/lib/module/jotai/utils/writeSemantics.js +1 -43
  35. package/lib/module/preset.js +1 -87
  36. package/lib/web/index.mjs +2 -3
  37. package/package.json +4 -7
  38. package/lib/typescript/index.d.ts.map +0 -1
  39. package/lib/typescript/jotai/components/JotaiAtomBrowser.d.ts.map +0 -1
  40. package/lib/typescript/jotai/components/JotaiAtomChangeItem.d.ts.map +0 -1
  41. package/lib/typescript/jotai/components/JotaiAtomDetailContent.d.ts.map +0 -1
  42. package/lib/typescript/jotai/components/JotaiAtomValueDetailContent.d.ts.map +0 -1
  43. package/lib/typescript/jotai/components/JotaiEventFilterView.d.ts.map +0 -1
  44. package/lib/typescript/jotai/components/JotaiIcon.d.ts.map +0 -1
  45. package/lib/typescript/jotai/components/JotaiModal.d.ts.map +0 -1
  46. package/lib/typescript/jotai/hooks/useJotaiAtomChanges.d.ts.map +0 -1
  47. package/lib/typescript/jotai/sync/jotaiSyncAdapter.d.ts.map +0 -1
  48. package/lib/typescript/jotai/types/index.d.ts.map +0 -1
  49. package/lib/typescript/jotai/utils/jotaiStateStore.d.ts.map +0 -1
  50. package/lib/typescript/jotai/utils/snapshotProvider.d.ts.map +0 -1
  51. package/lib/typescript/jotai/utils/testIDs.d.ts.map +0 -1
  52. package/lib/typescript/jotai/utils/watchAtoms.d.ts.map +0 -1
  53. package/lib/typescript/jotai/utils/writeSemantics.d.ts.map +0 -1
  54. package/lib/typescript/preset.d.ts.map +0 -1
@@ -1,317 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * Jotai state store — captures and stores Jotai atom changes
5
- *
6
- * Mirrors the architecture of zustandStateStore.ts from @buoy-gg/zustand
7
- */
8
-
9
- import { ChangeLog, entityColor, formatValuePreview as sharedFormatValuePreview, shallowDiffSummary } from "@buoy-gg/shared-ui";
10
- // ============================================
11
- // Atom Color Palette
12
- // ============================================
13
-
14
- const ATOM_COLORS = {
15
- count: "#10B981",
16
- // emerald
17
- auth: "#8B5CF6",
18
- // purple
19
- user: "#3B82F6",
20
- // blue
21
- cart: "#EC4899",
22
- // pink
23
- app: "#6366F1",
24
- // indigo
25
- ui: "#F59E0B",
26
- // amber
27
- settings: "#14B8A6",
28
- // teal
29
- theme: "#06B6D4",
30
- // cyan
31
- nav: "#F97316",
32
- // orange
33
- form: "#EF4444",
34
- // red
35
- modal: "#A855F7",
36
- // violet
37
- filter: "#84CC16" // lime
38
- };
39
- function getAtomColor(label) {
40
- return entityColor(label, ATOM_COLORS);
41
- }
42
-
43
- // ============================================
44
- // Helper Functions
45
- // ============================================
46
-
47
- function formatValuePreview(value, maxLength = 40) {
48
- return sharedFormatValuePreview(value, {
49
- maxLength,
50
- functionLabel: "(fn)"
51
- });
52
- }
53
- const getValueDiffSummary = shallowDiffSummary;
54
-
55
- // ============================================
56
- // Jotai State Store
57
- // ============================================
58
-
59
- class JotaiStateStore extends ChangeLog {
60
- atoms = new Map();
61
- atomListeners = new Set();
62
-
63
- // ---- Change Tracking ----
64
-
65
- addAtomChange(params) {
66
- if (!this.canRecord()) return;
67
- const {
68
- atomLabel,
69
- prevValue,
70
- nextValue,
71
- category = "write"
72
- } = params;
73
- const hasValueChange = prevValue !== nextValue;
74
- const {
75
- summary,
76
- changedKeys,
77
- changedCount
78
- } = getValueDiffSummary(prevValue, nextValue);
79
- const valuePreview = formatValuePreview(nextValue);
80
- const change = {
81
- id: this.nextId(),
82
- atomLabel,
83
- timestamp: Date.now(),
84
- prevValue,
85
- nextValue,
86
- hasValueChange,
87
- category,
88
- changedKeys,
89
- changedKeysCount: changedCount,
90
- diffSummary: summary,
91
- valuePreview,
92
- isSlowUpdate: false
93
- };
94
- this.push(change);
95
- const atomInfo = this.atoms.get(atomLabel);
96
- if (atomInfo) {
97
- atomInfo.changeCount++;
98
- }
99
- this.notifyListeners();
100
- this.notifyAtomListeners();
101
- }
102
- getAtomChanges() {
103
- return this.getAll();
104
- }
105
- getAtomChangeById(id) {
106
- return this.getById(id);
107
- }
108
- clearAtomChanges() {
109
- for (const atom of this.atoms.values()) {
110
- atom.changeCount = 0;
111
- }
112
- this.clear();
113
- this.notifyAtomListeners();
114
- }
115
-
116
- // ---- Remote Mirror Mode ----
117
-
118
- /**
119
- * Serializable snapshot of the tracked atoms (registry metadata plus each
120
- * atom's current value). Used by the sync adapter on the device side —
121
- * the live `getValue` handles can't go over the wire.
122
- */
123
- getAtomSnapshots() {
124
- return Array.from(this.atoms.values()).map(atom => {
125
- let currentValue;
126
- try {
127
- currentValue = atom.getValue();
128
- } catch {
129
- currentValue = undefined;
130
- }
131
- return {
132
- label: atom.label,
133
- changeCount: atom.changeCount,
134
- color: atom.color,
135
- currentValue,
136
- writeKind: atom.writeKind
137
- };
138
- });
139
- }
140
-
141
- /**
142
- * Replace the entire mirror contents from a synced device snapshot. The
143
- * rebuilt registry entries get stub `getValue` handles serving the
144
- * snapshotted value. Respects the enabled flag so the UI's pause button
145
- * freezes the mirror.
146
- */
147
- replaceFromSnapshot(changes, atomSnapshots) {
148
- if (!this.getEnabled()) return;
149
-
150
- // Preserve values a remote mirror already hydrated — wire-form snapshots
151
- // always carry markers, and a later snapshot must not blank the detail pane.
152
- const hydrated = new Map();
153
- for (const existing of this.items) {
154
- const prev = existing.prevValue;
155
- if (!prev || prev.__buoyValueOnDevice !== true) {
156
- hydrated.set(existing.id, existing);
157
- }
158
- }
159
- const merged = changes.slice(0, this.max).map(change => {
160
- const kept = hydrated.get(change.id);
161
- const incoming = change.prevValue;
162
- return kept && incoming?.__buoyValueOnDevice === true ? {
163
- ...change,
164
- prevValue: kept.prevValue,
165
- nextValue: kept.nextValue
166
- } : change;
167
- });
168
-
169
- // Preserve currentValue a remote mirror already hydrated — oversized
170
- // atoms arrive as VALUE_ON_DEVICE markers on every snapshot.
171
- const hydratedAtoms = new Map();
172
- for (const [label, atom] of this.atoms) {
173
- try {
174
- const value = atom.getValue();
175
- const marker = value;
176
- if (!marker || marker.__buoyValueOnDevice !== true) {
177
- hydratedAtoms.set(label, value);
178
- }
179
- } catch {
180
- // ignore
181
- }
182
- }
183
- this.atoms = new Map(atomSnapshots.map(snapshot => {
184
- const incoming = snapshot.currentValue;
185
- const kept = hydratedAtoms.get(snapshot.label);
186
- const currentValue = kept !== undefined && incoming?.__buoyValueOnDevice === true ? kept : snapshot.currentValue;
187
- return [snapshot.label, {
188
- label: snapshot.label,
189
- changeCount: snapshot.changeCount,
190
- color: snapshot.color,
191
- getValue: () => currentValue,
192
- // No setValue: the mirror has no wire back to the device from
193
- // this entry (yet) — the detail screen renders read-only there.
194
- writeKind: snapshot.writeKind
195
- }];
196
- }));
197
- this.setItems(merged);
198
- this.notifyAtomListeners();
199
- }
200
-
201
- /**
202
- * Hydrate one mirrored change with real values fetched on demand from the
203
- * device via the adapter's `getChangeDetail`. Remote-mirror (desktop) only.
204
- */
205
- hydrateChange(id, patch) {
206
- this.hydrateById(id, patch);
207
- }
208
-
209
- /**
210
- * Hydrate one mirrored atom's currentValue fetched on demand from the
211
- * device via the adapter's `getAtomValue`. Remote-mirror (desktop) only.
212
- */
213
- hydrateAtomValue(label, currentValue) {
214
- const atom = this.atoms.get(label);
215
- if (!atom) return;
216
- this.atoms.set(label, {
217
- ...atom,
218
- getValue: () => currentValue
219
- });
220
- this.notifyAtomListeners();
221
- }
222
-
223
- // ---- Atom Registry ----
224
-
225
- registerAtom(label, getValue, setValue, writeKind) {
226
- if (this.atoms.has(label)) return;
227
- const atomInfo = {
228
- label,
229
- changeCount: 0,
230
- color: getAtomColor(label),
231
- getValue,
232
- setValue,
233
- writeKind
234
- };
235
- this.atoms.set(label, atomInfo);
236
- this.notifyAtomListeners();
237
- }
238
-
239
- /**
240
- * Write a value to a registered atom (remote write from the dashboard/MCP).
241
- * Throws if the atom is unknown or read-only (derived atoms have no setter).
242
- */
243
- setAtomValue(label, value) {
244
- const atom = this.atoms.get(label);
245
- if (!atom) {
246
- throw new Error(`Atom "${label}" is not registered`);
247
- }
248
- if (!atom.setValue) {
249
- throw new Error(`Atom "${label}" is read-only (derived) — cannot set`);
250
- }
251
- atom.setValue(value);
252
- }
253
- unregisterAtom(label) {
254
- this.atoms.delete(label);
255
- this.notifyAtomListeners();
256
- }
257
- getAtoms() {
258
- return Array.from(this.atoms.values());
259
- }
260
- getAtom(label) {
261
- return this.atoms.get(label);
262
- }
263
- getAtomColor(label) {
264
- return this.atoms.get(label)?.color ?? getAtomColor(label);
265
- }
266
-
267
- // ---- Filtering ----
268
-
269
- filterAtomChanges(filter) {
270
- let filtered = this.getAll();
271
- if (filter.searchText) {
272
- const search = filter.searchText.toLowerCase();
273
- filtered = filtered.filter(c => c.atomLabel.toLowerCase().includes(search) || c.valuePreview.toLowerCase().includes(search) || c.changedKeys.some(k => k.toLowerCase().includes(search)));
274
- }
275
- if (filter.atomLabels && filter.atomLabels.length > 0) {
276
- filtered = filtered.filter(c => filter.atomLabels.includes(c.atomLabel));
277
- }
278
- if (filter.onlyWithChanges) {
279
- filtered = filtered.filter(c => c.hasValueChange);
280
- }
281
- return filtered;
282
- }
283
-
284
- // ---- Stats ----
285
-
286
- getStats() {
287
- const total = this.items.length;
288
- const withChanges = this.items.filter(c => c.hasValueChange).length;
289
- return {
290
- totalChanges: total,
291
- changesWithValueChange: withChanges,
292
- changesWithoutValueChange: total - withChanges,
293
- atomCount: this.atoms.size
294
- };
295
- }
296
- getUniqueAtomLabels() {
297
- return Array.from(this.atoms.keys()).sort();
298
- }
299
-
300
- // ---- Enable / Disable ----
301
-
302
- setMaxChanges(max) {
303
- this.setMax(max);
304
- }
305
-
306
- // ---- Subscriptions ----
307
-
308
- subscribeToAtoms(listener) {
309
- this.atomListeners.add(listener);
310
- return () => this.atomListeners.delete(listener);
311
- }
312
- notifyAtomListeners() {
313
- const atoms = this.getAtoms();
314
- this.atomListeners.forEach(listener => listener(atoms));
315
- }
316
- }
317
- export const jotaiStateStore = new JotaiStateStore();
1
+ "use strict";import{ChangeLog as f,entityColor as d,formatValuePreview as C,shallowDiffSummary as y}from"@buoy-gg/shared-ui";const A={count:"#10B981",auth:"#8B5CF6",user:"#3B82F6",cart:"#EC4899",app:"#6366F1",ui:"#F59E0B",settings:"#14B8A6",theme:"#06B6D4",nav:"#F97316",form:"#EF4444",modal:"#A855F7",filter:"#84CC16"};function l(h){return d(h,A)}function V(h,t=40){return C(h,{maxLength:t,functionLabel:"(fn)"})}const w=y;class L extends f{atoms=new Map;atomListeners=new Set;addAtomChange(t){if(!this.canRecord())return;const{atomLabel:e,prevValue:s,nextValue:a,category:i="write"}=t;const o=s!==a;const{summary:n,changedKeys:r,changedCount:u}=w(s,a);const c=V(a);const g={id:this.nextId(),atomLabel:e,timestamp:Date.now(),prevValue:s,nextValue:a,hasValueChange:o,category:i,changedKeys:r,changedKeysCount:u,diffSummary:n,valuePreview:c,isSlowUpdate:false};this.push(g);const m=this.atoms.get(e);if(m){m.changeCount++}this.notifyListeners();this.notifyAtomListeners()}getAtomChanges(){return this.getAll()}getAtomChangeById(t){return this.getById(t)}clearAtomChanges(){for(const t of this.atoms.values()){t.changeCount=0}this.clear();this.notifyAtomListeners()}getAtomSnapshots(){return Array.from(this.atoms.values()).map(t=>{let e;try{e=t.getValue()}catch{e=void 0}return{label:t.label,changeCount:t.changeCount,color:t.color,currentValue:e,writeKind:t.writeKind}})}replaceFromSnapshot(t,e){if(!this.getEnabled())return;const s=new Map;for(const o of this.items){const n=o.prevValue;if(!n||n.__buoyValueOnDevice!==true){s.set(o.id,o)}}const a=t.slice(0,this.max).map(o=>{const n=s.get(o.id);const r=o.prevValue;return n&&r?.__buoyValueOnDevice===true?{...o,prevValue:n.prevValue,nextValue:n.nextValue}:o});const i=new Map;for(const[o,n]of this.atoms){try{const r=n.getValue();const u=r;if(!u||u.__buoyValueOnDevice!==true){i.set(o,r)}}catch{}}this.atoms=new Map(e.map(o=>{const n=o.currentValue;const r=i.get(o.label);const u=r!==void 0&&n?.__buoyValueOnDevice===true?r:o.currentValue;return[o.label,{label:o.label,changeCount:o.changeCount,color:o.color,getValue:()=>u,writeKind:o.writeKind}]}));this.setItems(a);this.notifyAtomListeners()}hydrateChange(t,e){this.hydrateById(t,e)}hydrateAtomValue(t,e){const s=this.atoms.get(t);if(!s)return;this.atoms.set(t,{...s,getValue:()=>e});this.notifyAtomListeners()}registerAtom(t,e,s,a){if(this.atoms.has(t))return;const i={label:t,changeCount:0,color:l(t),getValue:e,setValue:s,writeKind:a};this.atoms.set(t,i);this.notifyAtomListeners()}setAtomValue(t,e){const s=this.atoms.get(t);if(!s){throw new Error(`Atom "${t}" is not registered`)}if(!s.setValue){throw new Error(`Atom "${t}" is read-only (derived) \u2014 cannot set`)}s.setValue(e)}unregisterAtom(t){this.atoms.delete(t);this.notifyAtomListeners()}getAtoms(){return Array.from(this.atoms.values())}getAtom(t){return this.atoms.get(t)}getAtomColor(t){return this.atoms.get(t)?.color??l(t)}filterAtomChanges(t){let e=this.getAll();if(t.searchText){const s=t.searchText.toLowerCase();e=e.filter(a=>a.atomLabel.toLowerCase().includes(s)||a.valuePreview.toLowerCase().includes(s)||a.changedKeys.some(i=>i.toLowerCase().includes(s)))}if(t.atomLabels&&t.atomLabels.length>0){e=e.filter(s=>t.atomLabels.includes(s.atomLabel))}if(t.onlyWithChanges){e=e.filter(s=>s.hasValueChange)}return e}getStats(){const t=this.items.length;const e=this.items.filter(s=>s.hasValueChange).length;return{totalChanges:t,changesWithValueChange:e,changesWithoutValueChange:t-e,atomCount:this.atoms.size}}getUniqueAtomLabels(){return Array.from(this.atoms.keys()).sort()}setMaxChanges(t){this.setMax(t)}subscribeToAtoms(t){this.atomListeners.add(t);return()=>this.atomListeners.delete(t)}notifyAtomListeners(){const t=this.getAtoms();this.atomListeners.forEach(e=>e(t))}}export const jotaiStateStore=new L;
@@ -1,214 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * Time Machine snapshot provider for Jotai.
5
- *
6
- * Registered into @buoy-gg/shared-ui's stateSnapshotRegistry at module init;
7
- * consumed by @buoy-gg/time-machine.
8
- *
9
- * Coverage is opt-in by nature: only atoms the app passed to `watchAtoms` /
10
- * `watchDefaultStoreAtoms` are visible. Derived (read-only) atoms are captured
11
- * for inspection but never restored — they recompute from their sources.
12
- * Caveat (documented): atoms with custom write functions (`atomWithReducer`)
13
- * receive ACTIONS through `store.set`, so restoring a raw value can misbehave;
14
- * nothing in the registry distinguishes them from plain atoms.
15
- */
16
- import { createRestoreScope, emptyRestoreResult, failedRestore, registerStateSnapshotProvider, unionKeys } from "@buoy-gg/shared-ui";
17
- import { boundedDeepEqual } from "@buoy-gg/shared-ui/dataViewer";
18
- import { jotaiStateStore } from "./jotaiStateStore";
19
- function status() {
20
- const atoms = jotaiStateStore.getAtoms();
21
- if (atoms.length === 0) {
22
- return {
23
- canCapture: false,
24
- canRestore: false,
25
- detail: "No Jotai atoms registered — pass your atoms to watchAtoms() / watchDefaultStoreAtoms()"
26
- };
27
- }
28
- const writable = atoms.filter(a => a.setValue).length;
29
- return {
30
- canCapture: true,
31
- canRestore: writable > 0,
32
- detail: writable < atoms.length ? `${atoms.length - writable} derived atom(s) recompute automatically and are not restored` : undefined
33
- };
34
- }
35
- async function capture() {
36
- const warnings = [];
37
- const atoms = {};
38
- let derived = 0;
39
- for (const info of jotaiStateStore.getAtoms()) {
40
- try {
41
- const value = info.getValue();
42
- if (typeof value === "function") {
43
- warnings.push(`atom "${info.label}": function value not capturable`);
44
- continue;
45
- }
46
- if (!info.setValue) derived += 1;
47
- atoms[info.label] = {
48
- value,
49
- writable: Boolean(info.setValue)
50
- };
51
- } catch (e) {
52
- warnings.push(`atom "${info.label}": capture failed (${String(e)})`);
53
- }
54
- }
55
- if (derived > 0) {
56
- warnings.push(`${derived} derived atom(s) captured for inspection only (recompute on restore)`);
57
- }
58
- return {
59
- data: {
60
- atoms
61
- },
62
- warnings
63
- };
64
- }
65
- async function restore(data, options) {
66
- const snapshot = data;
67
- const scope = createRestoreScope(options);
68
- const result = emptyRestoreResult();
69
- if (!snapshot?.atoms) {
70
- return failedRestore("empty jotai snapshot");
71
- }
72
- let derived = 0;
73
- for (const [label, entry] of Object.entries(snapshot.atoms)) {
74
- if (!scope.allows(label)) continue;
75
- if (!entry.writable) {
76
- derived += 1;
77
- continue;
78
- }
79
- const info = jotaiStateStore.getAtom(label);
80
- if (!info) {
81
- result.skipped.push({
82
- id: label,
83
- reason: "atom not registered (renamed, or unwatched since snapshot?)"
84
- });
85
- continue;
86
- }
87
- if (!info.setValue) {
88
- result.skipped.push({
89
- id: label,
90
- reason: "atom is now read-only (derived)"
91
- });
92
- continue;
93
- }
94
- try {
95
- info.setValue(entry.value);
96
- result.applied += 1;
97
- } catch (e) {
98
- result.skipped.push({
99
- id: label,
100
- reason: String(e)
101
- });
102
- result.ok = false;
103
- }
104
- }
105
- if (derived > 0) {
106
- result.warnings.push(`${derived} derived atom(s) left to recompute`);
107
- }
108
- const excludedWarning = scope.excludedWarning("atom(s)");
109
- if (excludedWarning) result.warnings.push(excludedWarning);
110
- return result;
111
- }
112
-
113
- /** Per-atom restore preview. `base` omitted = compare against live atoms. */
114
- async function previewItems(target, base) {
115
- const targetAtoms = target?.atoms ?? {};
116
- const liveMode = base === undefined;
117
- const baseAtoms = {};
118
- if (liveMode) {
119
- for (const info of jotaiStateStore.getAtoms()) {
120
- try {
121
- baseAtoms[info.label] = {
122
- value: info.getValue(),
123
- writable: Boolean(info.setValue)
124
- };
125
- } catch {
126
- // unreadable atom — treated as absent
127
- }
128
- }
129
- } else {
130
- Object.assign(baseAtoms, base?.atoms ?? {});
131
- }
132
- const items = [];
133
- const notes = [];
134
- for (const label of unionKeys(baseAtoms, targetAtoms)) {
135
- const inTarget = label in targetAtoms;
136
- if (!inTarget) {
137
- if (liveMode) notes.push(`atom "${label}" not in snapshot — untouched`);else items.push({
138
- key: label,
139
- label,
140
- verdict: "removed",
141
- oldValue: baseAtoms[label]?.value
142
- });
143
- continue;
144
- }
145
- const entry = targetAtoms[label];
146
- const equal = label in baseAtoms && boundedDeepEqual(baseAtoms[label].value, entry.value);
147
- if (!entry.writable) {
148
- items.push({
149
- key: label,
150
- label,
151
- verdict: equal ? "unchanged" : "recomputes",
152
- reason: equal ? undefined : "derived atom — recomputes from its sources",
153
- oldValue: baseAtoms[label]?.value,
154
- newValue: entry.value
155
- });
156
- continue;
157
- }
158
- if (liveMode) {
159
- const info = jotaiStateStore.getAtom(label);
160
- if (!info) {
161
- items.push({
162
- key: label,
163
- label,
164
- verdict: "wont-apply",
165
- reason: "atom not registered (renamed, or unwatched since snapshot?)"
166
- });
167
- continue;
168
- }
169
- if (!info.setValue) {
170
- items.push({
171
- key: label,
172
- label,
173
- verdict: "wont-apply",
174
- reason: "atom is now read-only (derived)"
175
- });
176
- continue;
177
- }
178
- }
179
- if (!(label in baseAtoms)) {
180
- items.push({
181
- key: label,
182
- label,
183
- verdict: "added",
184
- newValue: entry.value
185
- });
186
- } else if (equal) {
187
- items.push({
188
- key: label,
189
- label,
190
- verdict: "unchanged"
191
- });
192
- } else {
193
- items.push({
194
- key: label,
195
- label,
196
- verdict: "changed",
197
- oldValue: baseAtoms[label].value,
198
- newValue: entry.value
199
- });
200
- }
201
- }
202
- return {
203
- items,
204
- notes
205
- };
206
- }
207
- registerStateSnapshotProvider({
208
- id: "jotai",
209
- label: "Jotai",
210
- status,
211
- capture,
212
- restore,
213
- previewItems
214
- });
1
+ "use strict";import{createRestoreScope as f,emptyRestoreResult as m,failedRestore as h,registerStateSnapshotProvider as v,unionKeys as g}from"@buoy-gg/shared-ui";import{boundedDeepEqual as y}from"@buoy-gg/shared-ui/dataViewer";import{jotaiStateStore as c}from"./jotaiStateStore";function w(){const o=c.getAtoms();if(o.length===0){return{canCapture:false,canRestore:false,detail:"No Jotai atoms registered \u2014 pass your atoms to watchAtoms() / watchDefaultStoreAtoms()"}}const i=o.filter(a=>a.setValue).length;return{canCapture:true,canRestore:i>0,detail:i<o.length?`${o.length-i} derived atom(s) recompute automatically and are not restored`:void 0}}async function b(){const o=[];const i={};let a=0;for(const n of c.getAtoms()){try{const t=n.getValue();if(typeof t==="function"){o.push(`atom "${n.label}": function value not capturable`);continue}if(!n.setValue)a+=1;i[n.label]={value:t,writable:Boolean(n.setValue)}}catch(t){o.push(`atom "${n.label}": capture failed (${String(t)})`)}}if(a>0){o.push(`${a} derived atom(s) captured for inspection only (recompute on restore)`)}return{data:{atoms:i},warnings:o}}async function V(o,i){const a=o;const n=f(i);const t=m();if(!a?.atoms){return h("empty jotai snapshot")}let s=0;for(const[e,d]of Object.entries(a.atoms)){if(!n.allows(e))continue;if(!d.writable){s+=1;continue}const r=c.getAtom(e);if(!r){t.skipped.push({id:e,reason:"atom not registered (renamed, or unwatched since snapshot?)"});continue}if(!r.setValue){t.skipped.push({id:e,reason:"atom is now read-only (derived)"});continue}try{r.setValue(d.value);t.applied+=1}catch(l){t.skipped.push({id:e,reason:String(l)});t.ok=false}}if(s>0){t.warnings.push(`${s} derived atom(s) left to recompute`)}const u=n.excludedWarning("atom(s)");if(u)t.warnings.push(u);return t}async function k(o,i){const a=o?.atoms??{};const n=i===void 0;const t={};if(n){for(const e of c.getAtoms()){try{t[e.label]={value:e.getValue(),writable:Boolean(e.setValue)}}catch{}}}else{Object.assign(t,i?.atoms??{})}const s=[];const u=[];for(const e of g(t,a)){const d=e in a;if(!d){if(n)u.push(`atom "${e}" not in snapshot \u2014 untouched`);else s.push({key:e,label:e,verdict:"removed",oldValue:t[e]?.value});continue}const r=a[e];const l=e in t&&y(t[e].value,r.value);if(!r.writable){s.push({key:e,label:e,verdict:l?"unchanged":"recomputes",reason:l?void 0:"derived atom \u2014 recomputes from its sources",oldValue:t[e]?.value,newValue:r.value});continue}if(n){const p=c.getAtom(e);if(!p){s.push({key:e,label:e,verdict:"wont-apply",reason:"atom not registered (renamed, or unwatched since snapshot?)"});continue}if(!p.setValue){s.push({key:e,label:e,verdict:"wont-apply",reason:"atom is now read-only (derived)"});continue}}if(!(e in t)){s.push({key:e,label:e,verdict:"added",newValue:r.value})}else if(l){s.push({key:e,label:e,verdict:"unchanged"})}else{s.push({key:e,label:e,verdict:"changed",oldValue:t[e].value,newValue:r.value})}}return{items:s,notes:u}}v({id:"jotai",label:"Jotai",status:w,capture:b,restore:V,previewItems:k});
@@ -1,82 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * Jotai tool test handles.
5
- *
6
- * Every tappable and every value the e2e suite reads back is addressed
7
- * `buoy-jotai-<thing>`. Two rules need explaining, and the second is the one
8
- * this tool does not share with any other.
9
- *
10
- * **A change row's handle carries an ORDINAL, and it counts per ATOM.** Same
11
- * reason as the Zustand tool: an atom a screen writes to fifty times produces
12
- * fifty separate, equally real rows, and addressing them by label alone would
13
- * give fifty views one handle — Detox fails a *matcher* that hits more than
14
- * one element, so the suite would break on `toExist()` before any assertion,
15
- * with an error that says nothing about duplicate changes. The handle is
16
- * `buoy-jotai-row-<slug(atomLabel)>-<n>`, n counting back from the newest row
17
- * for that atom (1 = newest). Deliberately relative: an atom change has no
18
- * identity a host could know in advance, and what a test CAN know is "the
19
- * write I just fired is the newest change to that atom". Read and tap in one
20
- * beat — a row's ordinal moves the instant another change lands.
21
- *
22
- * **The ordinal is per ATOM, and one write can move several atoms.** This is
23
- * where jotai differs from every store-shaped tool. Atoms form a dependency
24
- * graph, and this tool watches NODES: a single `set` on a primitive atom
25
- * notifies every mounted derived atom whose value actually moved, so one
26
- * press can add a row under three different labels — and add nothing at all
27
- * under a fourth whose recomputed value happened to be unchanged. Counting
28
- * ordinals across the whole list would make a row's handle depend on how many
29
- * *other* atoms the write happened to touch, which is exactly the thing a
30
- * test of a graph must not assume.
31
- *
32
- * **An atom row's handle does NOT carry an ordinal.** An atom is registered
33
- * once under a label that is unique by construction
34
- * (`jotaiStateStore.registerAtom` returns early on a label it already holds,
35
- * and `watchAtoms` marks the atom with a per-label symbol), so
36
- * `buoy-jotai-atom-<slug(label)>` is as stable as the registration itself —
37
- * and unlike a change, it is a thing the host knows the name of before the
38
- * app has booted.
39
- *
40
- * The suite mirrors these rules in CommonJS (`example-dev-build/utils/
41
- * jotaiBench.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
- export const JOTAI_TEST_ID = "buoy-jotai";
47
-
48
- /** `jbenchCount` → `jbenchcount`. Shared with every Buoy tool. */
49
- export 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-jotai-row-jbenchcount-1`.
55
- *
56
- * `ordinal` is 1-based and counts back from the newest row for this atom.
57
- */
58
- export function changeRowTestID(atomLabel, ordinal) {
59
- return `${JOTAI_TEST_ID}-row-${slugForTestID(atomLabel)}-${ordinal}`;
60
- }
61
-
62
- /** Handle for one atom row in the browser: `buoy-jotai-atom-jbenchcount`. */
63
- export function atomRowTestID(atomLabel) {
64
- return `${JOTAI_TEST_ID}-atom-${slugForTestID(atomLabel)}`;
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
- export 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.atomLabel) ?? 0) + 1;
78
- seen.set(change.atomLabel, ordinal);
79
- handles.set(change.id, changeRowTestID(change.atomLabel, ordinal));
80
- }
81
- return handles;
82
- }
1
+ "use strict";export const JOTAI_TEST_ID="buoy-jotai";export function slugForTestID(t){return String(t??"").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unknown"}export function changeRowTestID(t,e){return`${JOTAI_TEST_ID}-row-${slugForTestID(t)}-${e}`}export function atomRowTestID(t){return`${JOTAI_TEST_ID}-atom-${slugForTestID(t)}`}export function buildChangeRowTestIDs(t){const e=new Map;const n=new Map;for(const o of t){const r=(e.get(o.atomLabel)??0)+1;e.set(o.atomLabel,r);n.set(o.id,changeRowTestID(o.atomLabel,r))}return n}