@chromahq/store 1.0.10 → 1.0.11

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.
package/dist/index.cjs.js CHANGED
@@ -159,6 +159,8 @@ class BridgeStore {
159
159
  }
160
160
  }
161
161
  };
162
+ this.stateSyncSequence = 0;
163
+ this.pendingStateSync = false;
162
164
  this.notifyListeners = () => {
163
165
  if (!this.listeners) {
164
166
  console.warn("BridgeStore: listeners not initialized");
@@ -261,10 +263,21 @@ class BridgeStore {
261
263
  setupStateSync() {
262
264
  if (this.bridge.on) {
263
265
  this.bridge.on(`store:${this.storeName}:stateChanged`, () => {
266
+ if (this.pendingStateSync) {
267
+ return;
268
+ }
269
+ this.pendingStateSync = true;
270
+ const currentSequence = ++this.stateSyncSequence;
264
271
  this.bridge.send(`store:${this.storeName}:getState`).then((newState) => {
265
- this.previousState = this.currentState;
266
- this.currentState = newState;
267
- this.notifyListeners();
272
+ if (currentSequence === this.stateSyncSequence) {
273
+ this.previousState = this.currentState;
274
+ this.currentState = newState;
275
+ this.notifyListeners();
276
+ }
277
+ }).catch((error) => {
278
+ console.error(`BridgeStore[${this.storeName}]: Failed to sync state:`, error);
279
+ }).finally(() => {
280
+ this.pendingStateSync = false;
268
281
  });
269
282
  });
270
283
  } else {
@@ -502,12 +515,34 @@ function autoRegisterStoreHandlers(store, storeName = "default") {
502
515
  if (!store) {
503
516
  throw new Error("autoRegisterStoreHandlers: store parameter is required");
504
517
  }
518
+ if (typeof store.getState !== "function") {
519
+ throw new Error("autoRegisterStoreHandlers: store must have getState method");
520
+ }
521
+ if (typeof store.setState !== "function") {
522
+ throw new Error("autoRegisterStoreHandlers: store must have setState method");
523
+ }
524
+ console.log(`[Store] Creating handlers for store "${storeName}"`, {
525
+ hasGetState: typeof store.getState === "function",
526
+ hasSetState: typeof store.setState === "function"
527
+ });
505
528
  class AutoGetStoreStateMessage {
506
529
  handle() {
530
+ console.log(`[Store] GetState handler called for "${storeName}"`);
507
531
  if (!store) {
532
+ console.error(`[Store] Store instance not available for "${storeName}"`);
508
533
  throw new Error("Store instance not available");
509
534
  }
510
- return store.getState();
535
+ try {
536
+ const state = store.getState();
537
+ console.log(`[Store] GetState returning state for "${storeName}"`, {
538
+ hasState: state !== void 0 && state !== null,
539
+ stateType: typeof state
540
+ });
541
+ return state;
542
+ } catch (error) {
543
+ console.error(`[Store] GetState failed for "${storeName}":`, error);
544
+ throw error;
545
+ }
511
546
  }
512
547
  }
513
548
  class AutoSetStoreStateMessage {
@@ -546,9 +581,28 @@ function autoRegisterStoreHandlers(store, storeName = "default") {
546
581
  return updatedState;
547
582
  }
548
583
  }
584
+ class AutoResetStoreMessage {
585
+ handle() {
586
+ console.log(`[Store] Reset handler called for "${storeName}"`);
587
+ if (!store) {
588
+ console.error(`[Store] Store instance not available for "${storeName}"`);
589
+ throw new Error("Store instance not available");
590
+ }
591
+ try {
592
+ if (typeof store.reset === "function") {
593
+ store.reset();
594
+ }
595
+ return store.getState();
596
+ } catch (error) {
597
+ console.error(`[Store] Reset failed for "${storeName}":`, error);
598
+ throw error;
599
+ }
600
+ }
601
+ }
549
602
  return {
550
603
  GetStoreStateMessage: AutoGetStoreStateMessage,
551
- SetStoreStateMessage: AutoSetStoreStateMessage
604
+ SetStoreStateMessage: AutoSetStoreStateMessage,
605
+ ResetStoreMessage: AutoResetStoreMessage
552
606
  };
553
607
  }
554
608
 
package/dist/index.d.ts CHANGED
@@ -70,6 +70,8 @@ declare class BridgeStore<T> implements CentralStore<T> {
70
70
  private readonly maxInitializationAttempts;
71
71
  constructor(bridge: BridgeWithEvents, initialState?: T, storeName?: string, readyCallbacks?: Set<() => void>);
72
72
  initialize: () => Promise<void>;
73
+ private stateSyncSequence;
74
+ private pendingStateSync;
73
75
  private setupStateSync;
74
76
  private notifyListeners;
75
77
  getState: () => T;
package/dist/index.es.js CHANGED
@@ -139,6 +139,8 @@ class BridgeStore {
139
139
  }
140
140
  }
141
141
  };
142
+ this.stateSyncSequence = 0;
143
+ this.pendingStateSync = false;
142
144
  this.notifyListeners = () => {
143
145
  if (!this.listeners) {
144
146
  console.warn("BridgeStore: listeners not initialized");
@@ -241,10 +243,21 @@ class BridgeStore {
241
243
  setupStateSync() {
242
244
  if (this.bridge.on) {
243
245
  this.bridge.on(`store:${this.storeName}:stateChanged`, () => {
246
+ if (this.pendingStateSync) {
247
+ return;
248
+ }
249
+ this.pendingStateSync = true;
250
+ const currentSequence = ++this.stateSyncSequence;
244
251
  this.bridge.send(`store:${this.storeName}:getState`).then((newState) => {
245
- this.previousState = this.currentState;
246
- this.currentState = newState;
247
- this.notifyListeners();
252
+ if (currentSequence === this.stateSyncSequence) {
253
+ this.previousState = this.currentState;
254
+ this.currentState = newState;
255
+ this.notifyListeners();
256
+ }
257
+ }).catch((error) => {
258
+ console.error(`BridgeStore[${this.storeName}]: Failed to sync state:`, error);
259
+ }).finally(() => {
260
+ this.pendingStateSync = false;
248
261
  });
249
262
  });
250
263
  } else {
@@ -482,12 +495,34 @@ function autoRegisterStoreHandlers(store, storeName = "default") {
482
495
  if (!store) {
483
496
  throw new Error("autoRegisterStoreHandlers: store parameter is required");
484
497
  }
498
+ if (typeof store.getState !== "function") {
499
+ throw new Error("autoRegisterStoreHandlers: store must have getState method");
500
+ }
501
+ if (typeof store.setState !== "function") {
502
+ throw new Error("autoRegisterStoreHandlers: store must have setState method");
503
+ }
504
+ console.log(`[Store] Creating handlers for store "${storeName}"`, {
505
+ hasGetState: typeof store.getState === "function",
506
+ hasSetState: typeof store.setState === "function"
507
+ });
485
508
  class AutoGetStoreStateMessage {
486
509
  handle() {
510
+ console.log(`[Store] GetState handler called for "${storeName}"`);
487
511
  if (!store) {
512
+ console.error(`[Store] Store instance not available for "${storeName}"`);
488
513
  throw new Error("Store instance not available");
489
514
  }
490
- return store.getState();
515
+ try {
516
+ const state = store.getState();
517
+ console.log(`[Store] GetState returning state for "${storeName}"`, {
518
+ hasState: state !== void 0 && state !== null,
519
+ stateType: typeof state
520
+ });
521
+ return state;
522
+ } catch (error) {
523
+ console.error(`[Store] GetState failed for "${storeName}":`, error);
524
+ throw error;
525
+ }
491
526
  }
492
527
  }
493
528
  class AutoSetStoreStateMessage {
@@ -526,9 +561,28 @@ function autoRegisterStoreHandlers(store, storeName = "default") {
526
561
  return updatedState;
527
562
  }
528
563
  }
564
+ class AutoResetStoreMessage {
565
+ handle() {
566
+ console.log(`[Store] Reset handler called for "${storeName}"`);
567
+ if (!store) {
568
+ console.error(`[Store] Store instance not available for "${storeName}"`);
569
+ throw new Error("Store instance not available");
570
+ }
571
+ try {
572
+ if (typeof store.reset === "function") {
573
+ store.reset();
574
+ }
575
+ return store.getState();
576
+ } catch (error) {
577
+ console.error(`[Store] Reset failed for "${storeName}":`, error);
578
+ throw error;
579
+ }
580
+ }
581
+ }
529
582
  return {
530
583
  GetStoreStateMessage: AutoGetStoreStateMessage,
531
- SetStoreStateMessage: AutoSetStoreStateMessage
584
+ SetStoreStateMessage: AutoSetStoreStateMessage,
585
+ ResetStoreMessage: AutoResetStoreMessage
532
586
  };
533
587
  }
534
588
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromahq/store",
3
- "version": "1.0.10",
3
+ "version": "1.0.11",
4
4
  "description": "Centralized, persistent store for Chrome extensions using zustand, accessible from service workers and React, with chrome.storage.local persistence.",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs.js",