@legendapp/state 3.0.0-alpha.34 → 3.0.0-alpha.35
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/config.d.mts +1 -1
- package/config.d.ts +1 -1
- package/config.js +18 -12
- package/config.mjs +18 -12
- package/index.d.mts +10 -3
- package/index.d.ts +10 -3
- package/index.js +79 -42
- package/index.mjs +76 -42
- package/{observableInterfaces-CZR3_8mM.d.mts → observableInterfaces-CgBddSU6.d.mts} +6 -2
- package/{observableInterfaces-CZR3_8mM.d.ts → observableInterfaces-CgBddSU6.d.ts} +6 -2
- package/package.json +1 -1
- package/persist-plugins/async-storage.d.mts +2 -2
- package/persist-plugins/async-storage.d.ts +2 -2
- package/persist-plugins/async-storage.js +3 -7
- package/persist-plugins/async-storage.mjs +3 -7
- package/persist-plugins/indexeddb.d.mts +2 -2
- package/persist-plugins/indexeddb.d.ts +2 -2
- package/persist-plugins/indexeddb.js +3 -7
- package/persist-plugins/indexeddb.mjs +3 -7
- package/persist-plugins/mmkv.d.mts +2 -2
- package/persist-plugins/mmkv.d.ts +2 -2
- package/persist-plugins/mmkv.js +3 -7
- package/persist-plugins/mmkv.mjs +3 -7
- package/sync-plugins/crud.js +6 -7
- package/sync-plugins/crud.mjs +7 -8
- package/sync-plugins/keel.d.mts +28 -28
- package/sync-plugins/keel.d.ts +28 -28
- package/sync-plugins/keel.js +106 -82
- package/sync-plugins/keel.mjs +108 -82
- package/sync.d.mts +11 -13
- package/sync.d.ts +11 -13
- package/sync.js +149 -130
- package/sync.mjs +150 -131
package/sync.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isObject, isDate, isNullOrUndefined, isString, endBatch, beginBatch, isFunction, syncState, when, linked, internal, observable, isPromise as isPromise$1, mergeIntoObservable, isEmpty, shouldIgnoreUnobserved, whenReady, trackSelector, constructObjectWithPath, setAtPath, isArray } from '@legendapp/state';
|
|
1
|
+
import { isObject, isDate, isNullOrUndefined, isString, endBatch, beginBatch, isFunction, syncState, when, linked, internal, observable, isPromise as isPromise$1, mergeIntoObservable, isEmpty, shouldIgnoreUnobserved, whenReady, trackSelector, constructObjectWithPath, setAtPath, isPlainObject, ObservableHint, isArray } from '@legendapp/state';
|
|
2
2
|
|
|
3
3
|
// src/sync/configureObservableSync.ts
|
|
4
4
|
var observableSyncConfiguration = {};
|
|
@@ -176,7 +176,7 @@ function runWithRetry(state, retryOptions, fn, onError) {
|
|
|
176
176
|
clearTimeout(timeoutRetry);
|
|
177
177
|
}
|
|
178
178
|
if (onError) {
|
|
179
|
-
onError(error);
|
|
179
|
+
onError(error, state);
|
|
180
180
|
}
|
|
181
181
|
if (!state.cancelRetry) {
|
|
182
182
|
const timeout = createRetryTimeout(retryOptions, state.retryNum, () => {
|
|
@@ -251,7 +251,7 @@ async function updateMetadataImmediate(value$, localState, syncState2, syncOptio
|
|
|
251
251
|
await Promise.all(saves);
|
|
252
252
|
}
|
|
253
253
|
const { pluginPersist } = localState;
|
|
254
|
-
const { table, config } = parseLocalConfig(syncOptions
|
|
254
|
+
const { table, config } = parseLocalConfig(syncOptions.persist);
|
|
255
255
|
const oldMetadata = metadatas.get(value$);
|
|
256
256
|
const { lastSync } = newMetadata;
|
|
257
257
|
const metadata = Object.assign({}, oldMetadata, newMetadata);
|
|
@@ -581,96 +581,104 @@ async function doChangeRemote(changeInfo) {
|
|
|
581
581
|
}
|
|
582
582
|
state$.numPendingSets.set((v) => (v || 0) + 1);
|
|
583
583
|
state$.isSetting.set(true);
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
const onError = (error) => {
|
|
587
|
-
var _a2;
|
|
588
|
-
state$.error.set(error);
|
|
589
|
-
(_a2 = syncOptions.onSetError) == null ? void 0 : _a2.call(syncOptions, error, {
|
|
590
|
-
setParams,
|
|
591
|
-
source: "set",
|
|
592
|
-
value$: obs$
|
|
593
|
-
});
|
|
584
|
+
const beforeSetParams = {
|
|
585
|
+
cancel: false
|
|
594
586
|
};
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
if (
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
value
|
|
607
|
-
|
|
608
|
-
};
|
|
609
|
-
} else {
|
|
610
|
-
updateResult = params;
|
|
587
|
+
onBeforeSet == null ? void 0 : onBeforeSet(beforeSetParams);
|
|
588
|
+
if (!beforeSetParams.cancel) {
|
|
589
|
+
let updateResult = void 0;
|
|
590
|
+
let errorHandled = false;
|
|
591
|
+
const onError = (error, retryParams) => {
|
|
592
|
+
var _a2;
|
|
593
|
+
state$.error.set(error);
|
|
594
|
+
if (!errorHandled) {
|
|
595
|
+
(_a2 = syncOptions.onError) == null ? void 0 : _a2.call(syncOptions, error, {
|
|
596
|
+
setParams,
|
|
597
|
+
source: "set",
|
|
598
|
+
value$: obs$,
|
|
599
|
+
retryParams
|
|
600
|
+
});
|
|
611
601
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
602
|
+
errorHandled = true;
|
|
603
|
+
};
|
|
604
|
+
const setParams = {
|
|
605
|
+
node,
|
|
606
|
+
value$: obs$,
|
|
607
|
+
changes: changesRemote,
|
|
608
|
+
value,
|
|
609
|
+
onError,
|
|
610
|
+
update: (params) => {
|
|
611
|
+
if (updateResult) {
|
|
612
|
+
const { value: value2, lastSync, mode } = params;
|
|
613
|
+
updateResult = {
|
|
614
|
+
lastSync: Math.max(updateResult.lastSync || 0, lastSync || 0),
|
|
615
|
+
value: deepMerge(updateResult.value, value2),
|
|
616
|
+
mode
|
|
617
|
+
};
|
|
618
|
+
} else {
|
|
619
|
+
updateResult = params;
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
refresh: syncState2.sync,
|
|
623
|
+
retryNum: 0,
|
|
624
|
+
cancelRetry: false
|
|
625
|
+
};
|
|
626
|
+
const savedPromise = runWithRetry(
|
|
627
|
+
setParams,
|
|
628
|
+
syncOptions.retry,
|
|
629
|
+
async () => {
|
|
630
|
+
return syncOptions.set(setParams);
|
|
631
|
+
},
|
|
632
|
+
onError
|
|
633
|
+
);
|
|
634
|
+
let didError = false;
|
|
635
|
+
if (isPromise$1(savedPromise)) {
|
|
636
|
+
await savedPromise.catch(() => {
|
|
637
|
+
didError = true;
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
if (!didError) {
|
|
641
|
+
const pathStrs = Array.from(new Set(changesRemote.map((change) => change.pathStr)));
|
|
642
|
+
const { value: changes, lastSync } = updateResult || {};
|
|
643
|
+
if (pathStrs.length > 0) {
|
|
644
|
+
let transformedChanges = void 0;
|
|
645
|
+
const metadata = {};
|
|
646
|
+
if (saveLocal) {
|
|
647
|
+
const pendingMetadata = (_b = pluginPersist.getMetadata(table, configLocal)) == null ? void 0 : _b.pending;
|
|
648
|
+
const pending = localState.pendingChanges;
|
|
649
|
+
for (let i = 0; i < pathStrs.length; i++) {
|
|
650
|
+
const pathStr = pathStrs[i];
|
|
651
|
+
if (pendingMetadata == null ? void 0 : pendingMetadata[pathStr]) {
|
|
652
|
+
delete pendingMetadata[pathStr];
|
|
653
|
+
metadata.pending = pendingMetadata;
|
|
654
|
+
}
|
|
655
|
+
if (pending == null ? void 0 : pending[pathStr]) {
|
|
656
|
+
delete pending[pathStr];
|
|
657
|
+
}
|
|
647
658
|
}
|
|
648
|
-
if (
|
|
649
|
-
|
|
659
|
+
if (lastSync) {
|
|
660
|
+
metadata.lastSync = lastSync;
|
|
650
661
|
}
|
|
651
662
|
}
|
|
652
|
-
if (
|
|
653
|
-
|
|
663
|
+
if (changes && !isEmpty(changes)) {
|
|
664
|
+
transformedChanges = transformLoadData(changes, syncOptions, false, "set");
|
|
654
665
|
}
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
if (isPromise$1(transformedChanges)) {
|
|
661
|
-
transformedChanges = await transformedChanges;
|
|
666
|
+
if (transformedChanges !== void 0) {
|
|
667
|
+
if (isPromise$1(transformedChanges)) {
|
|
668
|
+
transformedChanges = await transformedChanges;
|
|
669
|
+
}
|
|
670
|
+
onChangeRemote(() => mergeIntoObservable(obs$, transformedChanges));
|
|
662
671
|
}
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
updateMetadata(obs$, localState, syncState2, syncOptions, metadata);
|
|
672
|
+
if (saveLocal) {
|
|
673
|
+
if (shouldSaveMetadata && !isEmpty(metadata)) {
|
|
674
|
+
updateMetadata(obs$, localState, syncState2, syncOptions, metadata);
|
|
675
|
+
}
|
|
668
676
|
}
|
|
669
677
|
}
|
|
678
|
+
state$.numPendingSets.set((v) => v - 1);
|
|
679
|
+
state$.isSetting.set(state$.numPendingSets.peek() > 0);
|
|
680
|
+
onAfterSet == null ? void 0 : onAfterSet();
|
|
670
681
|
}
|
|
671
|
-
state$.numPendingSets.set((v) => v - 1);
|
|
672
|
-
state$.isSetting.set(state$.numPendingSets.peek() > 0);
|
|
673
|
-
onAfterSet == null ? void 0 : onAfterSet();
|
|
674
682
|
}
|
|
675
683
|
}
|
|
676
684
|
}
|
|
@@ -708,7 +716,7 @@ async function loadLocal(value$, syncOptions, syncState$, localState) {
|
|
|
708
716
|
throw new Error("Local persist is not configured");
|
|
709
717
|
}
|
|
710
718
|
if (!mapSyncPlugins.has(PersistPlugin)) {
|
|
711
|
-
const persistPlugin2 = new PersistPlugin();
|
|
719
|
+
const persistPlugin2 = isFunction(PersistPlugin) ? new PersistPlugin() : PersistPlugin;
|
|
712
720
|
const mapValue = { plugin: persistPlugin2, initialized: observable(false) };
|
|
713
721
|
mapSyncPlugins.set(PersistPlugin, mapValue);
|
|
714
722
|
if (persistPlugin2.initialize) {
|
|
@@ -790,10 +798,14 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
790
798
|
const syncStateValue = getNodeValue(getNode(syncState$));
|
|
791
799
|
allSyncStates.set(syncState$, node);
|
|
792
800
|
syncStateValue.getPendingChanges = () => localState.pendingChanges;
|
|
801
|
+
let errorHandled = false;
|
|
793
802
|
const onGetError = (error, params) => {
|
|
794
803
|
var _a;
|
|
795
804
|
syncState$.error.set(error);
|
|
796
|
-
(
|
|
805
|
+
if (!errorHandled) {
|
|
806
|
+
(_a = syncOptions.onError) == null ? void 0 : _a.call(syncOptions, error, { ...params, value$: obs$ });
|
|
807
|
+
}
|
|
808
|
+
errorHandled = true;
|
|
797
809
|
};
|
|
798
810
|
loadLocal(obs$, syncOptions, syncState$, localState);
|
|
799
811
|
let isWaitingForLoad = !!syncOptions.get;
|
|
@@ -901,6 +913,9 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
901
913
|
}
|
|
902
914
|
}
|
|
903
915
|
onChangeRemote(() => {
|
|
916
|
+
if (isPlainObject(value)) {
|
|
917
|
+
value = ObservableHint.plain(value);
|
|
918
|
+
}
|
|
904
919
|
if (mode === "assign") {
|
|
905
920
|
obs$.assign(value);
|
|
906
921
|
} else if (mode === "append") {
|
|
@@ -972,7 +987,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
972
987
|
cancelRetry: false
|
|
973
988
|
};
|
|
974
989
|
let modeBeforeReset = void 0;
|
|
975
|
-
|
|
990
|
+
const beforeGetParams = {
|
|
976
991
|
value: getParams.value,
|
|
977
992
|
lastSync,
|
|
978
993
|
pendingChanges: pending && !isEmpty(pending) ? pending : void 0,
|
|
@@ -987,52 +1002,56 @@ function syncObservable(obs$, syncOptionsOrSynced) {
|
|
|
987
1002
|
modeBeforeReset = getParams.mode;
|
|
988
1003
|
getParams.mode = "set";
|
|
989
1004
|
return (_a3 = syncStateValue.clearPersist) == null ? void 0 : _a3.call(syncStateValue);
|
|
990
|
-
}
|
|
991
|
-
});
|
|
992
|
-
syncState$.assign({
|
|
993
|
-
numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
|
|
994
|
-
isGetting: true
|
|
995
|
-
});
|
|
996
|
-
const got = runWithRetry(
|
|
997
|
-
getParams,
|
|
998
|
-
syncOptions.retry,
|
|
999
|
-
(retryEvent) => {
|
|
1000
|
-
const params = getParams;
|
|
1001
|
-
params.cancelRetry = retryEvent.cancelRetry;
|
|
1002
|
-
params.retryNum = retryEvent.retryNum;
|
|
1003
|
-
return get(params);
|
|
1004
1005
|
},
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
syncState$.numPendingGets.set((v) => v - 1);
|
|
1010
|
-
if (isWaitingForLoad) {
|
|
1011
|
-
isWaitingForLoad = false;
|
|
1012
|
-
syncStateValue.numPendingRemoteLoads--;
|
|
1013
|
-
}
|
|
1014
|
-
if (numGets >= (node.getNumResolved || 0)) {
|
|
1015
|
-
node.getNumResolved = node.numGets;
|
|
1016
|
-
onChange({
|
|
1017
|
-
value,
|
|
1018
|
-
lastSync: getParams.lastSync,
|
|
1019
|
-
mode: getParams.mode
|
|
1020
|
-
});
|
|
1021
|
-
}
|
|
1022
|
-
if (modeBeforeReset) {
|
|
1023
|
-
getParams.mode = modeBeforeReset;
|
|
1024
|
-
modeBeforeReset = void 0;
|
|
1025
|
-
}
|
|
1006
|
+
cancel: false
|
|
1007
|
+
};
|
|
1008
|
+
(_a2 = syncOptions.onBeforeGet) == null ? void 0 : _a2.call(syncOptions, beforeGetParams);
|
|
1009
|
+
if (!beforeGetParams.cancel) {
|
|
1026
1010
|
syncState$.assign({
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
isGetting: syncStateValue.numPendingGets > 0
|
|
1011
|
+
numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
|
|
1012
|
+
isGetting: true
|
|
1030
1013
|
});
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1014
|
+
const got = runWithRetry(
|
|
1015
|
+
getParams,
|
|
1016
|
+
syncOptions.retry,
|
|
1017
|
+
(retryEvent) => {
|
|
1018
|
+
const params = getParams;
|
|
1019
|
+
params.cancelRetry = retryEvent.cancelRetry;
|
|
1020
|
+
params.retryNum = retryEvent.retryNum;
|
|
1021
|
+
return get(params);
|
|
1022
|
+
},
|
|
1023
|
+
onError
|
|
1024
|
+
);
|
|
1025
|
+
const numGets = node.numGets = (node.numGets || 0) + 1;
|
|
1026
|
+
const handle = (value) => {
|
|
1027
|
+
syncState$.numPendingGets.set((v) => v - 1);
|
|
1028
|
+
if (isWaitingForLoad) {
|
|
1029
|
+
isWaitingForLoad = false;
|
|
1030
|
+
syncStateValue.numPendingRemoteLoads--;
|
|
1031
|
+
}
|
|
1032
|
+
if (numGets >= (node.getNumResolved || 0)) {
|
|
1033
|
+
node.getNumResolved = node.numGets;
|
|
1034
|
+
onChange({
|
|
1035
|
+
value,
|
|
1036
|
+
lastSync: getParams.lastSync,
|
|
1037
|
+
mode: getParams.mode
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
if (modeBeforeReset) {
|
|
1041
|
+
getParams.mode = modeBeforeReset;
|
|
1042
|
+
modeBeforeReset = void 0;
|
|
1043
|
+
}
|
|
1044
|
+
syncState$.assign({
|
|
1045
|
+
isLoaded: syncStateValue.numPendingRemoteLoads < 1,
|
|
1046
|
+
error: void 0,
|
|
1047
|
+
isGetting: syncStateValue.numPendingGets > 0
|
|
1048
|
+
});
|
|
1049
|
+
};
|
|
1050
|
+
if (isPromise$1(got)) {
|
|
1051
|
+
got.then(handle);
|
|
1052
|
+
} else {
|
|
1053
|
+
handle(got);
|
|
1054
|
+
}
|
|
1036
1055
|
}
|
|
1037
1056
|
};
|
|
1038
1057
|
if (waitFor) {
|
|
@@ -1175,11 +1194,11 @@ function installPersistActivateNode() {
|
|
|
1175
1194
|
}
|
|
1176
1195
|
var { deepMerge: deepMerge2 } = internal;
|
|
1177
1196
|
function configureSynced(fnOrOrigOptions, origOptions) {
|
|
1178
|
-
const fn = origOptions ? fnOrOrigOptions :
|
|
1197
|
+
const fn = origOptions ? fnOrOrigOptions : synced;
|
|
1179
1198
|
origOptions = origOptions != null ? origOptions : fnOrOrigOptions;
|
|
1180
1199
|
return (options) => {
|
|
1181
1200
|
const merged = deepMerge2(origOptions, options);
|
|
1182
|
-
return fn
|
|
1201
|
+
return fn(merged);
|
|
1183
1202
|
};
|
|
1184
1203
|
}
|
|
1185
1204
|
|