@buoy-gg/zustand 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 (63) hide show
  1. package/README.md +11 -76
  2. package/lib/commonjs/index.js +1 -105
  3. package/lib/commonjs/preset.js +1 -102
  4. package/lib/commonjs/zustand/components/ZustandActionButton.js +1 -118
  5. package/lib/commonjs/zustand/components/ZustandDetailViewToggle.js +1 -48
  6. package/lib/commonjs/zustand/components/ZustandEventFilterView.js +1 -43
  7. package/lib/commonjs/zustand/components/ZustandIcon.js +1 -18
  8. package/lib/commonjs/zustand/components/ZustandModal.js +1 -772
  9. package/lib/commonjs/zustand/components/ZustandStateChangeItem.js +1 -117
  10. package/lib/commonjs/zustand/components/ZustandStateDetailContent.js +1 -372
  11. package/lib/commonjs/zustand/components/ZustandStateInfoView.js +1 -488
  12. package/lib/commonjs/zustand/components/ZustandStoreBrowser.js +1 -204
  13. package/lib/commonjs/zustand/components/ZustandStoreDetailContent.js +1 -471
  14. package/lib/commonjs/zustand/hooks/useZustandStateChanges.js +1 -74
  15. package/lib/commonjs/zustand/sync/zustandSyncAdapter.js +1 -418
  16. package/lib/commonjs/zustand/types/index.js +1 -1
  17. package/lib/commonjs/zustand/utils/buoyZustandMiddleware.js +1 -284
  18. package/lib/commonjs/zustand/utils/editableState.js +1 -146
  19. package/lib/commonjs/zustand/utils/snapshotProvider.js +1 -209
  20. package/lib/commonjs/zustand/utils/testIDs.js +1 -82
  21. package/lib/commonjs/zustand/utils/zustandStateStore.js +1 -379
  22. package/lib/module/index.js +1 -90
  23. package/lib/module/preset.js +1 -98
  24. package/lib/module/zustand/components/ZustandActionButton.js +1 -114
  25. package/lib/module/zustand/components/ZustandDetailViewToggle.js +1 -43
  26. package/lib/module/zustand/components/ZustandEventFilterView.js +1 -39
  27. package/lib/module/zustand/components/ZustandIcon.js +1 -11
  28. package/lib/module/zustand/components/ZustandModal.js +1 -767
  29. package/lib/module/zustand/components/ZustandStateChangeItem.js +1 -113
  30. package/lib/module/zustand/components/ZustandStateDetailContent.js +1 -368
  31. package/lib/module/zustand/components/ZustandStateInfoView.js +1 -483
  32. package/lib/module/zustand/components/ZustandStoreBrowser.js +1 -200
  33. package/lib/module/zustand/components/ZustandStoreDetailContent.js +1 -469
  34. package/lib/module/zustand/hooks/useZustandStateChanges.js +1 -70
  35. package/lib/module/zustand/sync/zustandSyncAdapter.js +1 -415
  36. package/lib/module/zustand/types/index.js +1 -1
  37. package/lib/module/zustand/utils/buoyZustandMiddleware.js +1 -278
  38. package/lib/module/zustand/utils/editableState.js +1 -138
  39. package/lib/module/zustand/utils/snapshotProvider.js +1 -209
  40. package/lib/module/zustand/utils/testIDs.js +1 -74
  41. package/lib/module/zustand/utils/zustandStateStore.js +1 -375
  42. package/lib/web/index.mjs +2 -3
  43. package/package.json +4 -7
  44. package/lib/typescript/index.d.ts.map +0 -1
  45. package/lib/typescript/preset.d.ts.map +0 -1
  46. package/lib/typescript/zustand/components/ZustandActionButton.d.ts.map +0 -1
  47. package/lib/typescript/zustand/components/ZustandDetailViewToggle.d.ts.map +0 -1
  48. package/lib/typescript/zustand/components/ZustandEventFilterView.d.ts.map +0 -1
  49. package/lib/typescript/zustand/components/ZustandIcon.d.ts.map +0 -1
  50. package/lib/typescript/zustand/components/ZustandModal.d.ts.map +0 -1
  51. package/lib/typescript/zustand/components/ZustandStateChangeItem.d.ts.map +0 -1
  52. package/lib/typescript/zustand/components/ZustandStateDetailContent.d.ts.map +0 -1
  53. package/lib/typescript/zustand/components/ZustandStateInfoView.d.ts.map +0 -1
  54. package/lib/typescript/zustand/components/ZustandStoreBrowser.d.ts.map +0 -1
  55. package/lib/typescript/zustand/components/ZustandStoreDetailContent.d.ts.map +0 -1
  56. package/lib/typescript/zustand/hooks/useZustandStateChanges.d.ts.map +0 -1
  57. package/lib/typescript/zustand/sync/zustandSyncAdapter.d.ts.map +0 -1
  58. package/lib/typescript/zustand/types/index.d.ts.map +0 -1
  59. package/lib/typescript/zustand/utils/buoyZustandMiddleware.d.ts.map +0 -1
  60. package/lib/typescript/zustand/utils/editableState.d.ts.map +0 -1
  61. package/lib/typescript/zustand/utils/snapshotProvider.d.ts.map +0 -1
  62. package/lib/typescript/zustand/utils/testIDs.d.ts.map +0 -1
  63. package/lib/typescript/zustand/utils/zustandStateStore.d.ts.map +0 -1
@@ -1,74 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * Zustand tool test handles.
5
- *
6
- * Every tappable and every value the e2e suite reads back is addressed
7
- * `buoy-zustand-<thing>`. Two rules in here need explaining.
8
- *
9
- * **A change row's handle carries an ORDINAL, and it counts per STORE.** The
10
- * Events tool can address a row by what it is about, because an event is a
11
- * thing that happened to a named subject — one key, one URL, one atom. A
12
- * zustand state change is not: a store that a screen writes to fifty times
13
- * produces fifty separate, equally real rows. Addressing them by store name
14
- * alone would give fifty views one handle, and Detox fails a *matcher* that
15
- * hits more than one element — so a suite would break on `toExist()`, before
16
- * any assertion, with an error that says nothing about duplicate changes.
17
- *
18
- * So the handle is `buoy-zustand-row-<slug(storeName)>-<n>`, where n counts
19
- * back from the newest row of that store (1 = newest). Deliberately relative:
20
- * a state change has no identity a host could know in advance — `id` is a
21
- * timestamp plus a per-process counter whose value depends on how many
22
- * changes the app happened to make before the test looked. What a test CAN
23
- * know is "the set I just fired is the newest change to that store". Read and
24
- * tap in one beat: a row's ordinal moves the instant another change lands.
25
- *
26
- * **A store row's handle does NOT.** A store is registered once under a name
27
- * that is unique by construction (`zustandStateStore.registerStore` returns
28
- * early on a name it already holds), so `buoy-zustand-store-<slug(name)>` is
29
- * as stable as the store itself — and unlike a change, it is a thing the host
30
- * knows the name of before the app has booted.
31
- *
32
- * The suite mirrors these rules in CommonJS (`example-dev-build/utils/
33
- * zustandBench.js`) and asserts the two agree — Detox runs in Node on the host
34
- * and cannot import TypeScript.
35
- */
36
-
37
- /** Prefix every handle in this tool shares. */
38
- export const ZUSTAND_TEST_ID = "buoy-zustand";
39
-
40
- /** `zbenchWatch` → `zbenchwatch`. Shared with every Buoy tool. */
41
- export function slugForTestID(value) {
42
- return String(value ?? "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "") || "unknown";
43
- }
44
-
45
- /**
46
- * Handle for one change row: `buoy-zustand-row-zbenchwatch-1`.
47
- *
48
- * `ordinal` is 1-based and counts back from the newest row of this store.
49
- */
50
- export function changeRowTestID(storeName, ordinal) {
51
- return `${ZUSTAND_TEST_ID}-row-${slugForTestID(storeName)}-${ordinal}`;
52
- }
53
-
54
- /** Handle for one store row in the browser: `buoy-zustand-store-zbenchwatch`. */
55
- export function storeRowTestID(storeName) {
56
- return `${ZUSTAND_TEST_ID}-store-${slugForTestID(storeName)}`;
57
- }
58
-
59
- /**
60
- * Row handle per change id for one rendered list, newest-first.
61
- *
62
- * Built once for the whole list rather than per row, because an ordinal is a
63
- * fact about the list and a row cannot compute its own.
64
- */
65
- export function buildChangeRowTestIDs(changes) {
66
- const seen = new Map();
67
- const handles = new Map();
68
- for (const change of changes) {
69
- const ordinal = (seen.get(change.storeName) ?? 0) + 1;
70
- seen.set(change.storeName, ordinal);
71
- handles.set(change.id, changeRowTestID(change.storeName, ordinal));
72
- }
73
- return handles;
74
- }
1
+ "use strict";export const ZUSTAND_TEST_ID="buoy-zustand";export function slugForTestID(e){return String(e??"").toLowerCase().replace(/[^a-z0-9]+/g,"-").replace(/^-+|-+$/g,"")||"unknown"}export function changeRowTestID(e,t){return`${ZUSTAND_TEST_ID}-row-${slugForTestID(e)}-${t}`}export function storeRowTestID(e){return`${ZUSTAND_TEST_ID}-store-${slugForTestID(e)}`}export function buildChangeRowTestIDs(e){const t=new Map;const n=new Map;for(const o of e){const r=(t.get(o.storeName)??0)+1;t.set(o.storeName,r);n.set(o.id,changeRowTestID(o.storeName,r))}return n}
@@ -1,375 +1 @@
1
- "use strict";
2
-
3
- /**
4
- * Zustand state store - captures and stores Zustand state changes
5
- *
6
- * The generic event-log half (ring buffer, listeners, clear-forwarding for
7
- * the desktop mirror, pause/suppress flags, hydrate-by-id) is the shared
8
- * `ChangeLog`; this file owns the zustand-specific half — the store registry,
9
- * snapshots, mirror rebuild, filters and stats.
10
- */
11
-
12
- import { ChangeLog, entityColor, formatValuePreview, shallowDiffSummary } from "@buoy-gg/shared-ui";
13
- // ============================================
14
- // Store Color Palette
15
- // ============================================
16
-
17
- const STORE_COLORS = {
18
- counter: "#10B981",
19
- // emerald
20
- auth: "#8B5CF6",
21
- // purple
22
- user: "#3B82F6",
23
- // blue
24
- cart: "#EC4899",
25
- // pink
26
- app: "#6366F1",
27
- // indigo
28
- ui: "#F59E0B",
29
- // amber
30
- settings: "#14B8A6",
31
- // teal
32
- theme: "#06B6D4",
33
- // cyan
34
- navigation: "#F97316",
35
- // orange
36
- form: "#EF4444" // red
37
- };
38
-
39
- /**
40
- * Get consistent color for a store based on its name
41
- */
42
- function getStoreColor(storeName) {
43
- return entityColor(storeName, STORE_COLORS);
44
- }
45
-
46
- /**
47
- * Format payload preview for display
48
- */
49
- function formatPartialPreview(partial, maxLength = 40) {
50
- return formatValuePreview(partial, {
51
- maxLength,
52
- undefinedLabel: "",
53
- functionLabel: "(updater fn)"
54
- });
55
- }
56
-
57
- // ============================================
58
- // Zustand State Store
59
- // ============================================
60
-
61
- class ZustandStateStore extends ChangeLog {
62
- stores = new Map();
63
- storeListeners = new Set();
64
- remoteSetStateHandler = null;
65
-
66
- // ---- State Change Tracking ----
67
-
68
- addStateChange(params) {
69
- if (!this.canRecord()) return;
70
- const {
71
- storeName,
72
- partial,
73
- replace,
74
- prevState,
75
- nextState,
76
- duration,
77
- category,
78
- isPersisted = false
79
- } = params;
80
- const hasStateChange = prevState !== nextState;
81
- // `changedCount` counts the keys in `changedKeys` and nothing else, so the
82
- // row's "N keys changed" and the detail page's CHANGED KEYS card agree —
83
- // including the persist-rehydrate case where the object is replaced
84
- // without any top-level value moving.
85
- const {
86
- summary,
87
- changedKeys,
88
- changedCount
89
- } = shallowDiffSummary(prevState, nextState);
90
- const partialPreview = formatPartialPreview(partial);
91
- const isSlowUpdate = (duration ?? 0) > 16;
92
- const resolvedCategory = category ?? (replace ? "replace" : "setState");
93
- const stateChange = {
94
- id: this.nextId(),
95
- storeName,
96
- timestamp: Date.now(),
97
- prevState,
98
- nextState,
99
- partial,
100
- duration,
101
- hasStateChange,
102
- replace,
103
- category: resolvedCategory,
104
- changedKeys,
105
- changedKeysCount: changedCount,
106
- diffSummary: summary,
107
- partialPreview,
108
- isSlowUpdate,
109
- isPersisted
110
- };
111
- this.push(stateChange);
112
-
113
- // Update store change count
114
- const storeInfo = this.stores.get(storeName);
115
- if (storeInfo) {
116
- storeInfo.stateChangeCount++;
117
- }
118
- this.notifyListeners();
119
- }
120
- getStateChanges() {
121
- return this.getAll();
122
- }
123
- getStateChangeById(id) {
124
- return this.getById(id);
125
- }
126
- clearStateChanges() {
127
- // Reset counts
128
- for (const store of this.stores.values()) {
129
- store.stateChangeCount = 0;
130
- }
131
- this.clear();
132
- // The browser keeps its store list in React state and only re-reads it
133
- // on a store notification — `clear()` above only tells the change-log
134
- // listeners, so without this the cards kept their old counts after Clear.
135
- this.notifyStoreListeners();
136
- }
137
-
138
- // ---- Remote Mirror Mode ----
139
-
140
- /**
141
- * Replace the change log without touching the store registry. Used when a
142
- * host keeps real registered stores (JUMP / RESET stay wired) but needs to
143
- * reseed the event list — the docs live tour, or any mirror that already
144
- * called `registerStore`. Does not fire `onClear`. Respects the enabled
145
- * flag so the UI's pause button freezes the reseed.
146
- */
147
- replaceStateChanges(changes) {
148
- if (!this.getEnabled()) return;
149
- for (const store of this.stores.values()) {
150
- store.stateChangeCount = 0;
151
- }
152
- const kept = changes.slice(0, this.max);
153
- for (const change of kept) {
154
- const store = this.stores.get(change.storeName);
155
- if (store) store.stateChangeCount++;
156
- }
157
- this.setItems(kept);
158
- this.notifyStoreListeners();
159
- }
160
-
161
- /**
162
- * Serializable snapshot of the tracked stores (registry metadata plus each
163
- * store's current state). Used by the sync adapter on the device side —
164
- * the live `api` handles can't go over the wire.
165
- */
166
- getStoreSnapshots() {
167
- return Array.from(this.stores.values()).map(store => {
168
- let currentState;
169
- try {
170
- currentState = store.api.getState();
171
- } catch {
172
- currentState = undefined;
173
- }
174
- return {
175
- name: store.name,
176
- stateChangeCount: store.stateChangeCount,
177
- isPersisted: store.isPersisted,
178
- persistName: store.persistName,
179
- // Persisted is not the same as re-readable: a store registered before
180
- // its persist middleware attached has a name but no handle. The
181
- // Storage tool needs the difference to decide between offering "apply"
182
- // and telling the user to restart.
183
- canRehydrate: typeof store.rehydrate === "function",
184
- color: store.color,
185
- currentState
186
- };
187
- });
188
- }
189
-
190
- /**
191
- * Handler invoked when the UI calls setState on a mirrored store (e.g.
192
- * time-travel from the dashboard). Forward it to the synced device.
193
- */
194
- setRemoteSetStateHandler(handler) {
195
- this.remoteSetStateHandler = handler;
196
- }
197
-
198
- /**
199
- * Replace the entire mirror contents from a synced device snapshot. The
200
- * rebuilt registry entries get stub `api` handles: getState() serves the
201
- * snapshotted state, setState() forwards to the remote handler. Respects
202
- * the enabled flag so the UI's pause button freezes the mirror.
203
- */
204
- replaceFromSnapshot(changes, storeSnapshots) {
205
- if (!this.getEnabled()) return;
206
-
207
- // Preserve trees a remote mirror already hydrated — wire-form snapshots
208
- // always carry markers, and a later snapshot must not blank the detail pane.
209
- const hydrated = new Map();
210
- for (const existing of this.items) {
211
- const prev = existing.prevState;
212
- if (!prev || prev.__buoyStateOnDevice !== true) {
213
- hydrated.set(existing.id, existing);
214
- }
215
- }
216
- const merged = changes.slice(0, this.max).map(change => {
217
- const kept = hydrated.get(change.id);
218
- const incoming = change.prevState;
219
- return kept && incoming?.__buoyStateOnDevice === true ? {
220
- ...change,
221
- prevState: kept.prevState,
222
- nextState: kept.nextState,
223
- partial: kept.partial
224
- } : change;
225
- });
226
-
227
- // Preserve currentState a remote mirror already hydrated — oversized
228
- // stores arrive as STATE_ON_DEVICE markers on every snapshot.
229
- const hydratedStores = new Map();
230
- for (const [name, store] of this.stores) {
231
- try {
232
- const state = store.api.getState();
233
- const marker = state;
234
- if (!marker || marker.__buoyStateOnDevice !== true) {
235
- hydratedStores.set(name, state);
236
- }
237
- } catch {
238
- // ignore
239
- }
240
- }
241
- this.stores = new Map(storeSnapshots.map(snapshot => {
242
- const incoming = snapshot.currentState;
243
- const kept = hydratedStores.get(snapshot.name);
244
- const currentState = kept !== undefined && incoming?.__buoyStateOnDevice === true ? kept : snapshot.currentState;
245
- return [snapshot.name, {
246
- name: snapshot.name,
247
- api: {
248
- getState: () => currentState,
249
- setState: (partial, replace) => {
250
- this.remoteSetStateHandler?.(snapshot.name, partial, replace);
251
- },
252
- subscribe: () => () => {}
253
- },
254
- stateChangeCount: snapshot.stateChangeCount,
255
- isPersisted: snapshot.isPersisted,
256
- persistName: snapshot.persistName,
257
- color: snapshot.color
258
- }];
259
- }));
260
- this.setItems(merged);
261
- this.notifyStoreListeners();
262
- }
263
-
264
- /**
265
- * Hydrate one mirrored change with real trees fetched on demand from the
266
- * device via the adapter's `getChangeDetail`. Remote-mirror (desktop) only.
267
- */
268
- hydrateChange(id, patch) {
269
- this.hydrateById(id, patch);
270
- }
271
-
272
- /**
273
- * Hydrate one mirrored store's currentState fetched on demand from the
274
- * device via the adapter's `getStoreState`. Remote-mirror (desktop) only.
275
- */
276
- hydrateStoreState(storeName, currentState) {
277
- const store = this.stores.get(storeName);
278
- if (!store) return;
279
- this.stores.set(storeName, {
280
- ...store,
281
- api: {
282
- ...store.api,
283
- getState: () => currentState
284
- }
285
- });
286
- this.notifyStoreListeners();
287
- }
288
-
289
- // ---- Store Registry ----
290
-
291
- registerStore(name, api, options) {
292
- if (this.stores.has(name)) return;
293
- const storeInfo = {
294
- name,
295
- api,
296
- stateChangeCount: 0,
297
- isPersisted: options?.isPersisted ?? false,
298
- persistName: options?.persistName,
299
- rehydrate: options?.rehydrate,
300
- color: getStoreColor(name)
301
- };
302
- this.stores.set(name, storeInfo);
303
- this.notifyStoreListeners();
304
- }
305
- unregisterStore(name) {
306
- this.stores.delete(name);
307
- this.notifyStoreListeners();
308
- }
309
- getStores() {
310
- return Array.from(this.stores.values());
311
- }
312
- getStore(name) {
313
- return this.stores.get(name);
314
- }
315
- getStoreColor(name) {
316
- return this.stores.get(name)?.color ?? getStoreColor(name);
317
- }
318
-
319
- // ---- Filtering ----
320
-
321
- filterStateChanges(filter) {
322
- let filtered = this.getAll();
323
- if (filter.searchText) {
324
- const search = filter.searchText.toLowerCase();
325
- filtered = filtered.filter(c => c.storeName.toLowerCase().includes(search) || c.partialPreview.toLowerCase().includes(search) || c.changedKeys.some(k => k.toLowerCase().includes(search)));
326
- }
327
- if (filter.storeNames && filter.storeNames.length > 0) {
328
- filtered = filtered.filter(c => filter.storeNames.includes(c.storeName));
329
- }
330
- if (filter.onlyWithChanges) {
331
- filtered = filtered.filter(c => c.hasStateChange);
332
- }
333
- return filtered;
334
- }
335
-
336
- // ---- Stats ----
337
-
338
- getStats() {
339
- const total = this.items.length;
340
- const withChanges = this.items.filter(c => c.hasStateChange).length;
341
- const storeCount = this.stores.size;
342
- const durations = this.items.filter(c => c.duration !== undefined).map(c => c.duration);
343
- const avgDuration = durations.length > 0 ? durations.reduce((sum, d) => sum + d, 0) / durations.length : 0;
344
- return {
345
- totalChanges: total,
346
- changesWithStateChange: withChanges,
347
- changesWithoutStateChange: total - withChanges,
348
- storeCount,
349
- averageDuration: Math.round(avgDuration * 100) / 100
350
- };
351
- }
352
- getUniqueStoreNames() {
353
- return Array.from(this.stores.keys()).sort();
354
- }
355
-
356
- // ---- Enable / Disable ----
357
-
358
- setMaxChanges(max) {
359
- this.setMax(max);
360
- }
361
-
362
- // ---- Subscriptions ----
363
-
364
- subscribeToStores(listener) {
365
- this.storeListeners.add(listener);
366
- return () => {
367
- this.storeListeners.delete(listener);
368
- };
369
- }
370
- notifyStoreListeners() {
371
- const stores = this.getStores();
372
- this.storeListeners.forEach(listener => listener(stores));
373
- }
374
- }
375
- export const zustandStateStore = new ZustandStateStore();
1
+ "use strict";import{ChangeLog as v,entityColor as L,formatValuePreview as w,shallowDiffSummary as x}from"@buoy-gg/shared-ui";const N={counter:"#10B981",auth:"#8B5CF6",user:"#3B82F6",cart:"#EC4899",app:"#6366F1",ui:"#F59E0B",settings:"#14B8A6",theme:"#06B6D4",navigation:"#F97316",form:"#EF4444"};function d(c){return L(c,N)}function b(c,t=40){return w(c,{maxLength:t,undefinedLabel:"",functionLabel:"(updater fn)"})}class P extends v{stores=new Map;storeListeners=new Set;remoteSetStateHandler=null;addStateChange(t){if(!this.canRecord())return;const{storeName:s,partial:r,replace:a,prevState:o,nextState:e,duration:n,category:i,isPersisted:h=false}=t;const u=o!==e;const{summary:g,changedKeys:l,changedCount:f}=x(o,e);const m=b(r);const C=(n??0)>16;const y=i??(a?"replace":"setState");const p={id:this.nextId(),storeName:s,timestamp:Date.now(),prevState:o,nextState:e,partial:r,duration:n,hasStateChange:u,replace:a,category:y,changedKeys:l,changedKeysCount:f,diffSummary:g,partialPreview:m,isSlowUpdate:C,isPersisted:h};this.push(p);const S=this.stores.get(s);if(S){S.stateChangeCount++}this.notifyListeners()}getStateChanges(){return this.getAll()}getStateChangeById(t){return this.getById(t)}clearStateChanges(){for(const t of this.stores.values()){t.stateChangeCount=0}this.clear();this.notifyStoreListeners()}replaceStateChanges(t){if(!this.getEnabled())return;for(const r of this.stores.values()){r.stateChangeCount=0}const s=t.slice(0,this.max);for(const r of s){const a=this.stores.get(r.storeName);if(a)a.stateChangeCount++}this.setItems(s);this.notifyStoreListeners()}getStoreSnapshots(){return Array.from(this.stores.values()).map(t=>{let s;try{s=t.api.getState()}catch{s=void 0}return{name:t.name,stateChangeCount:t.stateChangeCount,isPersisted:t.isPersisted,persistName:t.persistName,canRehydrate:typeof t.rehydrate==="function",color:t.color,currentState:s}})}setRemoteSetStateHandler(t){this.remoteSetStateHandler=t}replaceFromSnapshot(t,s){if(!this.getEnabled())return;const r=new Map;for(const e of this.items){const n=e.prevState;if(!n||n.__buoyStateOnDevice!==true){r.set(e.id,e)}}const a=t.slice(0,this.max).map(e=>{const n=r.get(e.id);const i=e.prevState;return n&&i?.__buoyStateOnDevice===true?{...e,prevState:n.prevState,nextState:n.nextState,partial:n.partial}:e});const o=new Map;for(const[e,n]of this.stores){try{const i=n.api.getState();const h=i;if(!h||h.__buoyStateOnDevice!==true){o.set(e,i)}}catch{}}this.stores=new Map(s.map(e=>{const n=e.currentState;const i=o.get(e.name);const h=i!==void 0&&n?.__buoyStateOnDevice===true?i:e.currentState;return[e.name,{name:e.name,api:{getState:()=>h,setState:(u,g)=>{this.remoteSetStateHandler?.(e.name,u,g)},subscribe:()=>()=>{}},stateChangeCount:e.stateChangeCount,isPersisted:e.isPersisted,persistName:e.persistName,color:e.color}]}));this.setItems(a);this.notifyStoreListeners()}hydrateChange(t,s){this.hydrateById(t,s)}hydrateStoreState(t,s){const r=this.stores.get(t);if(!r)return;this.stores.set(t,{...r,api:{...r.api,getState:()=>s}});this.notifyStoreListeners()}registerStore(t,s,r){if(this.stores.has(t))return;const a={name:t,api:s,stateChangeCount:0,isPersisted:r?.isPersisted??false,persistName:r?.persistName,rehydrate:r?.rehydrate,color:d(t)};this.stores.set(t,a);this.notifyStoreListeners()}unregisterStore(t){this.stores.delete(t);this.notifyStoreListeners()}getStores(){return Array.from(this.stores.values())}getStore(t){return this.stores.get(t)}getStoreColor(t){return this.stores.get(t)?.color??d(t)}filterStateChanges(t){let s=this.getAll();if(t.searchText){const r=t.searchText.toLowerCase();s=s.filter(a=>a.storeName.toLowerCase().includes(r)||a.partialPreview.toLowerCase().includes(r)||a.changedKeys.some(o=>o.toLowerCase().includes(r)))}if(t.storeNames&&t.storeNames.length>0){s=s.filter(r=>t.storeNames.includes(r.storeName))}if(t.onlyWithChanges){s=s.filter(r=>r.hasStateChange)}return s}getStats(){const t=this.items.length;const s=this.items.filter(e=>e.hasStateChange).length;const r=this.stores.size;const a=this.items.filter(e=>e.duration!==void 0).map(e=>e.duration);const o=a.length>0?a.reduce((e,n)=>e+n,0)/a.length:0;return{totalChanges:t,changesWithStateChange:s,changesWithoutStateChange:t-s,storeCount:r,averageDuration:Math.round(o*100)/100}}getUniqueStoreNames(){return Array.from(this.stores.keys()).sort()}setMaxChanges(t){this.setMax(t)}subscribeToStores(t){this.storeListeners.add(t);return()=>{this.storeListeners.delete(t)}}notifyStoreListeners(){const t=this.getStores();this.storeListeners.forEach(s=>s(t))}}export const zustandStateStore=new P;