@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.
- package/README.md +272 -0
- package/dist/cache/index.d.ts +198 -0
- package/dist/cache/index.js +2 -0
- package/dist/chunk-4QWZX3LM.js +498 -0
- package/dist/chunk-IIG4QLLN.js +178 -0
- package/dist/chunk-U27TKOYZ.js +312 -0
- package/dist/chunk-UQUCUBV2.js +297 -0
- package/dist/chunk-UXUGWW74.js +1432 -0
- package/dist/chunk-XHMX3BYT.js +1040 -0
- package/dist/chunk-ZC7F2HDV.js +100 -0
- package/dist/drafts/index.d.ts +203 -0
- package/dist/drafts/index.js +3 -0
- package/dist/files/index.d.ts +181 -0
- package/dist/files/index.js +2 -0
- package/dist/index.d.ts +249 -0
- package/dist/index.js +107 -0
- package/dist/storage/index.d.ts +198 -0
- package/dist/storage/index.js +2 -0
- package/dist/sync/index.d.ts +621 -0
- package/dist/sync/index.js +3 -0
- package/dist/types-B2FJpQuN.d.ts +179 -0
- package/dist/types-CWuauHAr.d.ts +204 -0
- package/package.json +77 -0
|
@@ -0,0 +1,1040 @@
|
|
|
1
|
+
import { generateDraftId, DraftManagerService } from './chunk-4QWZX3LM.js';
|
|
2
|
+
|
|
3
|
+
// src/sync/types.ts
|
|
4
|
+
var SyncConflictError = class extends Error {
|
|
5
|
+
constructor(message, remoteVersion) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.remoteVersion = remoteVersion;
|
|
8
|
+
this.status = 409;
|
|
9
|
+
this.name = "SyncConflictError";
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
function isSyncConflictError(error) {
|
|
13
|
+
if (error instanceof SyncConflictError) {
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
if (error instanceof Error && "status" in error) {
|
|
17
|
+
return error.status === 409;
|
|
18
|
+
}
|
|
19
|
+
if (error instanceof Error && error.message.includes("409")) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/sync/connectivity.ts
|
|
26
|
+
var ConnectivityMonitor = class {
|
|
27
|
+
constructor() {
|
|
28
|
+
this.listeners = /* @__PURE__ */ new Set();
|
|
29
|
+
this.state = {
|
|
30
|
+
isOnline: this.checkOnline(),
|
|
31
|
+
lastCheckedAt: Date.now()
|
|
32
|
+
};
|
|
33
|
+
this.boundHandleOnline = this.handleOnline.bind(this);
|
|
34
|
+
this.boundHandleOffline = this.handleOffline.bind(this);
|
|
35
|
+
if (typeof window !== "undefined") {
|
|
36
|
+
window.addEventListener("online", this.boundHandleOnline);
|
|
37
|
+
window.addEventListener("offline", this.boundHandleOffline);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Check if currently online
|
|
42
|
+
*/
|
|
43
|
+
checkOnline() {
|
|
44
|
+
if (typeof navigator !== "undefined") {
|
|
45
|
+
return navigator.onLine;
|
|
46
|
+
}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Handle online event
|
|
51
|
+
*/
|
|
52
|
+
handleOnline() {
|
|
53
|
+
this.state = {
|
|
54
|
+
...this.state,
|
|
55
|
+
isOnline: true,
|
|
56
|
+
lastCheckedAt: Date.now()
|
|
57
|
+
};
|
|
58
|
+
this.notifyListeners(true);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Handle offline event
|
|
62
|
+
*/
|
|
63
|
+
handleOffline() {
|
|
64
|
+
this.state = {
|
|
65
|
+
...this.state,
|
|
66
|
+
isOnline: false,
|
|
67
|
+
lastCheckedAt: Date.now()
|
|
68
|
+
};
|
|
69
|
+
this.notifyListeners(false);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Notify all listeners of status change
|
|
73
|
+
*/
|
|
74
|
+
notifyListeners(online) {
|
|
75
|
+
this.listeners.forEach((listener) => {
|
|
76
|
+
try {
|
|
77
|
+
listener(online);
|
|
78
|
+
} catch (error) {
|
|
79
|
+
console.error("[ConnectivityMonitor] Listener error:", error);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get current connectivity state
|
|
85
|
+
*/
|
|
86
|
+
getState() {
|
|
87
|
+
this.state = {
|
|
88
|
+
...this.state,
|
|
89
|
+
isOnline: this.checkOnline(),
|
|
90
|
+
lastCheckedAt: Date.now()
|
|
91
|
+
};
|
|
92
|
+
return this.state;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Check if currently online
|
|
96
|
+
*/
|
|
97
|
+
get isOnline() {
|
|
98
|
+
return this.checkOnline();
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Subscribe to online status changes
|
|
102
|
+
*/
|
|
103
|
+
subscribe(listener) {
|
|
104
|
+
this.listeners.add(listener);
|
|
105
|
+
return () => {
|
|
106
|
+
this.listeners.delete(listener);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Perform a connectivity check by making a request
|
|
111
|
+
*/
|
|
112
|
+
async checkConnectivity(testUrl) {
|
|
113
|
+
try {
|
|
114
|
+
const url = testUrl || "https://www.google.com/favicon.ico";
|
|
115
|
+
const controller = new AbortController();
|
|
116
|
+
const timeoutId = setTimeout(() => controller.abort(), 5e3);
|
|
117
|
+
const response = await fetch(url, {
|
|
118
|
+
method: "HEAD",
|
|
119
|
+
mode: "no-cors",
|
|
120
|
+
cache: "no-store",
|
|
121
|
+
signal: controller.signal
|
|
122
|
+
});
|
|
123
|
+
clearTimeout(timeoutId);
|
|
124
|
+
const online = response.ok || response.type === "opaque";
|
|
125
|
+
this.state = {
|
|
126
|
+
...this.state,
|
|
127
|
+
isOnline: online,
|
|
128
|
+
lastCheckedAt: Date.now()
|
|
129
|
+
};
|
|
130
|
+
return online;
|
|
131
|
+
} catch {
|
|
132
|
+
this.state = {
|
|
133
|
+
...this.state,
|
|
134
|
+
isOnline: false,
|
|
135
|
+
lastCheckedAt: Date.now()
|
|
136
|
+
};
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Get connection info if available
|
|
142
|
+
*/
|
|
143
|
+
getConnectionInfo() {
|
|
144
|
+
if (typeof navigator !== "undefined" && "connection" in navigator) {
|
|
145
|
+
const conn = navigator.connection;
|
|
146
|
+
return {
|
|
147
|
+
type: conn.type,
|
|
148
|
+
effectiveType: conn.effectiveType,
|
|
149
|
+
downlink: conn.downlink
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Dispose and clean up
|
|
156
|
+
*/
|
|
157
|
+
dispose() {
|
|
158
|
+
if (typeof window !== "undefined") {
|
|
159
|
+
window.removeEventListener("online", this.boundHandleOnline);
|
|
160
|
+
window.removeEventListener("offline", this.boundHandleOffline);
|
|
161
|
+
}
|
|
162
|
+
this.listeners.clear();
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
function createConnectivityMonitor() {
|
|
166
|
+
return new ConnectivityMonitor();
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// src/sync/queue.ts
|
|
170
|
+
var QUEUE_STORAGE_KEY = "sync-queue";
|
|
171
|
+
var MAX_RETRY_COUNT = 3;
|
|
172
|
+
var SyncQueue = class {
|
|
173
|
+
constructor(adapter) {
|
|
174
|
+
this.adapter = adapter;
|
|
175
|
+
this.saveTimeout = null;
|
|
176
|
+
this.state = {
|
|
177
|
+
pending: [],
|
|
178
|
+
processing: [],
|
|
179
|
+
failed: [],
|
|
180
|
+
lastSyncAt: null
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Initialize the queue from storage
|
|
185
|
+
*/
|
|
186
|
+
async initialize() {
|
|
187
|
+
try {
|
|
188
|
+
const entry = await this.adapter.getJSON(QUEUE_STORAGE_KEY);
|
|
189
|
+
if (entry?.data) {
|
|
190
|
+
this.state = {
|
|
191
|
+
...entry.data,
|
|
192
|
+
// Move any processing items back to pending on restart
|
|
193
|
+
pending: [...entry.data.pending, ...entry.data.processing],
|
|
194
|
+
processing: []
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
} catch (error) {
|
|
198
|
+
console.warn("[SyncQueue] Failed to load queue state:", error);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Save queue state to storage
|
|
203
|
+
*/
|
|
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);
|
|
215
|
+
}
|
|
216
|
+
}, 100);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Queue a create operation
|
|
220
|
+
*/
|
|
221
|
+
async queueCreate(draftId, formKey, payload) {
|
|
222
|
+
const operation = {
|
|
223
|
+
id: generateDraftId(),
|
|
224
|
+
type: "create",
|
|
225
|
+
draftId,
|
|
226
|
+
formKey,
|
|
227
|
+
payload,
|
|
228
|
+
queuedAt: Date.now(),
|
|
229
|
+
retryCount: 0
|
|
230
|
+
};
|
|
231
|
+
this.removeOperationsForDraft(draftId);
|
|
232
|
+
this.state.pending.push(operation);
|
|
233
|
+
await this.save();
|
|
234
|
+
return operation.id;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Queue an update operation
|
|
238
|
+
*/
|
|
239
|
+
async queueUpdate(draftId, formKey, remoteId, payload) {
|
|
240
|
+
const operation = {
|
|
241
|
+
id: generateDraftId(),
|
|
242
|
+
type: "update",
|
|
243
|
+
draftId,
|
|
244
|
+
formKey,
|
|
245
|
+
remoteId,
|
|
246
|
+
payload,
|
|
247
|
+
queuedAt: Date.now(),
|
|
248
|
+
retryCount: 0
|
|
249
|
+
};
|
|
250
|
+
this.removeOperationsForDraft(draftId);
|
|
251
|
+
this.state.pending.push(operation);
|
|
252
|
+
await this.save();
|
|
253
|
+
return operation.id;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Queue a delete operation
|
|
257
|
+
*/
|
|
258
|
+
async queueDelete(draftId, formKey, remoteId) {
|
|
259
|
+
const operation = {
|
|
260
|
+
id: generateDraftId(),
|
|
261
|
+
type: "delete",
|
|
262
|
+
draftId,
|
|
263
|
+
formKey,
|
|
264
|
+
remoteId,
|
|
265
|
+
queuedAt: Date.now(),
|
|
266
|
+
retryCount: 0
|
|
267
|
+
};
|
|
268
|
+
this.removeOperationsForDraft(draftId);
|
|
269
|
+
this.state.pending.push(operation);
|
|
270
|
+
await this.save();
|
|
271
|
+
return operation.id;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Remove all operations for a draft
|
|
275
|
+
*/
|
|
276
|
+
removeOperationsForDraft(draftId) {
|
|
277
|
+
this.state.pending = this.state.pending.filter((op) => op.draftId !== draftId);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Get next pending operation
|
|
281
|
+
*/
|
|
282
|
+
getNext() {
|
|
283
|
+
return this.state.pending[0] ?? null;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Get all pending operations
|
|
287
|
+
*/
|
|
288
|
+
getPending() {
|
|
289
|
+
return [...this.state.pending];
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Get pending operation count
|
|
293
|
+
*/
|
|
294
|
+
get pendingCount() {
|
|
295
|
+
return this.state.pending.length;
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Get failed operation count
|
|
299
|
+
*/
|
|
300
|
+
get failedCount() {
|
|
301
|
+
return this.state.failed.length;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Get processing operation count
|
|
305
|
+
*/
|
|
306
|
+
get processingCount() {
|
|
307
|
+
return this.state.processing.length;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Mark an operation as processing
|
|
311
|
+
*/
|
|
312
|
+
async markProcessing(operationId) {
|
|
313
|
+
const index = this.state.pending.findIndex((op) => op.id === operationId);
|
|
314
|
+
if (index !== -1) {
|
|
315
|
+
const [operation] = this.state.pending.splice(index, 1);
|
|
316
|
+
this.state.processing.push(operation);
|
|
317
|
+
await this.save();
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Mark an operation as completed
|
|
322
|
+
*/
|
|
323
|
+
async markCompleted(operationId) {
|
|
324
|
+
this.state.processing = this.state.processing.filter((op) => op.id !== operationId);
|
|
325
|
+
await this.save();
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Mark an operation as failed
|
|
329
|
+
*/
|
|
330
|
+
async markFailed(operationId, error) {
|
|
331
|
+
const index = this.state.processing.findIndex((op) => op.id === operationId);
|
|
332
|
+
if (index !== -1) {
|
|
333
|
+
const [operation] = this.state.processing.splice(index, 1);
|
|
334
|
+
operation.retryCount++;
|
|
335
|
+
operation.lastError = error;
|
|
336
|
+
if (operation.retryCount < MAX_RETRY_COUNT) {
|
|
337
|
+
this.state.pending.push(operation);
|
|
338
|
+
} else {
|
|
339
|
+
this.state.failed.push(operation);
|
|
340
|
+
}
|
|
341
|
+
await this.save();
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* Retry a failed operation
|
|
346
|
+
*/
|
|
347
|
+
async retryFailed(operationId) {
|
|
348
|
+
const index = this.state.failed.findIndex((op) => op.id === operationId);
|
|
349
|
+
if (index !== -1) {
|
|
350
|
+
const [operation] = this.state.failed.splice(index, 1);
|
|
351
|
+
operation.retryCount = 0;
|
|
352
|
+
operation.lastError = void 0;
|
|
353
|
+
this.state.pending.push(operation);
|
|
354
|
+
await this.save();
|
|
355
|
+
return true;
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Retry all failed operations
|
|
361
|
+
*/
|
|
362
|
+
async retryAllFailed() {
|
|
363
|
+
const count = this.state.failed.length;
|
|
364
|
+
for (const operation of this.state.failed) {
|
|
365
|
+
operation.retryCount = 0;
|
|
366
|
+
operation.lastError = void 0;
|
|
367
|
+
this.state.pending.push(operation);
|
|
368
|
+
}
|
|
369
|
+
this.state.failed = [];
|
|
370
|
+
await this.save();
|
|
371
|
+
return count;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Remove a failed operation
|
|
375
|
+
*/
|
|
376
|
+
async removeFailed(operationId) {
|
|
377
|
+
const index = this.state.failed.findIndex((op) => op.id === operationId);
|
|
378
|
+
if (index !== -1) {
|
|
379
|
+
this.state.failed.splice(index, 1);
|
|
380
|
+
await this.save();
|
|
381
|
+
return true;
|
|
382
|
+
}
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Clear all failed operations
|
|
387
|
+
*/
|
|
388
|
+
async clearFailed() {
|
|
389
|
+
this.state.failed = [];
|
|
390
|
+
await this.save();
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Get failed operations
|
|
394
|
+
*/
|
|
395
|
+
getFailed() {
|
|
396
|
+
return [...this.state.failed];
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Update last sync timestamp
|
|
400
|
+
*/
|
|
401
|
+
async updateLastSyncAt(timestamp) {
|
|
402
|
+
this.state.lastSyncAt = timestamp;
|
|
403
|
+
await this.save();
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Get last sync timestamp
|
|
407
|
+
*/
|
|
408
|
+
get lastSyncAt() {
|
|
409
|
+
return this.state.lastSyncAt;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Clear all pending operations
|
|
413
|
+
*/
|
|
414
|
+
async clearPending() {
|
|
415
|
+
this.state.pending = [];
|
|
416
|
+
await this.save();
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Clear all operations
|
|
420
|
+
*/
|
|
421
|
+
async clearAll() {
|
|
422
|
+
this.state = {
|
|
423
|
+
pending: [],
|
|
424
|
+
processing: [],
|
|
425
|
+
failed: [],
|
|
426
|
+
lastSyncAt: this.state.lastSyncAt
|
|
427
|
+
};
|
|
428
|
+
await this.save();
|
|
429
|
+
}
|
|
430
|
+
/**
|
|
431
|
+
* Dispose the queue
|
|
432
|
+
*/
|
|
433
|
+
dispose() {
|
|
434
|
+
if (this.saveTimeout) {
|
|
435
|
+
clearTimeout(this.saveTimeout);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
function createSyncQueue(adapter) {
|
|
440
|
+
return new SyncQueue(adapter);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// src/sync/conflict.ts
|
|
444
|
+
function detectConflict(local, remote) {
|
|
445
|
+
if (local.metadata.remoteId === remote.id) {
|
|
446
|
+
return local.metadata.version !== remote.version && local.metadata.lastSyncedAt !== null && local.metadata.updatedAt > local.metadata.lastSyncedAt && remote.updatedAt > local.metadata.lastSyncedAt;
|
|
447
|
+
}
|
|
448
|
+
return false;
|
|
449
|
+
}
|
|
450
|
+
function createConflict(local, remote) {
|
|
451
|
+
return {
|
|
452
|
+
localDraftId: local.metadata.id,
|
|
453
|
+
remoteDraftId: remote.id,
|
|
454
|
+
formKey: local.metadata.formKey,
|
|
455
|
+
localVersion: local.metadata.version,
|
|
456
|
+
remoteVersion: remote.version,
|
|
457
|
+
localDraft: local,
|
|
458
|
+
remoteDraft: remote,
|
|
459
|
+
detectedAt: Date.now()
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
function resolveKeepLocal(local, _remote) {
|
|
463
|
+
return {
|
|
464
|
+
...local,
|
|
465
|
+
metadata: {
|
|
466
|
+
...local.metadata,
|
|
467
|
+
syncStatus: "pending-upload",
|
|
468
|
+
// Increment version to override remote
|
|
469
|
+
version: local.metadata.version + 1
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
function resolveKeepRemote(local, remote) {
|
|
474
|
+
return {
|
|
475
|
+
metadata: {
|
|
476
|
+
...local.metadata,
|
|
477
|
+
name: remote.name,
|
|
478
|
+
updatedAt: remote.updatedAt,
|
|
479
|
+
syncStatus: "synced",
|
|
480
|
+
version: remote.version,
|
|
481
|
+
lastSyncedAt: Date.now()
|
|
482
|
+
},
|
|
483
|
+
data: remote.data,
|
|
484
|
+
fileRefs: remote.fileRefs
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
function applyResolution(local, remote, resolution) {
|
|
488
|
+
switch (resolution) {
|
|
489
|
+
case "keep-local":
|
|
490
|
+
return resolveKeepLocal(local);
|
|
491
|
+
case "keep-remote":
|
|
492
|
+
return resolveKeepRemote(local, remote);
|
|
493
|
+
case "merge":
|
|
494
|
+
return resolveMerge(local, remote);
|
|
495
|
+
default:
|
|
496
|
+
throw new Error(`Unknown resolution: ${resolution}`);
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
function resolveMerge(local, remote) {
|
|
500
|
+
const { remoteOnly } = compareDrafts(local, remote);
|
|
501
|
+
const mergedData = { ...local.data };
|
|
502
|
+
for (const field of remoteOnly) {
|
|
503
|
+
mergedData[field] = remote.data[field];
|
|
504
|
+
}
|
|
505
|
+
const mergedFileRefs = { ...local.fileRefs };
|
|
506
|
+
for (const [field, files] of Object.entries(remote.fileRefs)) {
|
|
507
|
+
if (mergedFileRefs[field]) {
|
|
508
|
+
const uniqueFiles = [.../* @__PURE__ */ new Set([...mergedFileRefs[field], ...files])];
|
|
509
|
+
mergedFileRefs[field] = uniqueFiles;
|
|
510
|
+
} else {
|
|
511
|
+
mergedFileRefs[field] = [...files];
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return {
|
|
515
|
+
metadata: {
|
|
516
|
+
...local.metadata,
|
|
517
|
+
syncStatus: "pending-upload",
|
|
518
|
+
// Use max version + 1 to ensure we override both
|
|
519
|
+
version: Math.max(local.metadata.version, remote.version) + 1,
|
|
520
|
+
// Merge name: prefer local name, but use remote if local is auto-generated
|
|
521
|
+
name: local.metadata.name.includes("(copy)") || local.metadata.name.includes("Migrated") ? remote.name : local.metadata.name
|
|
522
|
+
},
|
|
523
|
+
data: mergedData,
|
|
524
|
+
fileRefs: mergedFileRefs
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
function compareDrafts(local, remote) {
|
|
528
|
+
const localFields = new Set(Object.keys(local.data));
|
|
529
|
+
const remoteFields = new Set(Object.keys(remote.data));
|
|
530
|
+
const changedFields = [];
|
|
531
|
+
const localOnly = [];
|
|
532
|
+
const remoteOnly = [];
|
|
533
|
+
for (const field of localFields) {
|
|
534
|
+
if (!remoteFields.has(field)) {
|
|
535
|
+
localOnly.push(field);
|
|
536
|
+
} else if (JSON.stringify(local.data[field]) !== JSON.stringify(remote.data[field])) {
|
|
537
|
+
changedFields.push(field);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
for (const field of remoteFields) {
|
|
541
|
+
if (!localFields.has(field)) {
|
|
542
|
+
remoteOnly.push(field);
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
return {
|
|
546
|
+
hasDifferences: changedFields.length > 0 || localOnly.length > 0 || remoteOnly.length > 0,
|
|
547
|
+
changedFields,
|
|
548
|
+
localOnly,
|
|
549
|
+
remoteOnly
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
// src/sync/service.ts
|
|
554
|
+
var DEFAULT_SYNC_INTERVAL = 3e4;
|
|
555
|
+
var DEFAULT_BATCH_SIZE = 50;
|
|
556
|
+
var CONFLICTS_STORAGE_KEY = "sync-conflicts";
|
|
557
|
+
var SyncService = class {
|
|
558
|
+
constructor(adapter, config) {
|
|
559
|
+
this.adapter = adapter;
|
|
560
|
+
this.autoSyncInterval = null;
|
|
561
|
+
this.isSyncing = false;
|
|
562
|
+
this.lastSyncResult = null;
|
|
563
|
+
this.conflicts = [];
|
|
564
|
+
this.eventListeners = /* @__PURE__ */ new Map();
|
|
565
|
+
this.connectivityUnsubscribe = null;
|
|
566
|
+
this.syncAdapter = config.adapter;
|
|
567
|
+
this.config = {
|
|
568
|
+
syncIntervalMs: config.syncIntervalMs ?? DEFAULT_SYNC_INTERVAL,
|
|
569
|
+
batchSize: config.batchSize ?? DEFAULT_BATCH_SIZE,
|
|
570
|
+
autoSyncOnReconnect: config.autoSyncOnReconnect ?? true
|
|
571
|
+
};
|
|
572
|
+
this.onSyncComplete = config.onSyncComplete;
|
|
573
|
+
this.onConflictDetected = config.onConflictDetected;
|
|
574
|
+
this.onOnlineStatusChange = config.onOnlineStatusChange;
|
|
575
|
+
this.onSyncError = config.onSyncError;
|
|
576
|
+
this.draftManager = new DraftManagerService(adapter);
|
|
577
|
+
this.connectivity = createConnectivityMonitor();
|
|
578
|
+
this.queue = createSyncQueue(adapter);
|
|
579
|
+
this.connectivityUnsubscribe = this.connectivity.subscribe((online) => {
|
|
580
|
+
this.emit("online", online);
|
|
581
|
+
this.onOnlineStatusChange?.(online);
|
|
582
|
+
if (online && this.config.autoSyncOnReconnect) {
|
|
583
|
+
void this.syncAll();
|
|
584
|
+
}
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
/**
|
|
588
|
+
* Initialize the sync service
|
|
589
|
+
*/
|
|
590
|
+
async initialize() {
|
|
591
|
+
await this.queue.initialize();
|
|
592
|
+
await this.loadConflicts();
|
|
593
|
+
}
|
|
594
|
+
// ============================================
|
|
595
|
+
// Properties
|
|
596
|
+
// ============================================
|
|
597
|
+
/**
|
|
598
|
+
* Check if currently online
|
|
599
|
+
*/
|
|
600
|
+
get isOnline() {
|
|
601
|
+
return this.connectivity.isOnline;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Check if currently syncing
|
|
605
|
+
*/
|
|
606
|
+
get syncing() {
|
|
607
|
+
return this.isSyncing;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* Get last sync timestamp
|
|
611
|
+
*/
|
|
612
|
+
get lastSyncAt() {
|
|
613
|
+
return this.queue.lastSyncAt;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Get pending sync count
|
|
617
|
+
*/
|
|
618
|
+
get pendingCount() {
|
|
619
|
+
return this.queue.pendingCount;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Get current conflicts
|
|
623
|
+
*/
|
|
624
|
+
getConflicts() {
|
|
625
|
+
return [...this.conflicts];
|
|
626
|
+
}
|
|
627
|
+
/**
|
|
628
|
+
* Get last sync result
|
|
629
|
+
*/
|
|
630
|
+
getLastSyncResult() {
|
|
631
|
+
return this.lastSyncResult;
|
|
632
|
+
}
|
|
633
|
+
// ============================================
|
|
634
|
+
// Sync Operations
|
|
635
|
+
// ============================================
|
|
636
|
+
/**
|
|
637
|
+
* Sync all drafts
|
|
638
|
+
*/
|
|
639
|
+
async syncAll() {
|
|
640
|
+
if (this.isSyncing) {
|
|
641
|
+
return this.createErrorResult("Sync already in progress");
|
|
642
|
+
}
|
|
643
|
+
if (!this.isOnline) {
|
|
644
|
+
return this.createErrorResult("Device is offline");
|
|
645
|
+
}
|
|
646
|
+
this.isSyncing = true;
|
|
647
|
+
const startTime = Date.now();
|
|
648
|
+
this.emit("sync:start", null);
|
|
649
|
+
try {
|
|
650
|
+
const result = await this.performSync();
|
|
651
|
+
this.lastSyncResult = result;
|
|
652
|
+
if (result.conflicts.length > 0) {
|
|
653
|
+
this.conflicts = result.conflicts;
|
|
654
|
+
await this.saveConflicts();
|
|
655
|
+
this.emit("sync:conflict", result.conflicts);
|
|
656
|
+
this.onConflictDetected?.(result.conflicts);
|
|
657
|
+
}
|
|
658
|
+
await this.queue.updateLastSyncAt(Date.now());
|
|
659
|
+
this.emit("sync:complete", result);
|
|
660
|
+
this.onSyncComplete?.(result);
|
|
661
|
+
return result;
|
|
662
|
+
} catch (error) {
|
|
663
|
+
const errorResult = this.createErrorResult(
|
|
664
|
+
error instanceof Error ? error.message : "Unknown error",
|
|
665
|
+
startTime
|
|
666
|
+
);
|
|
667
|
+
this.emit("sync:error", error);
|
|
668
|
+
this.onSyncError?.(error instanceof Error ? error : new Error(String(error)));
|
|
669
|
+
return errorResult;
|
|
670
|
+
} finally {
|
|
671
|
+
this.isSyncing = false;
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* Sync a specific draft
|
|
676
|
+
*/
|
|
677
|
+
async syncDraft(draftId) {
|
|
678
|
+
if (!this.isOnline) {
|
|
679
|
+
return this.createErrorResult("Device is offline");
|
|
680
|
+
}
|
|
681
|
+
const startTime = Date.now();
|
|
682
|
+
try {
|
|
683
|
+
const draft = await this.draftManager.getDraft(draftId);
|
|
684
|
+
if (!draft) {
|
|
685
|
+
return this.createErrorResult("Draft not found");
|
|
686
|
+
}
|
|
687
|
+
const result = await this.syncSingleDraft(draft);
|
|
688
|
+
return {
|
|
689
|
+
success: true,
|
|
690
|
+
uploaded: result.uploaded ? 1 : 0,
|
|
691
|
+
downloaded: result.downloaded ? 1 : 0,
|
|
692
|
+
deletedRemote: 0,
|
|
693
|
+
deletedLocal: 0,
|
|
694
|
+
conflicts: result.conflict ? [result.conflict] : [],
|
|
695
|
+
timestamp: Date.now(),
|
|
696
|
+
durationMs: Date.now() - startTime
|
|
697
|
+
};
|
|
698
|
+
} catch (error) {
|
|
699
|
+
return this.createErrorResult(
|
|
700
|
+
error instanceof Error ? error.message : "Unknown error",
|
|
701
|
+
startTime
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
706
|
+
* Force push a draft (overwrite remote)
|
|
707
|
+
*/
|
|
708
|
+
async forcePush(draftId) {
|
|
709
|
+
const draft = await this.draftManager.getDraft(draftId);
|
|
710
|
+
if (!draft) {
|
|
711
|
+
throw new Error("Draft not found");
|
|
712
|
+
}
|
|
713
|
+
const payload = this.createPayload(draft);
|
|
714
|
+
if (draft.metadata.remoteId) {
|
|
715
|
+
await this.syncAdapter.updateDraft(draft.metadata.remoteId, payload);
|
|
716
|
+
} else {
|
|
717
|
+
const remote = await this.syncAdapter.createDraft(payload);
|
|
718
|
+
await this.draftManager.updateDraftMetadata(draftId, {
|
|
719
|
+
remoteId: remote.id,
|
|
720
|
+
syncStatus: "synced",
|
|
721
|
+
lastSyncedAt: Date.now()
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
await this.removeConflict(draftId);
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Force pull a draft (overwrite local)
|
|
728
|
+
*/
|
|
729
|
+
async forcePull(draftId) {
|
|
730
|
+
const draft = await this.draftManager.getDraft(draftId);
|
|
731
|
+
if (!draft || !draft.metadata.remoteId) {
|
|
732
|
+
throw new Error("Draft not found or not synced");
|
|
733
|
+
}
|
|
734
|
+
const remotes = await this.syncAdapter.fetchDrafts();
|
|
735
|
+
const remote = remotes.find((r) => r.id === draft.metadata.remoteId);
|
|
736
|
+
if (!remote) {
|
|
737
|
+
throw new Error("Remote draft not found");
|
|
738
|
+
}
|
|
739
|
+
await this.draftManager.updateDraft(draftId, remote.data, remote.fileRefs);
|
|
740
|
+
await this.draftManager.updateDraftMetadata(draftId, {
|
|
741
|
+
name: remote.name,
|
|
742
|
+
syncStatus: "synced",
|
|
743
|
+
lastSyncedAt: Date.now()
|
|
744
|
+
});
|
|
745
|
+
await this.removeConflict(draftId);
|
|
746
|
+
}
|
|
747
|
+
// ============================================
|
|
748
|
+
// Conflict Resolution
|
|
749
|
+
// ============================================
|
|
750
|
+
/**
|
|
751
|
+
* Resolve a conflict
|
|
752
|
+
*/
|
|
753
|
+
async resolveConflict(draftId, resolution) {
|
|
754
|
+
const conflict = this.conflicts.find((c) => c.localDraftId === draftId);
|
|
755
|
+
if (!conflict) {
|
|
756
|
+
throw new Error("Conflict not found");
|
|
757
|
+
}
|
|
758
|
+
const resolved = applyResolution(conflict.localDraft, conflict.remoteDraft, resolution);
|
|
759
|
+
await this.draftManager.updateDraft(draftId, resolved.data, resolved.fileRefs);
|
|
760
|
+
await this.draftManager.updateDraftMetadata(draftId, {
|
|
761
|
+
name: resolved.metadata.name,
|
|
762
|
+
syncStatus: resolved.metadata.syncStatus,
|
|
763
|
+
lastSyncedAt: resolved.metadata.lastSyncedAt
|
|
764
|
+
});
|
|
765
|
+
if (resolution === "keep-local" || resolution === "merge") {
|
|
766
|
+
await this.forcePush(draftId);
|
|
767
|
+
}
|
|
768
|
+
await this.removeConflict(draftId);
|
|
769
|
+
}
|
|
770
|
+
// ============================================
|
|
771
|
+
// Auto Sync
|
|
772
|
+
// ============================================
|
|
773
|
+
/**
|
|
774
|
+
* Start auto sync
|
|
775
|
+
*/
|
|
776
|
+
startAutoSync() {
|
|
777
|
+
if (this.autoSyncInterval !== null) {
|
|
778
|
+
return;
|
|
779
|
+
}
|
|
780
|
+
this.autoSyncInterval = setInterval(() => {
|
|
781
|
+
if (this.isOnline && !this.isSyncing) {
|
|
782
|
+
void this.syncAll();
|
|
783
|
+
}
|
|
784
|
+
}, this.config.syncIntervalMs);
|
|
785
|
+
}
|
|
786
|
+
/**
|
|
787
|
+
* Stop auto sync
|
|
788
|
+
*/
|
|
789
|
+
stopAutoSync() {
|
|
790
|
+
if (this.autoSyncInterval !== null) {
|
|
791
|
+
clearInterval(this.autoSyncInterval);
|
|
792
|
+
this.autoSyncInterval = null;
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Check if auto sync is active
|
|
797
|
+
*/
|
|
798
|
+
get isAutoSyncActive() {
|
|
799
|
+
return this.autoSyncInterval !== null;
|
|
800
|
+
}
|
|
801
|
+
// ============================================
|
|
802
|
+
// Event Handling
|
|
803
|
+
// ============================================
|
|
804
|
+
/**
|
|
805
|
+
* Subscribe to sync events
|
|
806
|
+
*/
|
|
807
|
+
on(event, listener) {
|
|
808
|
+
if (!this.eventListeners.has(event)) {
|
|
809
|
+
this.eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
810
|
+
}
|
|
811
|
+
this.eventListeners.get(event).add(listener);
|
|
812
|
+
return () => {
|
|
813
|
+
this.eventListeners.get(event)?.delete(listener);
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
/**
|
|
817
|
+
* Emit a sync event
|
|
818
|
+
*/
|
|
819
|
+
emit(type, data) {
|
|
820
|
+
const event = {
|
|
821
|
+
type,
|
|
822
|
+
timestamp: Date.now(),
|
|
823
|
+
data
|
|
824
|
+
};
|
|
825
|
+
this.eventListeners.get(type)?.forEach((listener) => {
|
|
826
|
+
try {
|
|
827
|
+
listener(event);
|
|
828
|
+
} catch (error) {
|
|
829
|
+
console.error("[SyncService] Event listener error:", error);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
// ============================================
|
|
834
|
+
// Lifecycle
|
|
835
|
+
// ============================================
|
|
836
|
+
/**
|
|
837
|
+
* Dispose the sync service
|
|
838
|
+
*/
|
|
839
|
+
dispose() {
|
|
840
|
+
this.stopAutoSync();
|
|
841
|
+
this.connectivityUnsubscribe?.();
|
|
842
|
+
this.connectivity.dispose();
|
|
843
|
+
this.queue.dispose();
|
|
844
|
+
this.eventListeners.clear();
|
|
845
|
+
}
|
|
846
|
+
// ============================================
|
|
847
|
+
// Private Methods
|
|
848
|
+
// ============================================
|
|
849
|
+
/**
|
|
850
|
+
* Perform the actual sync
|
|
851
|
+
*/
|
|
852
|
+
async performSync() {
|
|
853
|
+
const startTime = Date.now();
|
|
854
|
+
let uploaded = 0;
|
|
855
|
+
let downloaded = 0;
|
|
856
|
+
let deletedRemote = 0;
|
|
857
|
+
let deletedLocal = 0;
|
|
858
|
+
const conflicts = [];
|
|
859
|
+
const localDrafts = await this.draftManager.listDrafts();
|
|
860
|
+
const pendingDrafts = localDrafts.filter(
|
|
861
|
+
(d) => d.syncStatus === "pending-upload" || d.syncStatus === "local-only"
|
|
862
|
+
);
|
|
863
|
+
const remoteDrafts = await this.syncAdapter.fetchDrafts();
|
|
864
|
+
const remoteMap = new Map(remoteDrafts.map((r) => [r.id, r]));
|
|
865
|
+
for (const metadata of pendingDrafts) {
|
|
866
|
+
const draft = await this.draftManager.getDraft(metadata.id);
|
|
867
|
+
if (!draft) continue;
|
|
868
|
+
const result = await this.syncSingleDraft(draft);
|
|
869
|
+
if (result.uploaded) uploaded++;
|
|
870
|
+
if (result.conflict) conflicts.push(result.conflict);
|
|
871
|
+
}
|
|
872
|
+
for (const remote of remoteDrafts) {
|
|
873
|
+
const localDraft = localDrafts.find((d) => d.remoteId === remote.id);
|
|
874
|
+
if (!localDraft) {
|
|
875
|
+
await this.downloadRemoteDraft(remote);
|
|
876
|
+
downloaded++;
|
|
877
|
+
} else if (localDraft.syncStatus === "synced") {
|
|
878
|
+
const local = await this.draftManager.getDraft(localDraft.id);
|
|
879
|
+
if (local && remote.updatedAt > (local.metadata.lastSyncedAt ?? 0)) {
|
|
880
|
+
if (local.metadata.updatedAt > (local.metadata.lastSyncedAt ?? 0)) {
|
|
881
|
+
conflicts.push(createConflict(local, remote));
|
|
882
|
+
await this.draftManager.updateDraftMetadata(localDraft.id, {
|
|
883
|
+
syncStatus: "conflict"
|
|
884
|
+
});
|
|
885
|
+
} else {
|
|
886
|
+
await this.downloadRemoteDraft(remote, localDraft.id);
|
|
887
|
+
downloaded++;
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
const syncedDrafts = localDrafts.filter((d) => d.remoteId && d.syncStatus === "synced");
|
|
893
|
+
for (const metadata of syncedDrafts) {
|
|
894
|
+
if (!remoteMap.has(metadata.remoteId)) {
|
|
895
|
+
await this.draftManager.deleteDraft(metadata.id);
|
|
896
|
+
deletedLocal++;
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
return {
|
|
900
|
+
success: true,
|
|
901
|
+
uploaded,
|
|
902
|
+
downloaded,
|
|
903
|
+
deletedRemote,
|
|
904
|
+
deletedLocal,
|
|
905
|
+
conflicts,
|
|
906
|
+
timestamp: Date.now(),
|
|
907
|
+
durationMs: Date.now() - startTime
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
/**
|
|
911
|
+
* Sync a single draft
|
|
912
|
+
*/
|
|
913
|
+
async syncSingleDraft(draft) {
|
|
914
|
+
const payload = this.createPayload(draft);
|
|
915
|
+
try {
|
|
916
|
+
if (draft.metadata.remoteId) {
|
|
917
|
+
await this.syncAdapter.updateDraft(draft.metadata.remoteId, payload);
|
|
918
|
+
await this.draftManager.updateDraftMetadata(draft.metadata.id, {
|
|
919
|
+
syncStatus: "synced",
|
|
920
|
+
lastSyncedAt: Date.now()
|
|
921
|
+
});
|
|
922
|
+
return { uploaded: true, downloaded: false, conflict: null };
|
|
923
|
+
} else {
|
|
924
|
+
const remote = await this.syncAdapter.createDraft(payload);
|
|
925
|
+
await this.draftManager.updateDraftMetadata(draft.metadata.id, {
|
|
926
|
+
remoteId: remote.id,
|
|
927
|
+
syncStatus: "synced",
|
|
928
|
+
lastSyncedAt: Date.now()
|
|
929
|
+
});
|
|
930
|
+
return { uploaded: true, downloaded: false, conflict: null };
|
|
931
|
+
}
|
|
932
|
+
} catch (error) {
|
|
933
|
+
if (isSyncConflictError(error)) {
|
|
934
|
+
const remotes = await this.syncAdapter.fetchDrafts();
|
|
935
|
+
const remote = remotes.find((r) => r.id === draft.metadata.remoteId);
|
|
936
|
+
if (remote) {
|
|
937
|
+
const conflict = createConflict(draft, remote);
|
|
938
|
+
await this.draftManager.updateDraftMetadata(draft.metadata.id, {
|
|
939
|
+
syncStatus: "conflict"
|
|
940
|
+
});
|
|
941
|
+
return { uploaded: false, downloaded: false, conflict };
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
throw error;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* Download a remote draft
|
|
949
|
+
*/
|
|
950
|
+
async downloadRemoteDraft(remote, existingDraftId) {
|
|
951
|
+
if (existingDraftId) {
|
|
952
|
+
await this.draftManager.updateDraft(existingDraftId, remote.data, remote.fileRefs);
|
|
953
|
+
await this.draftManager.updateDraftMetadata(existingDraftId, {
|
|
954
|
+
name: remote.name,
|
|
955
|
+
syncStatus: "synced",
|
|
956
|
+
remoteId: remote.id,
|
|
957
|
+
lastSyncedAt: Date.now()
|
|
958
|
+
});
|
|
959
|
+
} else {
|
|
960
|
+
const metadata = await this.draftManager.createDraft(remote.formKey, {
|
|
961
|
+
name: remote.name,
|
|
962
|
+
initialData: remote.data,
|
|
963
|
+
initialFileRefs: remote.fileRefs
|
|
964
|
+
});
|
|
965
|
+
await this.draftManager.updateDraftMetadata(metadata.id, {
|
|
966
|
+
syncStatus: "synced",
|
|
967
|
+
remoteId: remote.id,
|
|
968
|
+
lastSyncedAt: Date.now()
|
|
969
|
+
});
|
|
970
|
+
}
|
|
971
|
+
this.emit("draft:downloaded", remote);
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* Create a payload from a draft
|
|
975
|
+
*/
|
|
976
|
+
createPayload(draft) {
|
|
977
|
+
return {
|
|
978
|
+
formKey: draft.metadata.formKey,
|
|
979
|
+
name: draft.metadata.name,
|
|
980
|
+
data: draft.data,
|
|
981
|
+
fileRefs: draft.fileRefs,
|
|
982
|
+
version: draft.metadata.version,
|
|
983
|
+
updatedAt: draft.metadata.updatedAt,
|
|
984
|
+
deviceId: draft.metadata.deviceId
|
|
985
|
+
};
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Create an error result
|
|
989
|
+
*/
|
|
990
|
+
createErrorResult(error, startTime) {
|
|
991
|
+
return {
|
|
992
|
+
success: false,
|
|
993
|
+
uploaded: 0,
|
|
994
|
+
downloaded: 0,
|
|
995
|
+
deletedRemote: 0,
|
|
996
|
+
deletedLocal: 0,
|
|
997
|
+
conflicts: [],
|
|
998
|
+
error,
|
|
999
|
+
timestamp: Date.now(),
|
|
1000
|
+
durationMs: startTime ? Date.now() - startTime : 0
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
/**
|
|
1004
|
+
* Load conflicts from storage
|
|
1005
|
+
*/
|
|
1006
|
+
async loadConflicts() {
|
|
1007
|
+
try {
|
|
1008
|
+
const entry = await this.adapter.getJSON(CONFLICTS_STORAGE_KEY);
|
|
1009
|
+
if (entry?.data) {
|
|
1010
|
+
this.conflicts = entry.data;
|
|
1011
|
+
}
|
|
1012
|
+
} catch (error) {
|
|
1013
|
+
console.warn("[SyncService] Failed to load conflicts:", error);
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Save conflicts to storage
|
|
1018
|
+
*/
|
|
1019
|
+
async saveConflicts() {
|
|
1020
|
+
try {
|
|
1021
|
+
await this.adapter.storeJSON(CONFLICTS_STORAGE_KEY, this.conflicts, {
|
|
1022
|
+
tags: ["sync-conflicts"]
|
|
1023
|
+
});
|
|
1024
|
+
} catch (error) {
|
|
1025
|
+
console.warn("[SyncService] Failed to save conflicts:", error);
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
/**
|
|
1029
|
+
* Remove a conflict
|
|
1030
|
+
*/
|
|
1031
|
+
async removeConflict(draftId) {
|
|
1032
|
+
this.conflicts = this.conflicts.filter((c) => c.localDraftId !== draftId);
|
|
1033
|
+
await this.saveConflicts();
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
function createSyncService(adapter, config) {
|
|
1037
|
+
return new SyncService(adapter, config);
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
export { ConnectivityMonitor, SyncConflictError, SyncQueue, SyncService, applyResolution, compareDrafts, createConflict, createConnectivityMonitor, createSyncQueue, createSyncService, detectConflict, isSyncConflictError, resolveKeepLocal, resolveKeepRemote, resolveMerge };
|