@classic-homes/offline-core 0.3.0 → 0.3.1

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.
@@ -169,10 +169,16 @@ function createConnectivityMonitor() {
169
169
  // src/sync/queue.ts
170
170
  var QUEUE_STORAGE_KEY = "sync-queue";
171
171
  var MAX_RETRY_COUNT = 3;
172
+ var lastWriteByAdapter = /* @__PURE__ */ new WeakMap();
172
173
  var SyncQueue = class {
173
174
  constructor(adapter) {
174
175
  this.adapter = adapter;
175
- this.saveTimeout = null;
176
+ /** Write currently being performed by the adapter */
177
+ this.activeSave = null;
178
+ /** Write scheduled after `activeSave`, not started yet; later mutations join it */
179
+ this.queuedSave = null;
180
+ /** The latest write failed, so storage is behind the in-memory state */
181
+ this.unsaved = false;
176
182
  this.state = {
177
183
  pending: [],
178
184
  processing: [],
@@ -184,6 +190,7 @@ var SyncQueue = class {
184
190
  * Initialize the queue from storage
185
191
  */
186
192
  async initialize() {
193
+ await lastWriteByAdapter.get(this.adapter)?.catch(() => void 0);
187
194
  try {
188
195
  const entry = await this.adapter.getJSON(QUEUE_STORAGE_KEY);
189
196
  if (entry?.data) {
@@ -199,21 +206,35 @@ var SyncQueue = class {
199
206
  }
200
207
  }
201
208
  /**
202
- * Save queue state to storage
209
+ * Persist queue state. Resolves once the adapter has stored a snapshot that
210
+ * includes every mutation made before the call, and rejects if that write
211
+ * fails. Writes are serialised; calls made while a write is in flight share
212
+ * the next one, so bursts cost at most two writes.
203
213
  */
204
- async save() {
205
- if (this.saveTimeout) {
206
- clearTimeout(this.saveTimeout);
207
- }
208
- this.saveTimeout = setTimeout(async () => {
209
- try {
210
- await this.adapter.storeJSON(QUEUE_STORAGE_KEY, this.state, {
211
- tags: ["sync-queue"]
212
- });
213
- } catch (error) {
214
- console.warn("[SyncQueue] Failed to save queue state:", error);
214
+ save() {
215
+ if (this.queuedSave) return this.queuedSave;
216
+ const previous = this.activeSave;
217
+ const write = (previous ?? Promise.resolve()).catch(() => void 0).then(() => {
218
+ this.queuedSave = null;
219
+ this.activeSave = write;
220
+ return this.adapter.storeJSON(QUEUE_STORAGE_KEY, structuredClone(this.state), {
221
+ tags: ["sync-queue"]
222
+ });
223
+ });
224
+ this.queuedSave = write;
225
+ lastWriteByAdapter.set(this.adapter, write);
226
+ const settle = (failed) => {
227
+ if (this.activeSave === write) {
228
+ this.activeSave = null;
229
+ if (!this.queuedSave) this.unsaved = failed;
215
230
  }
216
- }, 100);
231
+ if (lastWriteByAdapter.get(this.adapter) === write) lastWriteByAdapter.delete(this.adapter);
232
+ };
233
+ write.then(
234
+ () => settle(false),
235
+ () => settle(true)
236
+ );
237
+ return write;
217
238
  }
218
239
  /**
219
240
  * Queue a create operation
@@ -428,12 +449,16 @@ var SyncQueue = class {
428
449
  await this.save();
429
450
  }
430
451
  /**
431
- * Dispose the queue
452
+ * Dispose the queue, flushing any write still in flight (or retrying one
453
+ * that failed). The returned promise rejects if that write fails; callers
454
+ * that do not await it are unaffected (the rejection is marked handled), and
455
+ * a new queue on the same adapter still waits for the write before loading.
432
456
  */
433
457
  dispose() {
434
- if (this.saveTimeout) {
435
- clearTimeout(this.saveTimeout);
436
- }
458
+ const pending = this.queuedSave ?? this.activeSave ?? (this.unsaved ? this.save() : null);
459
+ const flushed = pending ? pending.then(() => void 0) : Promise.resolve();
460
+ flushed.catch(() => void 0);
461
+ return flushed;
437
462
  }
438
463
  };
439
464
  function createSyncQueue(adapter) {
package/dist/index.js CHANGED
@@ -2,6 +2,6 @@ export { CACHE_PREFIX, CacheService, createCacheService, detectStorageAdapter, g
2
2
  export { OfflineStorageError, createAdapter, detectAvailableAdapters, detectBrowser, isBrowser, isSSR, isWorker, supportsIndexedDB, supportsLocalStorage, supportsOPFS } from './chunk-4KL3GDYP.js';
3
3
  export { FileStorageService, createFileStorageService } from './chunk-R2IP2AA5.js';
4
4
  export { DraftStorageService, createDraftStorageService } from './chunk-UL7MZBSF.js';
5
- export { ConnectivityMonitor, SyncQueue, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './chunk-NCAGT2LL.js';
5
+ export { ConnectivityMonitor, SyncQueue, SyncService, applyResolution, compareDrafts, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict } from './chunk-YTJQYSHR.js';
6
6
  export { DRAFT_PREFIX, DRAFT_V2_PREFIX, DraftManagerService, createDraftManagerService, generateDraftId, getOrCreateDeviceId } from './chunk-MA4ZHPQS.js';
7
7
  export { CleanupScheduler, DEFAULT_EXPIRATIONS, calculateExpiration, isExpired } from './chunk-QS7N4PTV.js';
@@ -377,14 +377,22 @@ declare function createConnectivityMonitor(): ConnectivityMonitor;
377
377
  declare class SyncQueue {
378
378
  private readonly adapter;
379
379
  private state;
380
- private saveTimeout;
380
+ /** Write currently being performed by the adapter */
381
+ private activeSave;
382
+ /** Write scheduled after `activeSave`, not started yet; later mutations join it */
383
+ private queuedSave;
384
+ /** The latest write failed, so storage is behind the in-memory state */
385
+ private unsaved;
381
386
  constructor(adapter: StorageAdapter);
382
387
  /**
383
388
  * Initialize the queue from storage
384
389
  */
385
390
  initialize(): Promise<void>;
386
391
  /**
387
- * Save queue state to storage
392
+ * Persist queue state. Resolves once the adapter has stored a snapshot that
393
+ * includes every mutation made before the call, and rejects if that write
394
+ * fails. Writes are serialised; calls made while a write is in flight share
395
+ * the next one, so bursts cost at most two writes.
388
396
  */
389
397
  private save;
390
398
  /**
@@ -472,9 +480,12 @@ declare class SyncQueue {
472
480
  */
473
481
  clearAll(): Promise<void>;
474
482
  /**
475
- * Dispose the queue
483
+ * Dispose the queue, flushing any write still in flight (or retrying one
484
+ * that failed). The returned promise rejects if that write fails; callers
485
+ * that do not await it are unaffected (the rejection is marked handled), and
486
+ * a new queue on the same adapter still waits for the write before loading.
476
487
  */
477
- dispose(): void;
488
+ dispose(): Promise<void>;
478
489
  }
479
490
  /**
480
491
  * Create a sync queue
@@ -1,3 +1,3 @@
1
- export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge } from '../chunk-NCAGT2LL.js';
1
+ export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge } from '../chunk-YTJQYSHR.js';
2
2
  import '../chunk-MA4ZHPQS.js';
3
3
  import '../chunk-QS7N4PTV.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/offline-core",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "Framework-agnostic offline persistence and caching for Classic Theme",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",