@classic-homes/offline-react 0.2.0 → 0.3.2
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 +95 -93
- package/package.json +4 -4
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
|
|
|
@@ -606,121 +606,112 @@ function usePersistedForm(options) {
|
|
|
606
606
|
loadDraftById
|
|
607
607
|
};
|
|
608
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
|
+
}
|
|
609
620
|
function useOfflineCache(options) {
|
|
610
621
|
const { key, ttl, tags, staleWhileRevalidate = false, staleTime } = options;
|
|
611
|
-
const [
|
|
612
|
-
const [isLoading, setIsLoading] = useState(false);
|
|
613
|
-
const [isStale, setIsStale] = useState(false);
|
|
614
|
-
const [error, setError] = useState(null);
|
|
615
|
-
const [ttlRemaining, setTtlRemaining] = useState(null);
|
|
622
|
+
const [state, setState] = useState(() => emptyState(key));
|
|
616
623
|
const mountedRef = useRef(true);
|
|
617
|
-
const
|
|
618
|
-
|
|
619
|
-
if (
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
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
|
+
});
|
|
624
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
|
+
);
|
|
625
645
|
const get = useCallback(
|
|
626
646
|
async (fetcher) => {
|
|
647
|
+
const owner = key;
|
|
627
648
|
if (!isOfflineStorageInitialized()) {
|
|
628
|
-
|
|
649
|
+
update(owner, { isLoading: true });
|
|
629
650
|
try {
|
|
630
651
|
const value = await fetcher();
|
|
631
|
-
|
|
632
|
-
setData(value);
|
|
633
|
-
}
|
|
652
|
+
update(owner, { data: value });
|
|
634
653
|
return value;
|
|
635
654
|
} catch (err) {
|
|
636
|
-
const
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
}
|
|
640
|
-
throw error2;
|
|
655
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
656
|
+
update(owner, { error });
|
|
657
|
+
throw error;
|
|
641
658
|
} finally {
|
|
642
|
-
|
|
643
|
-
setIsLoading(false);
|
|
644
|
-
}
|
|
659
|
+
update(owner, { isLoading: false });
|
|
645
660
|
}
|
|
646
661
|
}
|
|
647
662
|
const storage = getOfflineStorage();
|
|
648
663
|
try {
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
const value = await storage.cache.getOrSet(key, fetcher, {
|
|
664
|
+
update(owner, { isLoading: true, error: null });
|
|
665
|
+
const value = await storage.cache.getOrSet(owner, fetcher, {
|
|
652
666
|
ttl,
|
|
653
667
|
tags,
|
|
654
668
|
staleWhileRevalidate,
|
|
655
669
|
staleTime,
|
|
656
|
-
onFetching: () => {
|
|
657
|
-
|
|
658
|
-
setIsLoading(true);
|
|
659
|
-
}
|
|
660
|
-
},
|
|
661
|
-
onFetchComplete: (err) => {
|
|
662
|
-
if (mountedRef.current) {
|
|
663
|
-
setIsLoading(false);
|
|
664
|
-
if (err) {
|
|
665
|
-
setError(err);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
}
|
|
670
|
+
onFetching: () => update(owner, { isLoading: true }),
|
|
671
|
+
onFetchComplete: (err) => update(owner, err ? { isLoading: false, error: err } : { isLoading: false })
|
|
669
672
|
});
|
|
670
|
-
const result = await storage.cache.get(
|
|
671
|
-
updateFromResult(result);
|
|
673
|
+
const result = await storage.cache.get(owner);
|
|
674
|
+
updateFromResult(owner, result);
|
|
672
675
|
return value;
|
|
673
676
|
} catch (err) {
|
|
674
|
-
const
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
}
|
|
678
|
-
throw error2;
|
|
677
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
678
|
+
update(owner, { error });
|
|
679
|
+
throw error;
|
|
679
680
|
} finally {
|
|
680
|
-
|
|
681
|
-
setIsLoading(false);
|
|
682
|
-
}
|
|
681
|
+
update(owner, { isLoading: false });
|
|
683
682
|
}
|
|
684
683
|
},
|
|
685
|
-
[key, ttl, tags, staleWhileRevalidate, staleTime, updateFromResult]
|
|
684
|
+
[key, ttl, tags, staleWhileRevalidate, staleTime, update, updateFromResult]
|
|
686
685
|
);
|
|
687
686
|
const set = useCallback(
|
|
688
687
|
async (value) => {
|
|
688
|
+
const owner = key;
|
|
689
689
|
if (!isOfflineStorageInitialized()) {
|
|
690
|
-
|
|
690
|
+
update(owner, { data: value });
|
|
691
691
|
return;
|
|
692
692
|
}
|
|
693
693
|
const storage = getOfflineStorage();
|
|
694
694
|
try {
|
|
695
|
-
await storage.cache.set(
|
|
696
|
-
|
|
697
|
-
setData(value);
|
|
698
|
-
setIsStale(false);
|
|
699
|
-
setTtlRemaining(ttl ?? null);
|
|
700
|
-
}
|
|
695
|
+
await storage.cache.set(owner, value, { ttl, tags });
|
|
696
|
+
update(owner, { data: value, isStale: false, ttlRemaining: ttl ?? null });
|
|
701
697
|
} catch (err) {
|
|
702
|
-
const
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
}
|
|
706
|
-
throw error2;
|
|
698
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
699
|
+
update(owner, { error });
|
|
700
|
+
throw error;
|
|
707
701
|
}
|
|
708
702
|
},
|
|
709
|
-
[key, ttl, tags]
|
|
703
|
+
[key, ttl, tags, update]
|
|
710
704
|
);
|
|
711
705
|
const invalidate = useCallback(async () => {
|
|
706
|
+
const owner = key;
|
|
712
707
|
if (!isOfflineStorageInitialized()) {
|
|
713
|
-
|
|
708
|
+
update(owner, { data: null });
|
|
714
709
|
return;
|
|
715
710
|
}
|
|
716
711
|
const storage = getOfflineStorage();
|
|
717
|
-
await storage.cache.invalidate(
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
setIsStale(false);
|
|
721
|
-
setTtlRemaining(null);
|
|
722
|
-
}
|
|
723
|
-
}, [key]);
|
|
712
|
+
await storage.cache.invalidate(owner);
|
|
713
|
+
update(owner, { data: null, isStale: false, ttlRemaining: null });
|
|
714
|
+
}, [key, update]);
|
|
724
715
|
const refresh = useCallback(
|
|
725
716
|
async (fetcher) => {
|
|
726
717
|
await invalidate();
|
|
@@ -735,26 +726,30 @@ function useOfflineCache(options) {
|
|
|
735
726
|
const storage = getOfflineStorage();
|
|
736
727
|
return storage.cache.has(key);
|
|
737
728
|
}, [key]);
|
|
738
|
-
|
|
729
|
+
useIsomorphicLayoutEffect(() => {
|
|
739
730
|
mountedRef.current = true;
|
|
731
|
+
keyRef.current = key;
|
|
732
|
+
return () => {
|
|
733
|
+
mountedRef.current = false;
|
|
734
|
+
};
|
|
735
|
+
}, [key]);
|
|
736
|
+
useEffect(() => {
|
|
740
737
|
if (isOfflineStorageInitialized()) {
|
|
741
738
|
const storage = getOfflineStorage();
|
|
742
739
|
storage.cache.get(key).then((result) => {
|
|
743
|
-
if (
|
|
744
|
-
updateFromResult(result);
|
|
740
|
+
if (result.hit && !result.stale) {
|
|
741
|
+
updateFromResult(key, result);
|
|
745
742
|
}
|
|
746
743
|
});
|
|
747
744
|
}
|
|
748
|
-
return () => {
|
|
749
|
-
mountedRef.current = false;
|
|
750
|
-
};
|
|
751
745
|
}, [key, updateFromResult]);
|
|
746
|
+
const current = state.owner === key ? state : emptyState(key);
|
|
752
747
|
return {
|
|
753
|
-
data,
|
|
754
|
-
isLoading,
|
|
755
|
-
isStale,
|
|
756
|
-
error,
|
|
757
|
-
ttlRemaining,
|
|
748
|
+
data: current.data,
|
|
749
|
+
isLoading: current.isLoading,
|
|
750
|
+
isStale: current.isStale,
|
|
751
|
+
error: current.error,
|
|
752
|
+
ttlRemaining: current.ttlRemaining,
|
|
758
753
|
get,
|
|
759
754
|
set,
|
|
760
755
|
invalidate,
|
|
@@ -895,8 +890,10 @@ function useDraftSync(options) {
|
|
|
895
890
|
const [lastResult, setLastResult] = useState(null);
|
|
896
891
|
const [error, setError] = useState(null);
|
|
897
892
|
const syncServiceRef = useRef(null);
|
|
893
|
+
const disposingRef = useRef(Promise.resolve());
|
|
898
894
|
const initSyncService = useCallback(
|
|
899
895
|
async (isCancelled) => {
|
|
896
|
+
await disposingRef.current;
|
|
900
897
|
if (!isOfflineStorageInitialized()) {
|
|
901
898
|
return null;
|
|
902
899
|
}
|
|
@@ -930,9 +927,12 @@ function useDraftSync(options) {
|
|
|
930
927
|
});
|
|
931
928
|
await service.initialize();
|
|
932
929
|
if (isCancelled()) {
|
|
933
|
-
service.dispose();
|
|
930
|
+
disposingRef.current = Promise.resolve(service.dispose());
|
|
934
931
|
return null;
|
|
935
932
|
}
|
|
933
|
+
setIsSyncing(false);
|
|
934
|
+
setLastResult(null);
|
|
935
|
+
setError(null);
|
|
936
936
|
setIsOnline(service.isOnline);
|
|
937
937
|
setLastSyncAt(service.lastSyncAt);
|
|
938
938
|
setPendingCount(service.pendingCount);
|
|
@@ -962,13 +962,14 @@ function useDraftSync(options) {
|
|
|
962
962
|
}
|
|
963
963
|
setIsSyncing(true);
|
|
964
964
|
setError(null);
|
|
965
|
+
const isCurrent = () => syncServiceRef.current === service;
|
|
965
966
|
try {
|
|
966
967
|
const result = await service.syncAll();
|
|
967
|
-
setPendingCount(service.pendingCount);
|
|
968
|
+
if (isCurrent()) setPendingCount(service.pendingCount);
|
|
968
969
|
return result;
|
|
969
970
|
} catch (err) {
|
|
970
971
|
const errorMsg = err instanceof Error ? err.message : "Sync failed";
|
|
971
|
-
setError(errorMsg);
|
|
972
|
+
if (isCurrent()) setError(errorMsg);
|
|
972
973
|
return {
|
|
973
974
|
success: false,
|
|
974
975
|
uploaded: 0,
|
|
@@ -981,7 +982,7 @@ function useDraftSync(options) {
|
|
|
981
982
|
durationMs: 0
|
|
982
983
|
};
|
|
983
984
|
} finally {
|
|
984
|
-
setIsSyncing(false);
|
|
985
|
+
if (isCurrent()) setIsSyncing(false);
|
|
985
986
|
}
|
|
986
987
|
}, []);
|
|
987
988
|
const syncDraft = useCallback(async (draftId) => {
|
|
@@ -1007,7 +1008,7 @@ function useDraftSync(options) {
|
|
|
1007
1008
|
throw new Error("Sync service not initialized");
|
|
1008
1009
|
}
|
|
1009
1010
|
await service.forcePush(draftId);
|
|
1010
|
-
setConflicts(service.getConflicts());
|
|
1011
|
+
if (syncServiceRef.current === service) setConflicts(service.getConflicts());
|
|
1011
1012
|
}, []);
|
|
1012
1013
|
const forcePull = useCallback(async (draftId) => {
|
|
1013
1014
|
const service = syncServiceRef.current;
|
|
@@ -1015,7 +1016,7 @@ function useDraftSync(options) {
|
|
|
1015
1016
|
throw new Error("Sync service not initialized");
|
|
1016
1017
|
}
|
|
1017
1018
|
await service.forcePull(draftId);
|
|
1018
|
-
setConflicts(service.getConflicts());
|
|
1019
|
+
if (syncServiceRef.current === service) setConflicts(service.getConflicts());
|
|
1019
1020
|
}, []);
|
|
1020
1021
|
const resolveConflict = useCallback(
|
|
1021
1022
|
async (draftId, resolution) => {
|
|
@@ -1024,7 +1025,7 @@ function useDraftSync(options) {
|
|
|
1024
1025
|
throw new Error("Sync service not initialized");
|
|
1025
1026
|
}
|
|
1026
1027
|
await service.resolveConflict(draftId, resolution);
|
|
1027
|
-
setConflicts(service.getConflicts());
|
|
1028
|
+
if (syncServiceRef.current === service) setConflicts(service.getConflicts());
|
|
1028
1029
|
},
|
|
1029
1030
|
[]
|
|
1030
1031
|
);
|
|
@@ -1035,8 +1036,9 @@ function useDraftSync(options) {
|
|
|
1035
1036
|
syncServiceRef.current?.stopAutoSync();
|
|
1036
1037
|
}, []);
|
|
1037
1038
|
const dispose = useCallback(() => {
|
|
1038
|
-
syncServiceRef.current
|
|
1039
|
+
const service = syncServiceRef.current;
|
|
1039
1040
|
syncServiceRef.current = null;
|
|
1041
|
+
if (service) disposingRef.current = Promise.resolve(service.dispose());
|
|
1040
1042
|
}, []);
|
|
1041
1043
|
useEffect(() => {
|
|
1042
1044
|
let cancelled = false;
|
|
@@ -1046,10 +1048,10 @@ function useDraftSync(options) {
|
|
|
1046
1048
|
});
|
|
1047
1049
|
return () => {
|
|
1048
1050
|
cancelled = true;
|
|
1049
|
-
service?.dispose();
|
|
1050
1051
|
if (syncServiceRef.current === service) {
|
|
1051
1052
|
syncServiceRef.current = null;
|
|
1052
1053
|
}
|
|
1054
|
+
if (service) disposingRef.current = Promise.resolve(service.dispose());
|
|
1053
1055
|
};
|
|
1054
1056
|
}, [initSyncService]);
|
|
1055
1057
|
return {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@classic-homes/offline-react",
|
|
3
|
-
"version": "0.2
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "React bindings for offline persistence and caching",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -51,11 +51,11 @@
|
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@testing-library/jest-dom": "^6.0.0",
|
|
54
|
-
"@testing-library/react": "^16.
|
|
54
|
+
"@testing-library/react": "^16.3.3",
|
|
55
55
|
"@types/react": "^18.2.0",
|
|
56
|
-
"@vitejs/plugin-react": "^6.
|
|
56
|
+
"@vitejs/plugin-react": "^6.3.0",
|
|
57
57
|
"@vitest/coverage-v8": "^4.1.10",
|
|
58
|
-
"fake-indexeddb": "^6.
|
|
58
|
+
"fake-indexeddb": "^6.2.8",
|
|
59
59
|
"jsdom": "^25.0.0",
|
|
60
60
|
"react": "^18.2.0",
|
|
61
61
|
"react-dom": "^18.2.0",
|