@chromahq/store 1.0.12 → 1.0.14

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
@@ -213,14 +213,30 @@ class BridgeStore {
213
213
  };
214
214
  this.reset = () => {
215
215
  if (this.initialState !== null) {
216
- this.bridge.send(`store:${this.storeName}:reset`).catch((error) => {
217
- console.error("Failed to reset state via bridge:", error);
218
- });
216
+ if (!this.bridge.isConnected) {
217
+ console.warn(
218
+ `BridgeStore[${this.storeName}]: Bridge disconnected, reset applied locally only`
219
+ );
220
+ this.previousState = this.currentState;
221
+ this.currentState = { ...this.initialState };
222
+ this.notifyListeners();
223
+ return;
224
+ }
225
+ const stateBeforeReset = this.currentState ? { ...this.currentState } : null;
219
226
  this.previousState = this.currentState;
220
227
  this.currentState = { ...this.initialState };
221
228
  this.notifyListeners();
229
+ this.bridge.send(`store:${this.storeName}:reset`).catch((error) => {
230
+ console.error(`BridgeStore[${this.storeName}]: Failed to reset state via bridge:`, error);
231
+ if (stateBeforeReset !== null) {
232
+ console.warn(`BridgeStore[${this.storeName}]: Rolling back reset due to bridge error`);
233
+ this.previousState = this.currentState;
234
+ this.currentState = stateBeforeReset;
235
+ this.notifyListeners();
236
+ }
237
+ });
222
238
  } else {
223
- console.warn("BridgeStore: Cannot reset, initial state not available");
239
+ console.warn(`BridgeStore[${this.storeName}]: Cannot reset, initial state not available`);
224
240
  }
225
241
  };
226
242
  this.notifyReady = () => {
@@ -295,10 +311,29 @@ class BridgeStore {
295
311
  } else {
296
312
  actualUpdate = partial;
297
313
  }
314
+ if (!this.bridge.isConnected) {
315
+ console.warn(
316
+ `BridgeStore[${this.storeName}]: Bridge disconnected, state update queued locally only`
317
+ );
318
+ this.applyOptimisticUpdate(actualUpdate, replace);
319
+ return;
320
+ }
321
+ const stateBeforeUpdate = this.currentState ? { ...this.currentState } : null;
322
+ this.applyOptimisticUpdate(actualUpdate, replace);
298
323
  const payload = { partial: actualUpdate, replace };
299
324
  this.bridge.send(`store:${this.storeName}:setState`, payload).catch((error) => {
300
- console.error("Failed to update state via bridge:", error);
325
+ console.error(`BridgeStore[${this.storeName}]: Failed to update state via bridge:`, error);
326
+ if (stateBeforeUpdate !== null) {
327
+ console.warn(
328
+ `BridgeStore[${this.storeName}]: Rolling back optimistic update due to bridge error`
329
+ );
330
+ this.previousState = this.currentState;
331
+ this.currentState = stateBeforeUpdate;
332
+ this.notifyListeners();
333
+ }
301
334
  });
335
+ }
336
+ applyOptimisticUpdate(actualUpdate, replace) {
302
337
  if (this.currentState) {
303
338
  this.previousState = this.currentState;
304
339
  if (replace) {
package/dist/index.d.ts CHANGED
@@ -77,6 +77,7 @@ declare class BridgeStore<T> implements CentralStore<T> {
77
77
  getState: () => T;
78
78
  setState(partial: T | Partial<T> | ((state: T) => T | Partial<T>), replace?: false): void;
79
79
  setState(state: T | ((state: T) => T), replace: true): void;
80
+ private applyOptimisticUpdate;
80
81
  subscribe: (listener: (state: T, prevState: T) => void) => (() => void);
81
82
  destroy: () => void;
82
83
  getInitialState: () => T;
package/dist/index.es.js CHANGED
@@ -193,14 +193,30 @@ class BridgeStore {
193
193
  };
194
194
  this.reset = () => {
195
195
  if (this.initialState !== null) {
196
- this.bridge.send(`store:${this.storeName}:reset`).catch((error) => {
197
- console.error("Failed to reset state via bridge:", error);
198
- });
196
+ if (!this.bridge.isConnected) {
197
+ console.warn(
198
+ `BridgeStore[${this.storeName}]: Bridge disconnected, reset applied locally only`
199
+ );
200
+ this.previousState = this.currentState;
201
+ this.currentState = { ...this.initialState };
202
+ this.notifyListeners();
203
+ return;
204
+ }
205
+ const stateBeforeReset = this.currentState ? { ...this.currentState } : null;
199
206
  this.previousState = this.currentState;
200
207
  this.currentState = { ...this.initialState };
201
208
  this.notifyListeners();
209
+ this.bridge.send(`store:${this.storeName}:reset`).catch((error) => {
210
+ console.error(`BridgeStore[${this.storeName}]: Failed to reset state via bridge:`, error);
211
+ if (stateBeforeReset !== null) {
212
+ console.warn(`BridgeStore[${this.storeName}]: Rolling back reset due to bridge error`);
213
+ this.previousState = this.currentState;
214
+ this.currentState = stateBeforeReset;
215
+ this.notifyListeners();
216
+ }
217
+ });
202
218
  } else {
203
- console.warn("BridgeStore: Cannot reset, initial state not available");
219
+ console.warn(`BridgeStore[${this.storeName}]: Cannot reset, initial state not available`);
204
220
  }
205
221
  };
206
222
  this.notifyReady = () => {
@@ -275,10 +291,29 @@ class BridgeStore {
275
291
  } else {
276
292
  actualUpdate = partial;
277
293
  }
294
+ if (!this.bridge.isConnected) {
295
+ console.warn(
296
+ `BridgeStore[${this.storeName}]: Bridge disconnected, state update queued locally only`
297
+ );
298
+ this.applyOptimisticUpdate(actualUpdate, replace);
299
+ return;
300
+ }
301
+ const stateBeforeUpdate = this.currentState ? { ...this.currentState } : null;
302
+ this.applyOptimisticUpdate(actualUpdate, replace);
278
303
  const payload = { partial: actualUpdate, replace };
279
304
  this.bridge.send(`store:${this.storeName}:setState`, payload).catch((error) => {
280
- console.error("Failed to update state via bridge:", error);
305
+ console.error(`BridgeStore[${this.storeName}]: Failed to update state via bridge:`, error);
306
+ if (stateBeforeUpdate !== null) {
307
+ console.warn(
308
+ `BridgeStore[${this.storeName}]: Rolling back optimistic update due to bridge error`
309
+ );
310
+ this.previousState = this.currentState;
311
+ this.currentState = stateBeforeUpdate;
312
+ this.notifyListeners();
313
+ }
281
314
  });
315
+ }
316
+ applyOptimisticUpdate(actualUpdate, replace) {
282
317
  if (this.currentState) {
283
318
  this.previousState = this.currentState;
284
319
  if (replace) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chromahq/store",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
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",