@buoy-gg/jotai 7.0.35 → 7.0.38

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 (52) hide show
  1. package/README.md +32 -5
  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 -85
  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 -67
  11. package/lib/commonjs/jotai/sync/jotaiSyncAdapter.js +1 -149
  12. package/lib/commonjs/jotai/utils/jotaiStateStore.js +1 -321
  13. package/lib/commonjs/jotai/utils/snapshotProvider.js +1 -215
  14. package/lib/commonjs/jotai/utils/testIDs.js +1 -90
  15. package/lib/commonjs/jotai/utils/watchAtoms.js +1 -162
  16. package/lib/commonjs/jotai/utils/writeSemantics.js +1 -47
  17. package/lib/commonjs/preset.js +1 -90
  18. package/lib/module/index.js +1 -85
  19. package/lib/module/jotai/components/JotaiAtomBrowser.js +1 -195
  20. package/lib/module/jotai/components/JotaiAtomChangeItem.js +1 -81
  21. package/lib/module/jotai/components/JotaiAtomDetailContent.js +1 -710
  22. package/lib/module/jotai/components/JotaiAtomValueDetailContent.js +1 -442
  23. package/lib/module/jotai/components/JotaiEventFilterView.js +1 -39
  24. package/lib/module/jotai/components/JotaiIcon.js +1 -11
  25. package/lib/module/jotai/components/JotaiModal.js +1 -738
  26. package/lib/module/jotai/hooks/useJotaiAtomChanges.js +1 -63
  27. package/lib/module/jotai/sync/jotaiSyncAdapter.js +1 -146
  28. package/lib/module/jotai/utils/jotaiStateStore.js +1 -317
  29. package/lib/module/jotai/utils/snapshotProvider.js +1 -214
  30. package/lib/module/jotai/utils/testIDs.js +1 -82
  31. package/lib/module/jotai/utils/watchAtoms.js +1 -157
  32. package/lib/module/jotai/utils/writeSemantics.js +1 -43
  33. package/lib/module/preset.js +1 -87
  34. package/lib/typescript/jotai/sync/jotaiSyncAdapter.d.ts +52 -2
  35. package/package.json +4 -4
  36. package/lib/typescript/index.d.ts.map +0 -1
  37. package/lib/typescript/jotai/components/JotaiAtomBrowser.d.ts.map +0 -1
  38. package/lib/typescript/jotai/components/JotaiAtomChangeItem.d.ts.map +0 -1
  39. package/lib/typescript/jotai/components/JotaiAtomDetailContent.d.ts.map +0 -1
  40. package/lib/typescript/jotai/components/JotaiAtomValueDetailContent.d.ts.map +0 -1
  41. package/lib/typescript/jotai/components/JotaiEventFilterView.d.ts.map +0 -1
  42. package/lib/typescript/jotai/components/JotaiIcon.d.ts.map +0 -1
  43. package/lib/typescript/jotai/components/JotaiModal.d.ts.map +0 -1
  44. package/lib/typescript/jotai/hooks/useJotaiAtomChanges.d.ts.map +0 -1
  45. package/lib/typescript/jotai/sync/jotaiSyncAdapter.d.ts.map +0 -1
  46. package/lib/typescript/jotai/types/index.d.ts.map +0 -1
  47. package/lib/typescript/jotai/utils/jotaiStateStore.d.ts.map +0 -1
  48. package/lib/typescript/jotai/utils/snapshotProvider.d.ts.map +0 -1
  49. package/lib/typescript/jotai/utils/testIDs.d.ts.map +0 -1
  50. package/lib/typescript/jotai/utils/watchAtoms.d.ts.map +0 -1
  51. package/lib/typescript/jotai/utils/writeSemantics.d.ts.map +0 -1
  52. package/lib/typescript/preset.d.ts.map +0 -1
@@ -1,215 +1 @@
1
- "use strict";
2
-
3
- var _sharedUi = require("@buoy-gg/shared-ui");
4
- var _dataViewer = require("@buoy-gg/shared-ui/dataViewer");
5
- var _jotaiStateStore = require("./jotaiStateStore");
6
- /**
7
- * Time Machine snapshot provider for Jotai.
8
- *
9
- * Registered into @buoy-gg/shared-ui's stateSnapshotRegistry at module init;
10
- * consumed by @buoy-gg/time-machine.
11
- *
12
- * Coverage is opt-in by nature: only atoms the app passed to `watchAtoms` /
13
- * `watchDefaultStoreAtoms` are visible. Derived (read-only) atoms are captured
14
- * for inspection but never restored — they recompute from their sources.
15
- * Caveat (documented): atoms with custom write functions (`atomWithReducer`)
16
- * receive ACTIONS through `store.set`, so restoring a raw value can misbehave;
17
- * nothing in the registry distinguishes them from plain atoms.
18
- */
19
-
20
- function status() {
21
- const atoms = _jotaiStateStore.jotaiStateStore.getAtoms();
22
- if (atoms.length === 0) {
23
- return {
24
- canCapture: false,
25
- canRestore: false,
26
- detail: "No Jotai atoms registered — pass your atoms to watchAtoms() / watchDefaultStoreAtoms()"
27
- };
28
- }
29
- const writable = atoms.filter(a => a.setValue).length;
30
- return {
31
- canCapture: true,
32
- canRestore: writable > 0,
33
- detail: writable < atoms.length ? `${atoms.length - writable} derived atom(s) recompute automatically and are not restored` : undefined
34
- };
35
- }
36
- async function capture() {
37
- const warnings = [];
38
- const atoms = {};
39
- let derived = 0;
40
- for (const info of _jotaiStateStore.jotaiStateStore.getAtoms()) {
41
- try {
42
- const value = info.getValue();
43
- if (typeof value === "function") {
44
- warnings.push(`atom "${info.label}": function value not capturable`);
45
- continue;
46
- }
47
- if (!info.setValue) derived += 1;
48
- atoms[info.label] = {
49
- value,
50
- writable: Boolean(info.setValue)
51
- };
52
- } catch (e) {
53
- warnings.push(`atom "${info.label}": capture failed (${String(e)})`);
54
- }
55
- }
56
- if (derived > 0) {
57
- warnings.push(`${derived} derived atom(s) captured for inspection only (recompute on restore)`);
58
- }
59
- return {
60
- data: {
61
- atoms
62
- },
63
- warnings
64
- };
65
- }
66
- async function restore(data, options) {
67
- const snapshot = data;
68
- const scope = (0, _sharedUi.createRestoreScope)(options);
69
- const result = (0, _sharedUi.emptyRestoreResult)();
70
- if (!snapshot?.atoms) {
71
- return (0, _sharedUi.failedRestore)("empty jotai snapshot");
72
- }
73
- let derived = 0;
74
- for (const [label, entry] of Object.entries(snapshot.atoms)) {
75
- if (!scope.allows(label)) continue;
76
- if (!entry.writable) {
77
- derived += 1;
78
- continue;
79
- }
80
- const info = _jotaiStateStore.jotaiStateStore.getAtom(label);
81
- if (!info) {
82
- result.skipped.push({
83
- id: label,
84
- reason: "atom not registered (renamed, or unwatched since snapshot?)"
85
- });
86
- continue;
87
- }
88
- if (!info.setValue) {
89
- result.skipped.push({
90
- id: label,
91
- reason: "atom is now read-only (derived)"
92
- });
93
- continue;
94
- }
95
- try {
96
- info.setValue(entry.value);
97
- result.applied += 1;
98
- } catch (e) {
99
- result.skipped.push({
100
- id: label,
101
- reason: String(e)
102
- });
103
- result.ok = false;
104
- }
105
- }
106
- if (derived > 0) {
107
- result.warnings.push(`${derived} derived atom(s) left to recompute`);
108
- }
109
- const excludedWarning = scope.excludedWarning("atom(s)");
110
- if (excludedWarning) result.warnings.push(excludedWarning);
111
- return result;
112
- }
113
-
114
- /** Per-atom restore preview. `base` omitted = compare against live atoms. */
115
- async function previewItems(target, base) {
116
- const targetAtoms = target?.atoms ?? {};
117
- const liveMode = base === undefined;
118
- const baseAtoms = {};
119
- if (liveMode) {
120
- for (const info of _jotaiStateStore.jotaiStateStore.getAtoms()) {
121
- try {
122
- baseAtoms[info.label] = {
123
- value: info.getValue(),
124
- writable: Boolean(info.setValue)
125
- };
126
- } catch {
127
- // unreadable atom — treated as absent
128
- }
129
- }
130
- } else {
131
- Object.assign(baseAtoms, base?.atoms ?? {});
132
- }
133
- const items = [];
134
- const notes = [];
135
- for (const label of (0, _sharedUi.unionKeys)(baseAtoms, targetAtoms)) {
136
- const inTarget = label in targetAtoms;
137
- if (!inTarget) {
138
- if (liveMode) notes.push(`atom "${label}" not in snapshot — untouched`);else items.push({
139
- key: label,
140
- label,
141
- verdict: "removed",
142
- oldValue: baseAtoms[label]?.value
143
- });
144
- continue;
145
- }
146
- const entry = targetAtoms[label];
147
- const equal = label in baseAtoms && (0, _dataViewer.boundedDeepEqual)(baseAtoms[label].value, entry.value);
148
- if (!entry.writable) {
149
- items.push({
150
- key: label,
151
- label,
152
- verdict: equal ? "unchanged" : "recomputes",
153
- reason: equal ? undefined : "derived atom — recomputes from its sources",
154
- oldValue: baseAtoms[label]?.value,
155
- newValue: entry.value
156
- });
157
- continue;
158
- }
159
- if (liveMode) {
160
- const info = _jotaiStateStore.jotaiStateStore.getAtom(label);
161
- if (!info) {
162
- items.push({
163
- key: label,
164
- label,
165
- verdict: "wont-apply",
166
- reason: "atom not registered (renamed, or unwatched since snapshot?)"
167
- });
168
- continue;
169
- }
170
- if (!info.setValue) {
171
- items.push({
172
- key: label,
173
- label,
174
- verdict: "wont-apply",
175
- reason: "atom is now read-only (derived)"
176
- });
177
- continue;
178
- }
179
- }
180
- if (!(label in baseAtoms)) {
181
- items.push({
182
- key: label,
183
- label,
184
- verdict: "added",
185
- newValue: entry.value
186
- });
187
- } else if (equal) {
188
- items.push({
189
- key: label,
190
- label,
191
- verdict: "unchanged"
192
- });
193
- } else {
194
- items.push({
195
- key: label,
196
- label,
197
- verdict: "changed",
198
- oldValue: baseAtoms[label].value,
199
- newValue: entry.value
200
- });
201
- }
202
- }
203
- return {
204
- items,
205
- notes
206
- };
207
- }
208
- (0, _sharedUi.registerStateSnapshotProvider)({
209
- id: "jotai",
210
- label: "Jotai",
211
- status,
212
- capture,
213
- restore,
214
- previewItems
215
- });
1
+ "use strict";var _sharedUi=require("@buoy-gg/shared-ui"),_dataViewer=require("@buoy-gg/shared-ui/dataViewer"),_jotaiStateStore=require("./jotaiStateStore");function status(){const e=_jotaiStateStore.jotaiStateStore.getAtoms();if(0===e.length)return{canCapture:!1,canRestore:!1,detail:"No Jotai atoms registered — pass your atoms to watchAtoms() / watchDefaultStoreAtoms()"};const t=e.filter(e=>e.setValue).length;return{canCapture:!0,canRestore:t>0,detail:t<e.length?e.length-t+" derived atom(s) recompute automatically and are not restored":void 0}}async function capture(){const e=[],t={};let a=0;for(const o of _jotaiStateStore.jotaiStateStore.getAtoms())try{const r=o.getValue();if("function"==typeof r){e.push(`atom "${o.label}": function value not capturable`);continue}o.setValue||(a+=1),t[o.label]={value:r,writable:Boolean(o.setValue)}}catch(t){e.push(`atom "${o.label}": capture failed (${String(t)})`)}return a>0&&e.push(`${a} derived atom(s) captured for inspection only (recompute on restore)`),{data:{atoms:t},warnings:e}}async function restore(e,t){const a=e,o=(0,_sharedUi.createRestoreScope)(t),r=(0,_sharedUi.emptyRestoreResult)();if(!a?.atoms)return(0,_sharedUi.failedRestore)("empty jotai snapshot");let s=0;for(const[e,t]of Object.entries(a.atoms)){if(!o.allows(e))continue;if(!t.writable){s+=1;continue}const a=_jotaiStateStore.jotaiStateStore.getAtom(e);if(a)if(a.setValue)try{a.setValue(t.value),r.applied+=1}catch(t){r.skipped.push({id:e,reason:String(t)}),r.ok=!1}else r.skipped.push({id:e,reason:"atom is now read-only (derived)"});else r.skipped.push({id:e,reason:"atom not registered (renamed, or unwatched since snapshot?)"})}s>0&&r.warnings.push(`${s} derived atom(s) left to recompute`);const n=o.excludedWarning("atom(s)");return n&&r.warnings.push(n),r}async function previewItems(e,t){const a=e?.atoms??{},o=void 0===t,r={};if(o)for(const e of _jotaiStateStore.jotaiStateStore.getAtoms())try{r[e.label]={value:e.getValue(),writable:Boolean(e.setValue)}}catch{}else Object.assign(r,t?.atoms??{});const s=[],n=[];for(const e of(0,_sharedUi.unionKeys)(r,a)){if(!(e in a)){o?n.push(`atom "${e}" not in snapshot — untouched`):s.push({key:e,label:e,verdict:"removed",oldValue:r[e]?.value});continue}const t=a[e],i=e in r&&(0,_dataViewer.boundedDeepEqual)(r[e].value,t.value);if(t.writable){if(o){const t=_jotaiStateStore.jotaiStateStore.getAtom(e);if(!t){s.push({key:e,label:e,verdict:"wont-apply",reason:"atom not registered (renamed, or unwatched since snapshot?)"});continue}if(!t.setValue){s.push({key:e,label:e,verdict:"wont-apply",reason:"atom is now read-only (derived)"});continue}}e in r?i?s.push({key:e,label:e,verdict:"unchanged"}):s.push({key:e,label:e,verdict:"changed",oldValue:r[e].value,newValue:t.value}):s.push({key:e,label:e,verdict:"added",newValue:t.value})}else s.push({key:e,label:e,verdict:i?"unchanged":"recomputes",reason:i?void 0:"derived atom — recomputes from its sources",oldValue:r[e]?.value,newValue:t.value})}return{items:s,notes:n}}(0,_sharedUi.registerStateSnapshotProvider)({id:"jotai",label:"Jotai",status:status,capture:capture,restore:restore,previewItems:previewItems});
@@ -1,90 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.JOTAI_TEST_ID = void 0;
7
- exports.atomRowTestID = atomRowTestID;
8
- exports.buildChangeRowTestIDs = buildChangeRowTestIDs;
9
- exports.changeRowTestID = changeRowTestID;
10
- exports.slugForTestID = slugForTestID;
11
- /**
12
- * Jotai tool test handles.
13
- *
14
- * Every tappable and every value the e2e suite reads back is addressed
15
- * `buoy-jotai-<thing>`. Two rules need explaining, and the second is the one
16
- * this tool does not share with any other.
17
- *
18
- * **A change row's handle carries an ORDINAL, and it counts per ATOM.** Same
19
- * reason as the Zustand tool: an atom a screen writes to fifty times produces
20
- * fifty separate, equally real rows, and addressing them by label alone would
21
- * give fifty views one handle — Detox fails a *matcher* that hits more than
22
- * one element, so the suite would break on `toExist()` before any assertion,
23
- * with an error that says nothing about duplicate changes. The handle is
24
- * `buoy-jotai-row-<slug(atomLabel)>-<n>`, n counting back from the newest row
25
- * for that atom (1 = newest). Deliberately relative: an atom change has no
26
- * identity a host could know in advance, and what a test CAN know is "the
27
- * write I just fired is the newest change to that atom". Read and tap in one
28
- * beat — a row's ordinal moves the instant another change lands.
29
- *
30
- * **The ordinal is per ATOM, and one write can move several atoms.** This is
31
- * where jotai differs from every store-shaped tool. Atoms form a dependency
32
- * graph, and this tool watches NODES: a single `set` on a primitive atom
33
- * notifies every mounted derived atom whose value actually moved, so one
34
- * press can add a row under three different labels — and add nothing at all
35
- * under a fourth whose recomputed value happened to be unchanged. Counting
36
- * ordinals across the whole list would make a row's handle depend on how many
37
- * *other* atoms the write happened to touch, which is exactly the thing a
38
- * test of a graph must not assume.
39
- *
40
- * **An atom row's handle does NOT carry an ordinal.** An atom is registered
41
- * once under a label that is unique by construction
42
- * (`jotaiStateStore.registerAtom` returns early on a label it already holds,
43
- * and `watchAtoms` marks the atom with a per-label symbol), so
44
- * `buoy-jotai-atom-<slug(label)>` is as stable as the registration itself —
45
- * and unlike a change, it is a thing the host knows the name of before the
46
- * app has booted.
47
- *
48
- * The suite mirrors these rules in CommonJS (`example-dev-build/utils/
49
- * jotaiBench.js`) and asserts the two agree — Detox runs in Node on the host
50
- * and cannot import TypeScript.
51
- */
52
-
53
- /** Prefix every handle in this tool shares. */
54
- const JOTAI_TEST_ID = exports.JOTAI_TEST_ID = "buoy-jotai";
55
-
56
- /** `jbenchCount` → `jbenchcount`. Shared with every Buoy tool. */
57
- function slugForTestID(value) {
58
- return String(value ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "unknown";
59
- }
60
-
61
- /**
62
- * Handle for one change row: `buoy-jotai-row-jbenchcount-1`.
63
- *
64
- * `ordinal` is 1-based and counts back from the newest row for this atom.
65
- */
66
- function changeRowTestID(atomLabel, ordinal) {
67
- return `${JOTAI_TEST_ID}-row-${slugForTestID(atomLabel)}-${ordinal}`;
68
- }
69
-
70
- /** Handle for one atom row in the browser: `buoy-jotai-atom-jbenchcount`. */
71
- function atomRowTestID(atomLabel) {
72
- return `${JOTAI_TEST_ID}-atom-${slugForTestID(atomLabel)}`;
73
- }
74
-
75
- /**
76
- * Row handle per change id for one rendered list, newest-first.
77
- *
78
- * Built once for the whole list rather than per row, because an ordinal is a
79
- * fact about the list and a row cannot compute its own.
80
- */
81
- function buildChangeRowTestIDs(changes) {
82
- const seen = new Map();
83
- const handles = new Map();
84
- for (const change of changes) {
85
- const ordinal = (seen.get(change.atomLabel) ?? 0) + 1;
86
- seen.set(change.atomLabel, ordinal);
87
- handles.set(change.id, changeRowTestID(change.atomLabel, ordinal));
88
- }
89
- return handles;
90
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.JOTAI_TEST_ID=void 0,exports.atomRowTestID=atomRowTestID,exports.buildChangeRowTestIDs=buildChangeRowTestIDs,exports.changeRowTestID=changeRowTestID,exports.slugForTestID=slugForTestID;const JOTAI_TEST_ID=exports.JOTAI_TEST_ID="buoy-jotai";function slugForTestID(e){return String(e??"").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unknown"}function changeRowTestID(e,t){return`${JOTAI_TEST_ID}-row-${slugForTestID(e)}-${t}`}function atomRowTestID(e){return`${JOTAI_TEST_ID}-atom-${slugForTestID(e)}`}function buildChangeRowTestIDs(e){const t=new Map,o=new Map;for(const s of e){const e=(t.get(s.atomLabel)??0)+1;t.set(s.atomLabel,e),o.set(s.id,changeRowTestID(s.atomLabel,e))}return o}
@@ -1,162 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.isAtomWatched = isAtomWatched;
7
- exports.watchAtoms = watchAtoms;
8
- exports.watchDefaultStoreAtoms = watchDefaultStoreAtoms;
9
- var _jotaiStateStore = require("./jotaiStateStore");
10
- var _writeSemantics = require("./writeSemantics");
11
- /**
12
- * Buoy Jotai DevTools — Atom instrumentation
13
- *
14
- * watchAtoms() — RECOMMENDED. Pass a Jotai store and a named atom map.
15
- * Uses store.sub() to observe each atom externally. Never touches atom internals.
16
- *
17
- * watchDefaultStoreAtoms() — Convenience wrapper that uses getDefaultStore().
18
- */
19
-
20
- /** Any Jotai atom — alias for Atom<unknown> for readability */
21
-
22
- /** Minimal store shape — what Jotai's createStore() / getDefaultStore() returns */
23
-
24
- /**
25
- * Watch a set of Jotai atoms for value changes.
26
- *
27
- * Pass a Jotai store and an object mapping display names to atoms.
28
- * Uses store.sub() to observe each atom — never modifies atom internals.
29
- *
30
- * @example
31
- * ```tsx
32
- * import { getDefaultStore } from 'jotai';
33
- * import { watchAtoms } from '@buoy-gg/jotai';
34
- * import { countAtom } from './atoms/count';
35
- * import { authAtom } from './atoms/auth';
36
- *
37
- * watchAtoms(getDefaultStore(), {
38
- * countAtom,
39
- * authAtom,
40
- * });
41
- * ```
42
- *
43
- * @param store - Jotai store (from createStore() or getDefaultStore())
44
- * @param atoms - Object mapping display labels to Jotai atoms
45
- * @param options - Optional configuration
46
- * @returns Cleanup function that removes all subscriptions
47
- */
48
- function watchAtoms(store, atoms, options) {
49
- const enabled = options?.enabled !== false;
50
- const cleanups = [];
51
- for (const [label, atom] of Object.entries(atoms)) {
52
- // Skip if already watched under this label
53
- const atomAny = atom;
54
- const watchedKey = Symbol.for(`@@buoy-jotai/watched/${label}`);
55
- if (atomAny[watchedKey]) continue;
56
- atomAny[watchedKey] = true;
57
- let prevValue;
58
- try {
59
- prevValue = store.get(atom);
60
- } catch {
61
- prevValue = undefined;
62
- }
63
-
64
- // Register the atom for the browser view. A setter is captured only when
65
- // the store supports `set` AND the atom itself is writable (has a `write`
66
- // function — primitive atoms get a default one, read-only derived atoms
67
- // have none). Deriveds used to get a setter that threw inside jotai at
68
- // set time; now they're correctly reported as read-only up front.
69
- const isWritable = typeof atom.write === "function";
70
- const setter = store.set && isWritable ? value => {
71
- store.set(atom, value);
72
- } : undefined;
73
- _jotaiStateStore.jotaiStateStore.registerAtom(label, () => {
74
- try {
75
- return store.get(atom);
76
- } catch {
77
- return undefined;
78
- }
79
- }, setter,
80
- // "value" = jotai's shared defaultWrite (a primitive atom: store.set
81
- // stores the value). "action" = a custom write receiving the value as
82
- // its argument — still writable, but the editor says so.
83
- isWritable ? (0, _writeSemantics.classifyAtomWrite)(atom) : undefined);
84
-
85
- // Record initial value
86
- if (enabled) {
87
- _jotaiStateStore.jotaiStateStore.addAtomChange({
88
- atomLabel: label,
89
- prevValue: undefined,
90
- nextValue: prevValue,
91
- category: "initial"
92
- });
93
- }
94
-
95
- // Subscribe to future changes
96
- const unsubscribe = store.sub(atom, () => {
97
- try {
98
- const nextValue = store.get(atom);
99
- _jotaiStateStore.jotaiStateStore.addAtomChange({
100
- atomLabel: label,
101
- prevValue,
102
- nextValue,
103
- category: "write"
104
- });
105
- prevValue = nextValue;
106
- } catch {
107
- // Silently catch — our bug must never propagate into the store
108
- }
109
- });
110
- cleanups.push(() => {
111
- unsubscribe();
112
- delete atomAny[watchedKey];
113
- _jotaiStateStore.jotaiStateStore.unregisterAtom(label);
114
- });
115
- }
116
- return () => cleanups.forEach(fn => fn());
117
- }
118
-
119
- /**
120
- * Watch atoms using Jotai's default store (no Provider setup required).
121
- *
122
- * Convenience wrapper around watchAtoms() — auto-imports getDefaultStore.
123
- *
124
- * @example
125
- * ```tsx
126
- * import { watchDefaultStoreAtoms } from '@buoy-gg/jotai';
127
- * import { countAtom, authAtom } from './atoms';
128
- *
129
- * watchDefaultStoreAtoms({ countAtom, authAtom });
130
- * ```
131
- */
132
- function watchDefaultStoreAtoms(atoms, options) {
133
- // Dynamic import to avoid bundling jotai internals when not needed
134
- let store;
135
- try {
136
- // eslint-disable-next-line @typescript-eslint/no-var-requires
137
- const {
138
- getDefaultStore
139
- } = require("jotai/vanilla");
140
- store = getDefaultStore();
141
- } catch {
142
- // Try the main jotai entry (React Native)
143
- try {
144
- // eslint-disable-next-line @typescript-eslint/no-var-requires
145
- const {
146
- getDefaultStore
147
- } = require("jotai");
148
- store = getDefaultStore();
149
- } catch {
150
- console.warn("[@buoy-gg/jotai] Could not find getDefaultStore from jotai. " + "Pass the store explicitly via watchAtoms(store, atoms) instead.");
151
- return () => {};
152
- }
153
- }
154
- return watchAtoms(store, atoms, options);
155
- }
156
-
157
- /**
158
- * Check if an atom label is currently being watched
159
- */
160
- function isAtomWatched(label) {
161
- return _jotaiStateStore.jotaiStateStore.getAtom(label) !== undefined;
162
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.isAtomWatched=isAtomWatched,exports.watchAtoms=watchAtoms,exports.watchDefaultStoreAtoms=watchDefaultStoreAtoms;var _jotaiStateStore=require("./jotaiStateStore"),_writeSemantics=require("./writeSemantics");function watchAtoms(t,e,o){const a=!1!==o?.enabled,r=[];for(const[o,i]of Object.entries(e)){const e=i,c=Symbol.for(`@@buoy-jotai/watched/${o}`);if(e[c])continue;let s;e[c]=!0;try{s=t.get(i)}catch{s=void 0}const n="function"==typeof i.write,u=t.set&&n?e=>{t.set(i,e)}:void 0;_jotaiStateStore.jotaiStateStore.registerAtom(o,()=>{try{return t.get(i)}catch{return}},u,n?(0,_writeSemantics.classifyAtomWrite)(i):void 0),a&&_jotaiStateStore.jotaiStateStore.addAtomChange({atomLabel:o,prevValue:void 0,nextValue:s,category:"initial"});const S=t.sub(i,()=>{try{const e=t.get(i);_jotaiStateStore.jotaiStateStore.addAtomChange({atomLabel:o,prevValue:s,nextValue:e,category:"write"}),s=e}catch{}});r.push(()=>{S(),delete e[c],_jotaiStateStore.jotaiStateStore.unregisterAtom(o)})}return()=>r.forEach(t=>t())}function watchDefaultStoreAtoms(t,e){let o;try{const{getDefaultStore:t}=require("jotai/vanilla");o=t()}catch{try{const{getDefaultStore:t}=require("jotai");o=t()}catch{return console.warn("[@buoy-gg/jotai] Could not find getDefaultStore from jotai. Pass the store explicitly via watchAtoms(store, atoms) instead."),()=>{}}}return watchAtoms(o,t,e)}function isAtomWatched(t){return void 0!==_jotaiStateStore.jotaiStateStore.getAtom(t)}
@@ -1,47 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.classifyAtomWrite = classifyAtomWrite;
7
- var _vanilla = require("jotai/vanilla");
8
- /**
9
- * What does `store.set(atom, x)` DO with x?
10
- *
11
- * jotai's `atom(initialValue)` gives every primitive atom the SAME shared
12
- * `defaultWrite` function (see jotai/src/vanilla/atom.ts): it stores x as the
13
- * atom's new value (or calls it with the previous value when x is a
14
- * function). Any other `write` is app code — `atomWithReducer` receives
15
- * ACTIONS, `atomWithStorage` persists as a side effect, a custom write can do
16
- * anything — so a raw value handed to `store.set` may not mean "set to this".
17
- *
18
- * The tool's editor needs that distinction to be honest: a "value" atom edits
19
- * like plain data; an "action" atom is still writable (the MCP `setAtom` and
20
- * Time Machine restore already write to them) but the editor says what the
21
- * write actually does.
22
- *
23
- * Detection: a probe primitive atom exposes this jotai instance's
24
- * `defaultWrite`; identity match is exact. The `toString` fallback covers an
25
- * app that somehow bundles a second jotai copy (its atoms carry a different —
26
- * but textually identical — defaultWrite instance). An unrecognized write
27
- * classifies as "action", which only makes the editor more cautious.
28
- */
29
-
30
- const probe = (0, _vanilla.atom)(null);
31
- const DEFAULT_WRITE = probe.write;
32
- const DEFAULT_WRITE_SOURCE = typeof DEFAULT_WRITE === "function" ? String(DEFAULT_WRITE) : null;
33
-
34
- /** Classify a writable atom's write. Call only when `atom.write` exists. */
35
- function classifyAtomWrite(candidate) {
36
- const atomLike = candidate;
37
- const write = atomLike?.write;
38
- if (typeof write !== "function") return "action";
39
- if (write === DEFAULT_WRITE) return "value";
40
- // Dual-copy fallback: a primitive atom from another jotai instance holds
41
- // its own value (`init` present) and its write's source is the library's
42
- // defaultWrite verbatim.
43
- if (atomLike !== null && "init" in atomLike && DEFAULT_WRITE_SOURCE !== null && String(write) === DEFAULT_WRITE_SOURCE) {
44
- return "value";
45
- }
46
- return "action";
47
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.classifyAtomWrite=classifyAtomWrite;var _vanilla=require("jotai/vanilla");const probe=(0,_vanilla.atom)(null),DEFAULT_WRITE=probe.write,DEFAULT_WRITE_SOURCE="function"==typeof DEFAULT_WRITE?String(DEFAULT_WRITE):null;function classifyAtomWrite(t){const i=t,e=i?.write;return"function"!=typeof e?"action":e===DEFAULT_WRITE||null!==i&&"init"in i&&null!==DEFAULT_WRITE_SOURCE&&String(e)===DEFAULT_WRITE_SOURCE?"value":"action"}
@@ -1,90 +1 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.jotaiToolPreset = exports.createJotaiTool = void 0;
7
- var _JotaiModal = require("./jotai/components/JotaiModal");
8
- var _JotaiIcon = require("./jotai/components/JotaiIcon");
9
- var _sharedUi = require("@buoy-gg/shared-ui");
10
- var _jsxRuntime = require("react/jsx-runtime");
11
- /**
12
- * Pre-configured Jotai DevTools preset for FloatingDevTools
13
- *
14
- * ZERO-CONFIG: This preset is auto-discovered by FloatingDevTools!
15
- * Just install @buoy-gg/jotai and call watchAtoms() with your atoms.
16
- *
17
- * @example Automatic (recommended)
18
- * ```tsx
19
- * import { getDefaultStore } from 'jotai';
20
- * import { watchAtoms } from '@buoy-gg/jotai';
21
- * import { countAtom, authAtom } from './atoms';
22
- *
23
- * watchAtoms(getDefaultStore(), { countAtom, authAtom });
24
- *
25
- * // The Jotai tool appears automatically in FloatingDevTools!
26
- * <FloatingDevTools />
27
- * ```
28
- *
29
- * @example Manual (only for custom configuration)
30
- * ```tsx
31
- * import { createJotaiTool } from '@buoy-gg/jotai';
32
- *
33
- * const customJotaiTool = createJotaiTool({
34
- * name: "ATOMS",
35
- * iconColor: "#6C47FF",
36
- * });
37
- *
38
- * <FloatingDevTools apps={[customJotaiTool]} />
39
- * ```
40
- */
41
-
42
- /**
43
- * Pre-configured Jotai DevTools preset for FloatingDevTools.
44
- * Includes:
45
- * - Live atom change monitoring
46
- * - Atom value inspection (JSON viewer)
47
- * - Value diff visualization (tree + split)
48
- * - Filter by atom label
49
- * - Changed keys tracking for object atoms
50
- */
51
- const jotaiToolPreset = exports.jotaiToolPreset = (0, _sharedUi.defineToolPreset)({
52
- id: "jotai",
53
- name: "JOTAI",
54
- description: "Jotai atom & state inspector",
55
- slot: "both",
56
- icon: ({
57
- size
58
- }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_JotaiIcon.JotaiIcon, {
59
- size: size
60
- }),
61
- component: _JotaiModal.JotaiModal,
62
- props: {
63
- enableSharedModalDimensions: false
64
- }
65
- });
66
-
67
- /**
68
- * Create a custom Jotai DevTools configuration.
69
- * Use this if you want to override default settings.
70
- *
71
- * @example
72
- * ```tsx
73
- * import { createJotaiTool } from '@buoy-gg/jotai';
74
- *
75
- * const myJotaiTool = createJotaiTool({
76
- * name: "ATOMS",
77
- * iconColor: "#6C47FF",
78
- * });
79
- * ```
80
- */
81
- const createJotaiTool = exports.createJotaiTool = (0, _sharedUi.createToolFactory)(jotaiToolPreset, options => ({
82
- enableSharedModalDimensions: options.enableSharedModalDimensions ?? false
83
- }), options => ({
84
- icon: ({
85
- size
86
- }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_JotaiIcon.JotaiIcon, {
87
- size: size,
88
- color: options.iconColor
89
- })
90
- }));
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.jotaiToolPreset=exports.createJotaiTool=void 0;var _JotaiModal=require("./jotai/components/JotaiModal"),_JotaiIcon=require("./jotai/components/JotaiIcon"),_sharedUi=require("@buoy-gg/shared-ui"),_jsxRuntime=require("react/jsx-runtime");const jotaiToolPreset=exports.jotaiToolPreset=(0,_sharedUi.defineToolPreset)({id:"jotai",name:"JOTAI",description:"Jotai atom & state inspector",slot:"both",icon:({size:o})=>(0,_jsxRuntime.jsx)(_JotaiIcon.JotaiIcon,{size:o}),component:_JotaiModal.JotaiModal,props:{enableSharedModalDimensions:!1}}),createJotaiTool=exports.createJotaiTool=(0,_sharedUi.createToolFactory)(jotaiToolPreset,o=>({enableSharedModalDimensions:o.enableSharedModalDimensions??!1}),o=>({icon:({size:e})=>(0,_jsxRuntime.jsx)(_JotaiIcon.JotaiIcon,{size:e,color:o.iconColor})}));