@classic-homes/offline-core 0.1.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.
@@ -0,0 +1,621 @@
1
+ import { e as StoredDraftV2 } from '../types-B2FJpQuN.js';
2
+ import { S as StorageAdapter } from '../types-CWuauHAr.js';
3
+
4
+ /**
5
+ * Types for cloud sync functionality
6
+ */
7
+
8
+ /**
9
+ * Error thrown when a sync conflict is detected (HTTP 409)
10
+ */
11
+ declare class SyncConflictError extends Error {
12
+ readonly remoteVersion?: number | undefined;
13
+ readonly status: 409;
14
+ constructor(message: string, remoteVersion?: number | undefined);
15
+ }
16
+ /**
17
+ * Check if an error is a sync conflict error
18
+ */
19
+ declare function isSyncConflictError(error: unknown): error is SyncConflictError;
20
+ /**
21
+ * Network connectivity state
22
+ */
23
+ interface ConnectivityState {
24
+ /** Whether the device is online */
25
+ isOnline: boolean;
26
+ /** Type of connection (if available) */
27
+ connectionType?: 'wifi' | 'cellular' | 'ethernet' | 'unknown';
28
+ /** Whether the connection is metered */
29
+ isMetered?: boolean;
30
+ /** Timestamp of last connectivity check */
31
+ lastCheckedAt: number;
32
+ }
33
+ /**
34
+ * Payload sent to the server when syncing a draft
35
+ */
36
+ interface DraftPayload {
37
+ /** Form type identifier */
38
+ formKey: string;
39
+ /** User-defined name */
40
+ name: string;
41
+ /** Form data */
42
+ data: Record<string, unknown>;
43
+ /** File references */
44
+ fileRefs: Record<string, string[]>;
45
+ /** Version for conflict detection */
46
+ version: number;
47
+ /** Last update timestamp */
48
+ updatedAt: number;
49
+ /** Device ID */
50
+ deviceId: string;
51
+ }
52
+ /**
53
+ * Draft data returned from the server
54
+ */
55
+ interface RemoteDraft {
56
+ /** Server-side ID */
57
+ id: string;
58
+ /** Form type identifier */
59
+ formKey: string;
60
+ /** User-defined name */
61
+ name: string;
62
+ /** Form data */
63
+ data: Record<string, unknown>;
64
+ /** File references */
65
+ fileRefs: Record<string, string[]>;
66
+ /** Version number */
67
+ version: number;
68
+ /** Server timestamp */
69
+ updatedAt: number;
70
+ /** Creation timestamp */
71
+ createdAt: number;
72
+ /** Device ID of last modifier */
73
+ deviceId: string;
74
+ }
75
+ /**
76
+ * Batch sync request
77
+ */
78
+ interface SyncRequest {
79
+ /** Drafts to create */
80
+ create: DraftPayload[];
81
+ /** Drafts to update (remoteId -> payload) */
82
+ update: Array<{
83
+ remoteId: string;
84
+ payload: DraftPayload;
85
+ }>;
86
+ /** Drafts to delete (remoteIds) */
87
+ delete: string[];
88
+ /** Timestamp of last successful sync */
89
+ lastSyncAt: number | null;
90
+ }
91
+ /**
92
+ * Batch sync response
93
+ */
94
+ interface SyncResponse {
95
+ /** Created drafts (maps local temp ID to server ID) */
96
+ created: Array<{
97
+ tempId?: string;
98
+ draft: RemoteDraft;
99
+ }>;
100
+ /** Updated drafts */
101
+ updated: RemoteDraft[];
102
+ /** Deleted draft IDs */
103
+ deleted: string[];
104
+ /** Remote drafts that were modified since last sync */
105
+ changed: RemoteDraft[];
106
+ /** Conflicts detected */
107
+ conflicts: Array<{
108
+ remoteId: string;
109
+ localVersion: number;
110
+ remoteVersion: number;
111
+ remoteDraft: RemoteDraft;
112
+ }>;
113
+ /** Server timestamp */
114
+ serverTime: number;
115
+ }
116
+ /**
117
+ * Sync adapter interface - implement this to connect to your backend
118
+ */
119
+ interface SyncAdapter {
120
+ /**
121
+ * Fetch all drafts for the current user
122
+ */
123
+ fetchDrafts(): Promise<RemoteDraft[]>;
124
+ /**
125
+ * Fetch drafts changed since a timestamp
126
+ */
127
+ fetchChanges(since: number): Promise<RemoteDraft[]>;
128
+ /**
129
+ * Create a new draft on the server
130
+ */
131
+ createDraft(draft: DraftPayload): Promise<RemoteDraft>;
132
+ /**
133
+ * Update an existing draft on the server
134
+ */
135
+ updateDraft(remoteId: string, draft: DraftPayload): Promise<RemoteDraft>;
136
+ /**
137
+ * Delete a draft from the server
138
+ */
139
+ deleteDraft(remoteId: string): Promise<void>;
140
+ /**
141
+ * Batch sync operation (optional, for efficiency)
142
+ */
143
+ batchSync?(request: SyncRequest): Promise<SyncResponse>;
144
+ }
145
+ /**
146
+ * Sync service configuration
147
+ */
148
+ interface SyncConfig {
149
+ /** Sync adapter implementation */
150
+ adapter: SyncAdapter;
151
+ /** Auto-sync interval in milliseconds (default: 30000) */
152
+ syncIntervalMs?: number;
153
+ /** Maximum number of drafts to sync in one batch (default: 50) */
154
+ batchSize?: number;
155
+ /** Whether to sync automatically when online (default: true) */
156
+ autoSyncOnReconnect?: boolean;
157
+ /** Callback when sync completes */
158
+ onSyncComplete?: (result: SyncResult) => void;
159
+ /** Callback when conflicts are detected */
160
+ onConflictDetected?: (conflicts: DraftConflict[]) => void;
161
+ /** Callback when online status changes */
162
+ onOnlineStatusChange?: (online: boolean) => void;
163
+ /** Callback for sync errors */
164
+ onSyncError?: (error: Error) => void;
165
+ }
166
+ /**
167
+ * Result of a sync operation
168
+ */
169
+ interface SyncResult {
170
+ /** Whether sync was successful */
171
+ success: boolean;
172
+ /** Number of drafts uploaded */
173
+ uploaded: number;
174
+ /** Number of drafts downloaded */
175
+ downloaded: number;
176
+ /** Number of drafts deleted remotely */
177
+ deletedRemote: number;
178
+ /** Number of drafts deleted locally */
179
+ deletedLocal: number;
180
+ /** Conflicts detected */
181
+ conflicts: DraftConflict[];
182
+ /** Error message if failed */
183
+ error?: string;
184
+ /** Timestamp of sync */
185
+ timestamp: number;
186
+ /** Duration in milliseconds */
187
+ durationMs: number;
188
+ }
189
+ /**
190
+ * Conflict between local and remote draft
191
+ */
192
+ interface DraftConflict {
193
+ /** Local draft ID */
194
+ localDraftId: string;
195
+ /** Remote draft ID */
196
+ remoteDraftId: string;
197
+ /** Form key */
198
+ formKey: string;
199
+ /** Local version */
200
+ localVersion: number;
201
+ /** Remote version */
202
+ remoteVersion: number;
203
+ /** Local draft data */
204
+ localDraft: StoredDraftV2;
205
+ /** Remote draft data */
206
+ remoteDraft: RemoteDraft;
207
+ /** When conflict was detected */
208
+ detectedAt: number;
209
+ }
210
+ /**
211
+ * Conflict resolution strategy
212
+ */
213
+ type ConflictResolution = 'keep-local' | 'keep-remote' | 'merge';
214
+ /**
215
+ * Pending sync operation
216
+ */
217
+ interface PendingSyncOperation {
218
+ /** Operation ID */
219
+ id: string;
220
+ /** Operation type */
221
+ type: 'create' | 'update' | 'delete';
222
+ /** Draft ID */
223
+ draftId: string;
224
+ /** Form key */
225
+ formKey: string;
226
+ /** Remote ID (for updates/deletes) */
227
+ remoteId?: string;
228
+ /** Draft payload (for creates/updates) */
229
+ payload?: DraftPayload;
230
+ /** When operation was queued */
231
+ queuedAt: number;
232
+ /** Number of retry attempts */
233
+ retryCount: number;
234
+ /** Last error message */
235
+ lastError?: string;
236
+ }
237
+ /**
238
+ * Sync queue state
239
+ */
240
+ interface SyncQueueState {
241
+ /** Pending operations */
242
+ pending: PendingSyncOperation[];
243
+ /** Operations currently being processed */
244
+ processing: PendingSyncOperation[];
245
+ /** Failed operations */
246
+ failed: PendingSyncOperation[];
247
+ /** Last sync timestamp */
248
+ lastSyncAt: number | null;
249
+ }
250
+ /**
251
+ * Sync event types
252
+ */
253
+ type SyncEventType = 'sync:start' | 'sync:complete' | 'sync:error' | 'sync:conflict' | 'online' | 'offline' | 'draft:uploaded' | 'draft:downloaded' | 'draft:deleted';
254
+ /**
255
+ * Sync event payload
256
+ */
257
+ interface SyncEvent {
258
+ type: SyncEventType;
259
+ timestamp: number;
260
+ data?: unknown;
261
+ }
262
+ /**
263
+ * Sync event listener
264
+ */
265
+ type SyncEventListener = (event: SyncEvent) => void;
266
+
267
+ /**
268
+ * Network connectivity detection and monitoring
269
+ */
270
+
271
+ /**
272
+ * Connectivity monitor for detecting online/offline status
273
+ */
274
+ declare class ConnectivityMonitor {
275
+ private listeners;
276
+ private state;
277
+ private boundHandleOnline;
278
+ private boundHandleOffline;
279
+ constructor();
280
+ /**
281
+ * Check if currently online
282
+ */
283
+ private checkOnline;
284
+ /**
285
+ * Handle online event
286
+ */
287
+ private handleOnline;
288
+ /**
289
+ * Handle offline event
290
+ */
291
+ private handleOffline;
292
+ /**
293
+ * Notify all listeners of status change
294
+ */
295
+ private notifyListeners;
296
+ /**
297
+ * Get current connectivity state
298
+ */
299
+ getState(): ConnectivityState;
300
+ /**
301
+ * Check if currently online
302
+ */
303
+ get isOnline(): boolean;
304
+ /**
305
+ * Subscribe to online status changes
306
+ */
307
+ subscribe(listener: (online: boolean) => void): () => void;
308
+ /**
309
+ * Perform a connectivity check by making a request
310
+ */
311
+ checkConnectivity(testUrl?: string): Promise<boolean>;
312
+ /**
313
+ * Get connection info if available
314
+ */
315
+ getConnectionInfo(): {
316
+ type?: string;
317
+ effectiveType?: string;
318
+ downlink?: number;
319
+ } | null;
320
+ /**
321
+ * Dispose and clean up
322
+ */
323
+ dispose(): void;
324
+ }
325
+ /**
326
+ * Create a connectivity monitor
327
+ */
328
+ declare function createConnectivityMonitor(): ConnectivityMonitor;
329
+
330
+ /**
331
+ * Sync queue for managing pending sync operations
332
+ */
333
+
334
+ /**
335
+ * Sync queue for managing pending operations
336
+ */
337
+ declare class SyncQueue {
338
+ private readonly adapter;
339
+ private state;
340
+ private saveTimeout;
341
+ constructor(adapter: StorageAdapter);
342
+ /**
343
+ * Initialize the queue from storage
344
+ */
345
+ initialize(): Promise<void>;
346
+ /**
347
+ * Save queue state to storage
348
+ */
349
+ private save;
350
+ /**
351
+ * Queue a create operation
352
+ */
353
+ queueCreate(draftId: string, formKey: string, payload: DraftPayload): Promise<string>;
354
+ /**
355
+ * Queue an update operation
356
+ */
357
+ queueUpdate(draftId: string, formKey: string, remoteId: string, payload: DraftPayload): Promise<string>;
358
+ /**
359
+ * Queue a delete operation
360
+ */
361
+ queueDelete(draftId: string, formKey: string, remoteId: string): Promise<string>;
362
+ /**
363
+ * Remove all operations for a draft
364
+ */
365
+ private removeOperationsForDraft;
366
+ /**
367
+ * Get next pending operation
368
+ */
369
+ getNext(): PendingSyncOperation | null;
370
+ /**
371
+ * Get all pending operations
372
+ */
373
+ getPending(): PendingSyncOperation[];
374
+ /**
375
+ * Get pending operation count
376
+ */
377
+ get pendingCount(): number;
378
+ /**
379
+ * Get failed operation count
380
+ */
381
+ get failedCount(): number;
382
+ /**
383
+ * Get processing operation count
384
+ */
385
+ get processingCount(): number;
386
+ /**
387
+ * Mark an operation as processing
388
+ */
389
+ markProcessing(operationId: string): Promise<void>;
390
+ /**
391
+ * Mark an operation as completed
392
+ */
393
+ markCompleted(operationId: string): Promise<void>;
394
+ /**
395
+ * Mark an operation as failed
396
+ */
397
+ markFailed(operationId: string, error: string): Promise<void>;
398
+ /**
399
+ * Retry a failed operation
400
+ */
401
+ retryFailed(operationId: string): Promise<boolean>;
402
+ /**
403
+ * Retry all failed operations
404
+ */
405
+ retryAllFailed(): Promise<number>;
406
+ /**
407
+ * Remove a failed operation
408
+ */
409
+ removeFailed(operationId: string): Promise<boolean>;
410
+ /**
411
+ * Clear all failed operations
412
+ */
413
+ clearFailed(): Promise<void>;
414
+ /**
415
+ * Get failed operations
416
+ */
417
+ getFailed(): PendingSyncOperation[];
418
+ /**
419
+ * Update last sync timestamp
420
+ */
421
+ updateLastSyncAt(timestamp: number): Promise<void>;
422
+ /**
423
+ * Get last sync timestamp
424
+ */
425
+ get lastSyncAt(): number | null;
426
+ /**
427
+ * Clear all pending operations
428
+ */
429
+ clearPending(): Promise<void>;
430
+ /**
431
+ * Clear all operations
432
+ */
433
+ clearAll(): Promise<void>;
434
+ /**
435
+ * Dispose the queue
436
+ */
437
+ dispose(): void;
438
+ }
439
+ /**
440
+ * Create a sync queue
441
+ */
442
+ declare function createSyncQueue(adapter: StorageAdapter): SyncQueue;
443
+
444
+ /**
445
+ * Conflict resolution utilities
446
+ */
447
+
448
+ /**
449
+ * Check if two drafts are in conflict
450
+ */
451
+ declare function detectConflict(local: StoredDraftV2, remote: RemoteDraft): boolean;
452
+ /**
453
+ * Create a conflict record
454
+ */
455
+ declare function createConflict(local: StoredDraftV2, remote: RemoteDraft): DraftConflict;
456
+ /**
457
+ * Resolve a conflict by keeping local version
458
+ */
459
+ declare function resolveKeepLocal(local: StoredDraftV2, _remote: RemoteDraft): StoredDraftV2;
460
+ /**
461
+ * Resolve a conflict by keeping remote version
462
+ */
463
+ declare function resolveKeepRemote(local: StoredDraftV2, remote: RemoteDraft): StoredDraftV2;
464
+ /**
465
+ * Apply a conflict resolution
466
+ */
467
+ declare function applyResolution(local: StoredDraftV2, remote: RemoteDraft, resolution: ConflictResolution): StoredDraftV2;
468
+ /**
469
+ * Resolve a conflict by merging local and remote changes
470
+ * Uses a field-level merge strategy:
471
+ * - Remote-only fields are added to local
472
+ * - Local-only fields are preserved
473
+ * - Conflicting fields use last-write-wins (keep local)
474
+ */
475
+ declare function resolveMerge(local: StoredDraftV2, remote: RemoteDraft): StoredDraftV2;
476
+ /**
477
+ * Compare two drafts for differences
478
+ */
479
+ declare function compareDrafts(local: StoredDraftV2, remote: RemoteDraft): {
480
+ hasDifferences: boolean;
481
+ changedFields: string[];
482
+ localOnly: string[];
483
+ remoteOnly: string[];
484
+ };
485
+
486
+ /**
487
+ * Sync service for cloud synchronization
488
+ */
489
+
490
+ /**
491
+ * Sync service for managing cloud synchronization
492
+ */
493
+ declare class SyncService {
494
+ private readonly adapter;
495
+ private readonly draftManager;
496
+ private readonly connectivity;
497
+ private readonly queue;
498
+ private readonly syncAdapter;
499
+ private readonly config;
500
+ private autoSyncInterval;
501
+ private isSyncing;
502
+ private lastSyncResult;
503
+ private conflicts;
504
+ private eventListeners;
505
+ private connectivityUnsubscribe;
506
+ private onSyncComplete?;
507
+ private onConflictDetected?;
508
+ private onOnlineStatusChange?;
509
+ private onSyncError?;
510
+ constructor(adapter: StorageAdapter, config: SyncConfig);
511
+ /**
512
+ * Initialize the sync service
513
+ */
514
+ initialize(): Promise<void>;
515
+ /**
516
+ * Check if currently online
517
+ */
518
+ get isOnline(): boolean;
519
+ /**
520
+ * Check if currently syncing
521
+ */
522
+ get syncing(): boolean;
523
+ /**
524
+ * Get last sync timestamp
525
+ */
526
+ get lastSyncAt(): number | null;
527
+ /**
528
+ * Get pending sync count
529
+ */
530
+ get pendingCount(): number;
531
+ /**
532
+ * Get current conflicts
533
+ */
534
+ getConflicts(): DraftConflict[];
535
+ /**
536
+ * Get last sync result
537
+ */
538
+ getLastSyncResult(): SyncResult | null;
539
+ /**
540
+ * Sync all drafts
541
+ */
542
+ syncAll(): Promise<SyncResult>;
543
+ /**
544
+ * Sync a specific draft
545
+ */
546
+ syncDraft(draftId: string): Promise<SyncResult>;
547
+ /**
548
+ * Force push a draft (overwrite remote)
549
+ */
550
+ forcePush(draftId: string): Promise<void>;
551
+ /**
552
+ * Force pull a draft (overwrite local)
553
+ */
554
+ forcePull(draftId: string): Promise<void>;
555
+ /**
556
+ * Resolve a conflict
557
+ */
558
+ resolveConflict(draftId: string, resolution: ConflictResolution): Promise<void>;
559
+ /**
560
+ * Start auto sync
561
+ */
562
+ startAutoSync(): void;
563
+ /**
564
+ * Stop auto sync
565
+ */
566
+ stopAutoSync(): void;
567
+ /**
568
+ * Check if auto sync is active
569
+ */
570
+ get isAutoSyncActive(): boolean;
571
+ /**
572
+ * Subscribe to sync events
573
+ */
574
+ on(event: SyncEventType, listener: SyncEventListener): () => void;
575
+ /**
576
+ * Emit a sync event
577
+ */
578
+ private emit;
579
+ /**
580
+ * Dispose the sync service
581
+ */
582
+ dispose(): void;
583
+ /**
584
+ * Perform the actual sync
585
+ */
586
+ private performSync;
587
+ /**
588
+ * Sync a single draft
589
+ */
590
+ private syncSingleDraft;
591
+ /**
592
+ * Download a remote draft
593
+ */
594
+ private downloadRemoteDraft;
595
+ /**
596
+ * Create a payload from a draft
597
+ */
598
+ private createPayload;
599
+ /**
600
+ * Create an error result
601
+ */
602
+ private createErrorResult;
603
+ /**
604
+ * Load conflicts from storage
605
+ */
606
+ private loadConflicts;
607
+ /**
608
+ * Save conflicts to storage
609
+ */
610
+ private saveConflicts;
611
+ /**
612
+ * Remove a conflict
613
+ */
614
+ private removeConflict;
615
+ }
616
+ /**
617
+ * Create a sync service
618
+ */
619
+ declare function createSyncService(adapter: StorageAdapter, config: SyncConfig): SyncService;
620
+
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 };
@@ -0,0 +1,3 @@
1
+ export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge } from '../chunk-XHMX3BYT.js';
2
+ import '../chunk-4QWZX3LM.js';
3
+ import '../chunk-ZC7F2HDV.js';