@classic-homes/offline-react 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.
- package/dist/index.js +179 -113
- package/package.json +1 -6
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState, useRef, useCallback, useEffect } from 'react';
|
|
1
|
+
import { useState, useRef, useCallback, useEffect, useLayoutEffect } from 'react';
|
|
2
2
|
import { isOfflineStorageInitialized, getOfflineStorage, createSyncService } from '@classic-homes/offline-core';
|
|
3
3
|
export { getOfflineStorage, getOfflineStorageOrNull, initOfflineStorage, invalidateAllCache, invalidateCacheByTag, isOfflineStorageInitialized } from '@classic-homes/offline-core';
|
|
4
4
|
|
|
@@ -191,6 +191,9 @@ function usePersistedForm(options) {
|
|
|
191
191
|
const lastSubmitTimeRef = useRef(0);
|
|
192
192
|
const submittingRef = useRef(false);
|
|
193
193
|
const debounceTimerRef = useRef(null);
|
|
194
|
+
const loadPromiseRef = useRef(Promise.resolve());
|
|
195
|
+
const touchedRef = useRef({});
|
|
196
|
+
const touchedFileRefsRef = useRef({});
|
|
194
197
|
const originalValuesRef = useRef(JSON.parse(JSON.stringify(initialValues)));
|
|
195
198
|
const dataRef = useRef(data);
|
|
196
199
|
dataRef.current = data;
|
|
@@ -208,12 +211,12 @@ function usePersistedForm(options) {
|
|
|
208
211
|
debounceTimerRef.current = null;
|
|
209
212
|
}
|
|
210
213
|
}, []);
|
|
211
|
-
useEffect(() => cancelPendingSave, [cancelPendingSave]);
|
|
212
214
|
const isValid = Object.keys(errors).length === 0 && schema.safeParse(data).success;
|
|
213
215
|
const saveDraft = useCallback(async () => {
|
|
214
216
|
if (!isOfflineStorageInitialized() || !isDirtyRef.current) {
|
|
215
217
|
return;
|
|
216
218
|
}
|
|
219
|
+
await loadPromiseRef.current;
|
|
217
220
|
try {
|
|
218
221
|
const storage = getOfflineStorage();
|
|
219
222
|
const { excludeFields: excludeFields2 = [], expirationMs: expirationMs2 = DEFAULT_EXPIRATION_MS } = optionsRef.current;
|
|
@@ -223,9 +226,11 @@ function usePersistedForm(options) {
|
|
|
223
226
|
delete dataToPersist[field];
|
|
224
227
|
}
|
|
225
228
|
if (multiDraft) {
|
|
226
|
-
|
|
229
|
+
let draftId = currentDraftIdRef.current;
|
|
230
|
+
if (draftId && !await storage.draftManager.updateDraft(draftId, dataToPersist, fileRefs2)) {
|
|
231
|
+
draftId = null;
|
|
232
|
+
}
|
|
227
233
|
if (draftId) {
|
|
228
|
-
await storage.draftManager.updateDraft(draftId, dataToPersist, fileRefs2);
|
|
229
234
|
const metadata = await storage.draftManager.getDraftMetadata(draftId);
|
|
230
235
|
if (metadata) {
|
|
231
236
|
setCurrentDraftMetadata(metadata);
|
|
@@ -260,6 +265,22 @@ function usePersistedForm(options) {
|
|
|
260
265
|
void saveDraft();
|
|
261
266
|
}, debounceMs);
|
|
262
267
|
}, [saveDraft, debounceMs, cancelPendingSave]);
|
|
268
|
+
const flushPendingSave = useCallback(() => {
|
|
269
|
+
if (debounceTimerRef.current) {
|
|
270
|
+
cancelPendingSave();
|
|
271
|
+
void saveDraft();
|
|
272
|
+
}
|
|
273
|
+
}, [saveDraft, cancelPendingSave]);
|
|
274
|
+
const flushRef = useRef(flushPendingSave);
|
|
275
|
+
flushRef.current = flushPendingSave;
|
|
276
|
+
useEffect(() => {
|
|
277
|
+
const onPageHide = () => flushRef.current();
|
|
278
|
+
window.addEventListener("pagehide", onPageHide);
|
|
279
|
+
return () => {
|
|
280
|
+
window.removeEventListener("pagehide", onPageHide);
|
|
281
|
+
flushRef.current();
|
|
282
|
+
};
|
|
283
|
+
}, []);
|
|
263
284
|
useEffect(() => {
|
|
264
285
|
let cancelled = false;
|
|
265
286
|
async function loadDraft() {
|
|
@@ -275,10 +296,18 @@ function usePersistedForm(options) {
|
|
|
275
296
|
} else {
|
|
276
297
|
draft = await storage.draftManager.getCurrentDraft(storageKey);
|
|
277
298
|
}
|
|
278
|
-
if (draft && !cancelled
|
|
299
|
+
if (draft && !cancelled) {
|
|
279
300
|
const { initialValues: initialValues2, onDraftRestored: onDraftRestored2, onDraftChange: onDraftChange2 } = optionsRef.current;
|
|
280
|
-
|
|
281
|
-
|
|
301
|
+
const restored = {
|
|
302
|
+
...JSON.parse(JSON.stringify(initialValues2)),
|
|
303
|
+
...draft.data,
|
|
304
|
+
...touchedRef.current
|
|
305
|
+
};
|
|
306
|
+
const restoredFileRefs = { ...draft.fileRefs, ...touchedFileRefsRef.current };
|
|
307
|
+
dataRef.current = restored;
|
|
308
|
+
fileRefsRef.current = restoredFileRefs;
|
|
309
|
+
setData(restored);
|
|
310
|
+
setFileRefs(restoredFileRefs);
|
|
282
311
|
currentDraftIdRef.current = draft.metadata.id;
|
|
283
312
|
setCurrentDraftId(draft.metadata.id);
|
|
284
313
|
setCurrentDraftMetadata(draft.metadata);
|
|
@@ -288,10 +317,18 @@ function usePersistedForm(options) {
|
|
|
288
317
|
}
|
|
289
318
|
} else {
|
|
290
319
|
const result = await storage.drafts.loadDraft(storageKey);
|
|
291
|
-
if (result.restored && result.draft && !cancelled
|
|
320
|
+
if (result.restored && result.draft && !cancelled) {
|
|
292
321
|
const { initialValues: initialValues2, onDraftRestored: onDraftRestored2 } = optionsRef.current;
|
|
293
|
-
|
|
294
|
-
|
|
322
|
+
const restored = {
|
|
323
|
+
...JSON.parse(JSON.stringify(initialValues2)),
|
|
324
|
+
...result.draft.data,
|
|
325
|
+
...touchedRef.current
|
|
326
|
+
};
|
|
327
|
+
const restoredFileRefs = { ...result.draft.fileRefs, ...touchedFileRefsRef.current };
|
|
328
|
+
dataRef.current = restored;
|
|
329
|
+
fileRefsRef.current = restoredFileRefs;
|
|
330
|
+
setData(restored);
|
|
331
|
+
setFileRefs(restoredFileRefs);
|
|
295
332
|
setDraftRestored(true);
|
|
296
333
|
onDraftRestored2?.();
|
|
297
334
|
}
|
|
@@ -300,7 +337,7 @@ function usePersistedForm(options) {
|
|
|
300
337
|
console.warn("Failed to load form draft:", err);
|
|
301
338
|
}
|
|
302
339
|
}
|
|
303
|
-
|
|
340
|
+
loadPromiseRef.current = loadDraft();
|
|
304
341
|
return () => {
|
|
305
342
|
cancelled = true;
|
|
306
343
|
};
|
|
@@ -308,6 +345,7 @@ function usePersistedForm(options) {
|
|
|
308
345
|
const setField = useCallback(
|
|
309
346
|
(field, value) => {
|
|
310
347
|
setData((prev) => ({ ...prev, [field]: value }));
|
|
348
|
+
touchedRef.current = { ...touchedRef.current, [field]: value };
|
|
311
349
|
isDirtyRef.current = true;
|
|
312
350
|
setIsDirty(true);
|
|
313
351
|
scheduleSaveDraft();
|
|
@@ -317,6 +355,7 @@ function usePersistedForm(options) {
|
|
|
317
355
|
const setFields = useCallback(
|
|
318
356
|
(fields) => {
|
|
319
357
|
setData((prev) => ({ ...prev, ...fields }));
|
|
358
|
+
touchedRef.current = { ...touchedRef.current, ...fields };
|
|
320
359
|
isDirtyRef.current = true;
|
|
321
360
|
setIsDirty(true);
|
|
322
361
|
scheduleSaveDraft();
|
|
@@ -369,6 +408,13 @@ function usePersistedForm(options) {
|
|
|
369
408
|
for (const fieldName of fileFields) {
|
|
370
409
|
const ids = fileRefs[fieldName] || [];
|
|
371
410
|
const fileMap = await storage.files.getFilesAsFileObjects(ids);
|
|
411
|
+
const missing = ids.filter((id) => !fileMap.has(id));
|
|
412
|
+
if (missing.length > 0) {
|
|
413
|
+
console.warn(
|
|
414
|
+
`[usePersistedForm] ${missing.length} file(s) for "${fieldName}" are missing from storage (expired or deleted)`,
|
|
415
|
+
missing
|
|
416
|
+
);
|
|
417
|
+
}
|
|
372
418
|
result[fieldName] = Array.from(fileMap.values());
|
|
373
419
|
}
|
|
374
420
|
return result;
|
|
@@ -380,17 +426,19 @@ function usePersistedForm(options) {
|
|
|
380
426
|
try {
|
|
381
427
|
const storage = getOfflineStorage();
|
|
382
428
|
const draftId = currentDraftIdRef.current;
|
|
383
|
-
if (multiDraft
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
429
|
+
if (multiDraft) {
|
|
430
|
+
if (draftId) {
|
|
431
|
+
await storage.draftManager.deleteDraft(draftId, true);
|
|
432
|
+
currentDraftIdRef.current = null;
|
|
433
|
+
setCurrentDraftId(null);
|
|
434
|
+
setCurrentDraftMetadata(null);
|
|
435
|
+
optionsRef.current.onDraftChange?.(null);
|
|
436
|
+
}
|
|
389
437
|
} else {
|
|
390
438
|
await storage.drafts.clearDraft(storageKey);
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
439
|
+
for (const fieldName of fileFields) {
|
|
440
|
+
await storage.files.deleteFormFiles(storageKey, fieldName);
|
|
441
|
+
}
|
|
394
442
|
}
|
|
395
443
|
} catch (err) {
|
|
396
444
|
console.warn("Failed to clear form draft:", err);
|
|
@@ -424,13 +472,23 @@ function usePersistedForm(options) {
|
|
|
424
472
|
} catch (err) {
|
|
425
473
|
setGlobalError(err instanceof Error ? err.message : "Submission failed");
|
|
426
474
|
lastSubmitTimeRef.current = 0;
|
|
475
|
+
void saveDraft();
|
|
427
476
|
throw err;
|
|
428
477
|
} finally {
|
|
429
478
|
submittingRef.current = false;
|
|
430
479
|
setIsSubmitting(false);
|
|
431
480
|
}
|
|
432
481
|
},
|
|
433
|
-
[
|
|
482
|
+
[
|
|
483
|
+
data,
|
|
484
|
+
validate,
|
|
485
|
+
onSubmit,
|
|
486
|
+
getFilesForSubmit,
|
|
487
|
+
clearDraft,
|
|
488
|
+
submitCooldownMs,
|
|
489
|
+
cancelPendingSave,
|
|
490
|
+
saveDraft
|
|
491
|
+
]
|
|
434
492
|
);
|
|
435
493
|
const reset = useCallback(() => {
|
|
436
494
|
cancelPendingSave();
|
|
@@ -439,6 +497,8 @@ function usePersistedForm(options) {
|
|
|
439
497
|
setGlobalError(null);
|
|
440
498
|
setIsSubmitted(false);
|
|
441
499
|
isDirtyRef.current = false;
|
|
500
|
+
touchedRef.current = {};
|
|
501
|
+
touchedFileRefsRef.current = {};
|
|
442
502
|
setIsDirty(false);
|
|
443
503
|
setDraftRestored(false);
|
|
444
504
|
setFileRefs({});
|
|
@@ -454,6 +514,7 @@ function usePersistedForm(options) {
|
|
|
454
514
|
const updateFileRefs = useCallback(
|
|
455
515
|
(fieldName, fileIds) => {
|
|
456
516
|
setFileRefs((prev) => ({ ...prev, [fieldName]: fileIds }));
|
|
517
|
+
touchedFileRefsRef.current = { ...touchedFileRefsRef.current, [fieldName]: fileIds };
|
|
457
518
|
isDirtyRef.current = true;
|
|
458
519
|
setIsDirty(true);
|
|
459
520
|
scheduleSaveDraft();
|
|
@@ -504,6 +565,8 @@ function usePersistedForm(options) {
|
|
|
504
565
|
setCurrentDraftId(draft.metadata.id);
|
|
505
566
|
setCurrentDraftMetadata(draft.metadata);
|
|
506
567
|
isDirtyRef.current = false;
|
|
568
|
+
touchedRef.current = {};
|
|
569
|
+
touchedFileRefsRef.current = {};
|
|
507
570
|
setIsDirty(false);
|
|
508
571
|
setDraftRestored(true);
|
|
509
572
|
await storage.draftManager.setCurrentDraft(storageKey, draftIdToLoad);
|
|
@@ -543,121 +606,112 @@ function usePersistedForm(options) {
|
|
|
543
606
|
loadDraftById
|
|
544
607
|
};
|
|
545
608
|
}
|
|
609
|
+
var useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
|
|
610
|
+
function emptyState(owner) {
|
|
611
|
+
return {
|
|
612
|
+
owner,
|
|
613
|
+
data: null,
|
|
614
|
+
isLoading: false,
|
|
615
|
+
isStale: false,
|
|
616
|
+
error: null,
|
|
617
|
+
ttlRemaining: null
|
|
618
|
+
};
|
|
619
|
+
}
|
|
546
620
|
function useOfflineCache(options) {
|
|
547
621
|
const { key, ttl, tags, staleWhileRevalidate = false, staleTime } = options;
|
|
548
|
-
const [
|
|
549
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
550
|
-
const [isStale, setIsStale] = useState(false);
|
|
551
|
-
const [error, setError] = useState(null);
|
|
552
|
-
const [ttlRemaining, setTtlRemaining] = useState(null);
|
|
622
|
+
const [state, setState] = useState(() => emptyState(key));
|
|
553
623
|
const mountedRef = useRef(true);
|
|
554
|
-
const
|
|
555
|
-
|
|
556
|
-
if (
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
624
|
+
const keyRef = useRef(key);
|
|
625
|
+
const update = useCallback((owner, patch) => {
|
|
626
|
+
if (!mountedRef.current || keyRef.current !== owner) return;
|
|
627
|
+
setState((prev) => {
|
|
628
|
+
if (keyRef.current !== owner) return prev;
|
|
629
|
+
const base = prev.owner === owner ? prev : emptyState(owner);
|
|
630
|
+
return { ...base, ...patch, owner };
|
|
631
|
+
});
|
|
561
632
|
}, []);
|
|
633
|
+
const updateFromResult = useCallback(
|
|
634
|
+
(owner, result) => {
|
|
635
|
+
if (result.hit) {
|
|
636
|
+
update(owner, {
|
|
637
|
+
data: result.value,
|
|
638
|
+
isStale: result.stale,
|
|
639
|
+
ttlRemaining: result.ttlRemaining
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
},
|
|
643
|
+
[update]
|
|
644
|
+
);
|
|
562
645
|
const get = useCallback(
|
|
563
646
|
async (fetcher) => {
|
|
647
|
+
const owner = key;
|
|
564
648
|
if (!isOfflineStorageInitialized()) {
|
|
565
|
-
|
|
649
|
+
update(owner, { isLoading: true });
|
|
566
650
|
try {
|
|
567
651
|
const value = await fetcher();
|
|
568
|
-
|
|
569
|
-
setData(value);
|
|
570
|
-
}
|
|
652
|
+
update(owner, { data: value });
|
|
571
653
|
return value;
|
|
572
654
|
} catch (err) {
|
|
573
|
-
const
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
}
|
|
577
|
-
throw error2;
|
|
655
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
656
|
+
update(owner, { error });
|
|
657
|
+
throw error;
|
|
578
658
|
} finally {
|
|
579
|
-
|
|
580
|
-
setIsLoading(false);
|
|
581
|
-
}
|
|
659
|
+
update(owner, { isLoading: false });
|
|
582
660
|
}
|
|
583
661
|
}
|
|
584
662
|
const storage = getOfflineStorage();
|
|
585
663
|
try {
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
const value = await storage.cache.getOrSet(key, fetcher, {
|
|
664
|
+
update(owner, { isLoading: true, error: null });
|
|
665
|
+
const value = await storage.cache.getOrSet(owner, fetcher, {
|
|
589
666
|
ttl,
|
|
590
667
|
tags,
|
|
591
668
|
staleWhileRevalidate,
|
|
592
669
|
staleTime,
|
|
593
|
-
onFetching: () => {
|
|
594
|
-
|
|
595
|
-
setIsLoading(true);
|
|
596
|
-
}
|
|
597
|
-
},
|
|
598
|
-
onFetchComplete: (err) => {
|
|
599
|
-
if (mountedRef.current) {
|
|
600
|
-
setIsLoading(false);
|
|
601
|
-
if (err) {
|
|
602
|
-
setError(err);
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
}
|
|
670
|
+
onFetching: () => update(owner, { isLoading: true }),
|
|
671
|
+
onFetchComplete: (err) => update(owner, err ? { isLoading: false, error: err } : { isLoading: false })
|
|
606
672
|
});
|
|
607
|
-
const result = await storage.cache.get(
|
|
608
|
-
updateFromResult(result);
|
|
673
|
+
const result = await storage.cache.get(owner);
|
|
674
|
+
updateFromResult(owner, result);
|
|
609
675
|
return value;
|
|
610
676
|
} catch (err) {
|
|
611
|
-
const
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
}
|
|
615
|
-
throw error2;
|
|
677
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
678
|
+
update(owner, { error });
|
|
679
|
+
throw error;
|
|
616
680
|
} finally {
|
|
617
|
-
|
|
618
|
-
setIsLoading(false);
|
|
619
|
-
}
|
|
681
|
+
update(owner, { isLoading: false });
|
|
620
682
|
}
|
|
621
683
|
},
|
|
622
|
-
[key, ttl, tags, staleWhileRevalidate, staleTime, updateFromResult]
|
|
684
|
+
[key, ttl, tags, staleWhileRevalidate, staleTime, update, updateFromResult]
|
|
623
685
|
);
|
|
624
686
|
const set = useCallback(
|
|
625
687
|
async (value) => {
|
|
688
|
+
const owner = key;
|
|
626
689
|
if (!isOfflineStorageInitialized()) {
|
|
627
|
-
|
|
690
|
+
update(owner, { data: value });
|
|
628
691
|
return;
|
|
629
692
|
}
|
|
630
693
|
const storage = getOfflineStorage();
|
|
631
694
|
try {
|
|
632
|
-
await storage.cache.set(
|
|
633
|
-
|
|
634
|
-
setData(value);
|
|
635
|
-
setIsStale(false);
|
|
636
|
-
setTtlRemaining(ttl ?? null);
|
|
637
|
-
}
|
|
695
|
+
await storage.cache.set(owner, value, { ttl, tags });
|
|
696
|
+
update(owner, { data: value, isStale: false, ttlRemaining: ttl ?? null });
|
|
638
697
|
} catch (err) {
|
|
639
|
-
const
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
}
|
|
643
|
-
throw error2;
|
|
698
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
699
|
+
update(owner, { error });
|
|
700
|
+
throw error;
|
|
644
701
|
}
|
|
645
702
|
},
|
|
646
|
-
[key, ttl, tags]
|
|
703
|
+
[key, ttl, tags, update]
|
|
647
704
|
);
|
|
648
705
|
const invalidate = useCallback(async () => {
|
|
706
|
+
const owner = key;
|
|
649
707
|
if (!isOfflineStorageInitialized()) {
|
|
650
|
-
|
|
708
|
+
update(owner, { data: null });
|
|
651
709
|
return;
|
|
652
710
|
}
|
|
653
711
|
const storage = getOfflineStorage();
|
|
654
|
-
await storage.cache.invalidate(
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
setIsStale(false);
|
|
658
|
-
setTtlRemaining(null);
|
|
659
|
-
}
|
|
660
|
-
}, [key]);
|
|
712
|
+
await storage.cache.invalidate(owner);
|
|
713
|
+
update(owner, { data: null, isStale: false, ttlRemaining: null });
|
|
714
|
+
}, [key, update]);
|
|
661
715
|
const refresh = useCallback(
|
|
662
716
|
async (fetcher) => {
|
|
663
717
|
await invalidate();
|
|
@@ -672,26 +726,30 @@ function useOfflineCache(options) {
|
|
|
672
726
|
const storage = getOfflineStorage();
|
|
673
727
|
return storage.cache.has(key);
|
|
674
728
|
}, [key]);
|
|
675
|
-
|
|
729
|
+
useIsomorphicLayoutEffect(() => {
|
|
676
730
|
mountedRef.current = true;
|
|
731
|
+
keyRef.current = key;
|
|
732
|
+
return () => {
|
|
733
|
+
mountedRef.current = false;
|
|
734
|
+
};
|
|
735
|
+
}, [key]);
|
|
736
|
+
useEffect(() => {
|
|
677
737
|
if (isOfflineStorageInitialized()) {
|
|
678
738
|
const storage = getOfflineStorage();
|
|
679
739
|
storage.cache.get(key).then((result) => {
|
|
680
|
-
if (
|
|
681
|
-
updateFromResult(result);
|
|
740
|
+
if (result.hit && !result.stale) {
|
|
741
|
+
updateFromResult(key, result);
|
|
682
742
|
}
|
|
683
743
|
});
|
|
684
744
|
}
|
|
685
|
-
return () => {
|
|
686
|
-
mountedRef.current = false;
|
|
687
|
-
};
|
|
688
745
|
}, [key, updateFromResult]);
|
|
746
|
+
const current = state.owner === key ? state : emptyState(key);
|
|
689
747
|
return {
|
|
690
|
-
data,
|
|
691
|
-
isLoading,
|
|
692
|
-
isStale,
|
|
693
|
-
error,
|
|
694
|
-
ttlRemaining,
|
|
748
|
+
data: current.data,
|
|
749
|
+
isLoading: current.isLoading,
|
|
750
|
+
isStale: current.isStale,
|
|
751
|
+
error: current.error,
|
|
752
|
+
ttlRemaining: current.ttlRemaining,
|
|
695
753
|
get,
|
|
696
754
|
set,
|
|
697
755
|
invalidate,
|
|
@@ -832,8 +890,10 @@ function useDraftSync(options) {
|
|
|
832
890
|
const [lastResult, setLastResult] = useState(null);
|
|
833
891
|
const [error, setError] = useState(null);
|
|
834
892
|
const syncServiceRef = useRef(null);
|
|
893
|
+
const disposingRef = useRef(Promise.resolve());
|
|
835
894
|
const initSyncService = useCallback(
|
|
836
895
|
async (isCancelled) => {
|
|
896
|
+
await disposingRef.current;
|
|
837
897
|
if (!isOfflineStorageInitialized()) {
|
|
838
898
|
return null;
|
|
839
899
|
}
|
|
@@ -846,6 +906,7 @@ function useDraftSync(options) {
|
|
|
846
906
|
setIsSyncing(false);
|
|
847
907
|
setLastSyncAt(result.timestamp);
|
|
848
908
|
setLastResult(result);
|
|
909
|
+
setPendingCount(service.pendingCount);
|
|
849
910
|
setConflicts(service.getConflicts());
|
|
850
911
|
setError(result.error ?? null);
|
|
851
912
|
optionsRef.current.onSyncComplete?.(result);
|
|
@@ -866,9 +927,12 @@ function useDraftSync(options) {
|
|
|
866
927
|
});
|
|
867
928
|
await service.initialize();
|
|
868
929
|
if (isCancelled()) {
|
|
869
|
-
service.dispose();
|
|
930
|
+
disposingRef.current = Promise.resolve(service.dispose());
|
|
870
931
|
return null;
|
|
871
932
|
}
|
|
933
|
+
setIsSyncing(false);
|
|
934
|
+
setLastResult(null);
|
|
935
|
+
setError(null);
|
|
872
936
|
setIsOnline(service.isOnline);
|
|
873
937
|
setLastSyncAt(service.lastSyncAt);
|
|
874
938
|
setPendingCount(service.pendingCount);
|
|
@@ -898,13 +962,14 @@ function useDraftSync(options) {
|
|
|
898
962
|
}
|
|
899
963
|
setIsSyncing(true);
|
|
900
964
|
setError(null);
|
|
965
|
+
const isCurrent = () => syncServiceRef.current === service;
|
|
901
966
|
try {
|
|
902
967
|
const result = await service.syncAll();
|
|
903
|
-
setPendingCount(service.pendingCount);
|
|
968
|
+
if (isCurrent()) setPendingCount(service.pendingCount);
|
|
904
969
|
return result;
|
|
905
970
|
} catch (err) {
|
|
906
971
|
const errorMsg = err instanceof Error ? err.message : "Sync failed";
|
|
907
|
-
setError(errorMsg);
|
|
972
|
+
if (isCurrent()) setError(errorMsg);
|
|
908
973
|
return {
|
|
909
974
|
success: false,
|
|
910
975
|
uploaded: 0,
|
|
@@ -917,7 +982,7 @@ function useDraftSync(options) {
|
|
|
917
982
|
durationMs: 0
|
|
918
983
|
};
|
|
919
984
|
} finally {
|
|
920
|
-
setIsSyncing(false);
|
|
985
|
+
if (isCurrent()) setIsSyncing(false);
|
|
921
986
|
}
|
|
922
987
|
}, []);
|
|
923
988
|
const syncDraft = useCallback(async (draftId) => {
|
|
@@ -943,7 +1008,7 @@ function useDraftSync(options) {
|
|
|
943
1008
|
throw new Error("Sync service not initialized");
|
|
944
1009
|
}
|
|
945
1010
|
await service.forcePush(draftId);
|
|
946
|
-
setConflicts(service.getConflicts());
|
|
1011
|
+
if (syncServiceRef.current === service) setConflicts(service.getConflicts());
|
|
947
1012
|
}, []);
|
|
948
1013
|
const forcePull = useCallback(async (draftId) => {
|
|
949
1014
|
const service = syncServiceRef.current;
|
|
@@ -951,7 +1016,7 @@ function useDraftSync(options) {
|
|
|
951
1016
|
throw new Error("Sync service not initialized");
|
|
952
1017
|
}
|
|
953
1018
|
await service.forcePull(draftId);
|
|
954
|
-
setConflicts(service.getConflicts());
|
|
1019
|
+
if (syncServiceRef.current === service) setConflicts(service.getConflicts());
|
|
955
1020
|
}, []);
|
|
956
1021
|
const resolveConflict = useCallback(
|
|
957
1022
|
async (draftId, resolution) => {
|
|
@@ -960,7 +1025,7 @@ function useDraftSync(options) {
|
|
|
960
1025
|
throw new Error("Sync service not initialized");
|
|
961
1026
|
}
|
|
962
1027
|
await service.resolveConflict(draftId, resolution);
|
|
963
|
-
setConflicts(service.getConflicts());
|
|
1028
|
+
if (syncServiceRef.current === service) setConflicts(service.getConflicts());
|
|
964
1029
|
},
|
|
965
1030
|
[]
|
|
966
1031
|
);
|
|
@@ -971,8 +1036,9 @@ function useDraftSync(options) {
|
|
|
971
1036
|
syncServiceRef.current?.stopAutoSync();
|
|
972
1037
|
}, []);
|
|
973
1038
|
const dispose = useCallback(() => {
|
|
974
|
-
syncServiceRef.current
|
|
1039
|
+
const service = syncServiceRef.current;
|
|
975
1040
|
syncServiceRef.current = null;
|
|
1041
|
+
if (service) disposingRef.current = Promise.resolve(service.dispose());
|
|
976
1042
|
}, []);
|
|
977
1043
|
useEffect(() => {
|
|
978
1044
|
let cancelled = false;
|
|
@@ -982,10 +1048,10 @@ function useDraftSync(options) {
|
|
|
982
1048
|
});
|
|
983
1049
|
return () => {
|
|
984
1050
|
cancelled = true;
|
|
985
|
-
service?.dispose();
|
|
986
1051
|
if (syncServiceRef.current === service) {
|
|
987
1052
|
syncServiceRef.current = null;
|
|
988
1053
|
}
|
|
1054
|
+
if (service) disposingRef.current = Promise.resolve(service.dispose());
|
|
989
1055
|
};
|
|
990
1056
|
}, [initSyncService]);
|
|
991
1057
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classic-homes/offline-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "React bindings for offline persistence and caching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -46,11 +46,6 @@
|
|
|
46
46
|
"react": "^18.0.0 || ^19.0.0",
|
|
47
47
|
"zod": "^3.23.0"
|
|
48
48
|
},
|
|
49
|
-
"peerDependenciesMeta": {
|
|
50
|
-
"zod": {
|
|
51
|
-
"optional": true
|
|
52
|
-
}
|
|
53
|
-
},
|
|
54
49
|
"dependencies": {
|
|
55
50
|
"@classic-homes/offline-core": "*"
|
|
56
51
|
},
|