@classic-homes/offline-core 0.1.5 → 0.3.0

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.
@@ -1,4 +1,4 @@
1
- import { g as StoredDraftV2 } from '../types-Ho1JiLQR.js';
1
+ import { g as StoredDraftV2 } from '../types-CRMTQnjY.js';
2
2
  import { S as StorageAdapter } from '../types-DDhQm0hc.js';
3
3
 
4
4
  /**
@@ -48,6 +48,12 @@ interface DraftPayload {
48
48
  updatedAt: number;
49
49
  /** Device ID */
50
50
  deviceId: string;
51
+ /**
52
+ * Local draft ID. Stable across retries of the same draft, so a server can
53
+ * use it as an idempotency key on create: a retried create whose first
54
+ * response was lost should return the existing draft, not a duplicate.
55
+ */
56
+ localId?: string;
51
57
  }
52
58
  /**
53
59
  * Draft data returned from the server
@@ -116,27 +122,43 @@ interface SyncResponse {
116
122
  /**
117
123
  * Sync adapter interface - implement this to connect to your backend
118
124
  */
125
+ /**
126
+ * Per-call options the sync service passes to a {@link SyncAdapter}.
127
+ */
128
+ interface SyncAdapterCallOptions {
129
+ /**
130
+ * Aborted when the sync service is disposed (logout, account switch,
131
+ * unmount). Pass it to `fetch` so the request is cancelled; the service
132
+ * ignores the call's result after disposal either way.
133
+ */
134
+ signal?: AbortSignal;
135
+ }
119
136
  interface SyncAdapter {
120
137
  /**
121
- * Fetch all drafts for the current user
138
+ * Fetch all drafts for the current user.
139
+ *
140
+ * Must return the **complete** list, not a page or a partial result: the
141
+ * sync service treats a previously synced draft missing from this list as
142
+ * deleted on the server and deletes the local copy (unless it was edited
143
+ * since the sync started). Throw instead of returning a partial list.
122
144
  */
123
- fetchDrafts(): Promise<RemoteDraft[]>;
145
+ fetchDrafts(options?: SyncAdapterCallOptions): Promise<RemoteDraft[]>;
124
146
  /**
125
147
  * Fetch drafts changed since a timestamp
126
148
  */
127
- fetchChanges(since: number): Promise<RemoteDraft[]>;
149
+ fetchChanges(since: number, options?: SyncAdapterCallOptions): Promise<RemoteDraft[]>;
128
150
  /**
129
151
  * Create a new draft on the server
130
152
  */
131
- createDraft(draft: DraftPayload): Promise<RemoteDraft>;
153
+ createDraft(draft: DraftPayload, options?: SyncAdapterCallOptions): Promise<RemoteDraft>;
132
154
  /**
133
155
  * Update an existing draft on the server
134
156
  */
135
- updateDraft(remoteId: string, draft: DraftPayload): Promise<RemoteDraft>;
157
+ updateDraft(remoteId: string, draft: DraftPayload, options?: SyncAdapterCallOptions): Promise<RemoteDraft>;
136
158
  /**
137
159
  * Delete a draft from the server
138
160
  */
139
- deleteDraft(remoteId: string): Promise<void>;
161
+ deleteDraft(remoteId: string, options?: SyncAdapterCallOptions): Promise<void>;
140
162
  /**
141
163
  * Batch sync operation (optional, for efficiency)
142
164
  */
@@ -163,11 +185,24 @@ interface SyncConfig {
163
185
  /** Callback for sync errors */
164
186
  onSyncError?: (error: Error) => void;
165
187
  }
188
+ /**
189
+ * A single draft that failed to sync. The rest of the sync still runs.
190
+ */
191
+ interface SyncFailure {
192
+ /** What was being done */
193
+ operation: 'upload' | 'download' | 'delete';
194
+ /** Local draft ID (absent for a failed download of a new remote draft) */
195
+ draftId?: string;
196
+ /** Remote draft ID, if known */
197
+ remoteId?: string;
198
+ /** Error message */
199
+ error: string;
200
+ }
166
201
  /**
167
202
  * Result of a sync operation
168
203
  */
169
204
  interface SyncResult {
170
- /** Whether sync was successful */
205
+ /** Whether sync was successful (false if any draft failed; see `failures`) */
171
206
  success: boolean;
172
207
  /** Number of drafts uploaded */
173
208
  uploaded: number;
@@ -181,6 +216,11 @@ interface SyncResult {
181
216
  conflicts: DraftConflict[];
182
217
  /** Error message if failed */
183
218
  error?: string;
219
+ /**
220
+ * Drafts that failed while the rest synced. A failed upload is retried on
221
+ * later syncs with exponential backoff.
222
+ */
223
+ failures?: SyncFailure[];
184
224
  /** Timestamp of sync */
185
225
  timestamp: number;
186
226
  /** Duration in milliseconds */
@@ -503,6 +543,17 @@ declare class SyncService {
503
543
  private conflicts;
504
544
  private eventListeners;
505
545
  private connectivityUnsubscribe;
546
+ private pendingDraftCount;
547
+ /** Upload backoff per local draft ID */
548
+ private uploadFailures;
549
+ /**
550
+ * Aborted by dispose(). Passed to the remote adapter, raced against every
551
+ * remote call and checked after every await of a sync, so a disposed
552
+ * service writes nothing more and its sync lock is released promptly.
553
+ */
554
+ private readonly lifetime;
555
+ /** Settles once the sync running under the lock (if any) has stopped */
556
+ private activeSync;
506
557
  private onSyncComplete?;
507
558
  private onConflictDetected?;
508
559
  private onOnlineStatusChange?;
@@ -512,6 +563,17 @@ declare class SyncService {
512
563
  * Initialize the sync service
513
564
  */
514
565
  initialize(): Promise<void>;
566
+ /** Whether dispose() has been called */
567
+ get isDisposed(): boolean;
568
+ /** Throw if disposed: called after every await of a sync. */
569
+ private ensureActive;
570
+ /**
571
+ * Run a remote adapter call with the service's AbortSignal, settling as
572
+ * soon as the service is disposed even if the adapter ignores the signal.
573
+ */
574
+ private remote;
575
+ /** Run `fn` under the shared sync lock, tracked so dispose() can await it. */
576
+ private locked;
515
577
  /**
516
578
  * Check if currently online
517
579
  */
@@ -525,9 +587,15 @@ declare class SyncService {
525
587
  */
526
588
  get lastSyncAt(): number | null;
527
589
  /**
528
- * Get pending sync count
590
+ * Number of local drafts waiting to be uploaded ('local-only' or
591
+ * 'pending-upload'), as of the last sync, `initialize()` or
592
+ * `refreshPendingCount()`
529
593
  */
530
594
  get pendingCount(): number;
595
+ /**
596
+ * Recount the local drafts waiting to be uploaded
597
+ */
598
+ refreshPendingCount(): Promise<number>;
531
599
  /**
532
600
  * Get current conflicts
533
601
  */
@@ -540,10 +608,12 @@ declare class SyncService {
540
608
  * Sync all drafts
541
609
  */
542
610
  syncAll(): Promise<SyncResult>;
611
+ private runSyncAll;
543
612
  /**
544
613
  * Sync a specific draft
545
614
  */
546
615
  syncDraft(draftId: string): Promise<SyncResult>;
616
+ private runSyncDraft;
547
617
  /**
548
618
  * Force push a draft (overwrite remote)
549
619
  */
@@ -577,19 +647,44 @@ declare class SyncService {
577
647
  */
578
648
  private emit;
579
649
  /**
580
- * Dispose the sync service
650
+ * Dispose the sync service.
651
+ *
652
+ * Stops auto-sync and connectivity listening, drops every callback and
653
+ * event listener, and cancels an in-flight sync: it writes nothing more to
654
+ * local storage, its remote calls are aborted (the adapter receives an
655
+ * AbortSignal) and it releases the sync lock. The returned promise settles
656
+ * once that sync has stopped and the lock is free, so a caller switching
657
+ * accounts can `await` it before starting the next account's service.
658
+ * Calling it without awaiting is fine; it is idempotent.
581
659
  */
582
- dispose(): void;
660
+ dispose(): Promise<void>;
583
661
  /**
584
- * Perform the actual sync
662
+ * Perform the actual sync. One draft failing does not stop the rest: each
663
+ * failure is recorded in `failures` and the sync carries on.
585
664
  */
586
665
  private performSync;
666
+ /**
667
+ * Whether a failing draft is still waiting out its retry delay
668
+ */
669
+ private isBackedOff;
670
+ /**
671
+ * Record a failed upload and schedule its next attempt (exponential backoff)
672
+ */
673
+ private recordUploadFailure;
587
674
  /**
588
675
  * Sync a single draft
589
676
  */
590
677
  private syncSingleDraft;
591
678
  /**
592
- * Download a remote draft
679
+ * Mark an uploaded draft as synced, but only if it was not edited while the
680
+ * upload was in flight (compare-and-set on `version`). An edited draft stays
681
+ * pending so the edit is uploaded next time; its remote ID is still
682
+ * recorded so that upload updates the remote copy instead of creating another.
683
+ */
684
+ private markUploaded;
685
+ /**
686
+ * Download a remote draft. Into an existing local draft only if that draft
687
+ * is unchanged since it was read; returns false when a local edit won.
593
688
  */
594
689
  private downloadRemoteDraft;
595
690
  /**
@@ -618,4 +713,4 @@ declare class SyncService {
618
713
  */
619
714
  declare function createSyncService(adapter: StorageAdapter, config: SyncConfig): SyncService;
620
715
 
621
- export { type ConflictResolution, ConnectivityMonitor, type ConnectivityState, type DraftConflict, type DraftPayload, type PendingSyncOperation, type RemoteDraft, type SyncAdapter, type SyncConfig, SyncConflictError, type SyncEvent, type SyncEventListener, type SyncEventType, SyncQueue, type SyncQueueState, type SyncRequest, type SyncResponse, type SyncResult, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge };
716
+ export { type ConflictResolution, ConnectivityMonitor, type ConnectivityState, type DraftConflict, type DraftPayload, type PendingSyncOperation, type RemoteDraft, type SyncAdapter, type SyncAdapterCallOptions, type SyncConfig, SyncConflictError, type SyncEvent, type SyncEventListener, type SyncEventType, type SyncFailure, SyncQueue, type SyncQueueState, type SyncRequest, type SyncResponse, type SyncResult, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge };
@@ -1,3 +1,3 @@
1
- export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge } from '../chunk-DIXYWY34.js';
2
- import '../chunk-WWKATW37.js';
3
- import '../chunk-ZC7F2HDV.js';
1
+ export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge } from '../chunk-NCAGT2LL.js';
2
+ import '../chunk-MA4ZHPQS.js';
3
+ import '../chunk-QS7N4PTV.js';
@@ -40,8 +40,13 @@ interface DraftMetadata {
40
40
  syncStatus: DraftSyncStatus;
41
41
  /** Server-side ID (null if never synced) */
42
42
  remoteId: string | null;
43
- /** Version number for conflict detection */
43
+ /** Version number for conflict detection. Bumped by local edits only, not by metadata-only updates */
44
44
  version: number;
45
+ /**
46
+ * The `version` last confirmed in sync with the server. A draft whose
47
+ * `version` differs has local edits that have not been uploaded.
48
+ */
49
+ syncedVersion?: number;
45
50
  /** Timestamp of last successful sync */
46
51
  lastSyncedAt: number | null;
47
52
  /** Device ID where draft was created/modified */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@classic-homes/offline-core",
3
- "version": "0.1.5",
3
+ "version": "0.3.0",
4
4
  "description": "Framework-agnostic offline persistence and caching for Classic Theme",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",