@legendapp/state 3.0.0-beta.3 → 3.0.0-beta.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-beta.3",
3
+ "version": "3.0.0-beta.4",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
package/react.d.mts CHANGED
@@ -77,7 +77,7 @@ interface BindKey<P> {
77
77
  defaultValue?: any;
78
78
  selector?: (propsOut: Record<string, any>, p: Observable<any>) => any;
79
79
  }
80
- type BindKeys<P = any> = Record<keyof P, BindKey<P>>;
80
+ type BindKeys<P = any> = Partial<Record<keyof P, BindKey<P>>>;
81
81
  type FCReactiveObject<T> = {
82
82
  [K in keyof T]: FC<ShapeWithNew$<T[K]>>;
83
83
  };
package/react.d.ts CHANGED
@@ -77,7 +77,7 @@ interface BindKey<P> {
77
77
  defaultValue?: any;
78
78
  selector?: (propsOut: Record<string, any>, p: Observable<any>) => any;
79
79
  }
80
- type BindKeys<P = any> = Record<keyof P, BindKey<P>>;
80
+ type BindKeys<P = any> = Partial<Record<keyof P, BindKey<P>>>;
81
81
  type FCReactiveObject<T> = {
82
82
  [K in keyof T]: FC<ShapeWithNew$<T[K]>>;
83
83
  };
package/sync.js CHANGED
@@ -848,7 +848,8 @@ function syncObservable(obs$, syncOptionsOrSynced) {
848
848
  localState.isApplyingPending = false;
849
849
  }
850
850
  };
851
- if (syncOptions.get) {
851
+ const { get, subscribe } = syncOptions;
852
+ if (get || subscribe) {
852
853
  sync = async () => {
853
854
  var _a;
854
855
  if (isSynced && (!getNodeValue(getNode(syncState$)).isSyncEnabled || state.shouldIgnoreUnobserved(node, sync))) {
@@ -861,8 +862,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
861
862
  }
862
863
  const lastSync = (_a = metadatas.get(obs$)) == null ? void 0 : _a.lastSync;
863
864
  const pending = localState.pendingChanges;
864
- const get = syncOptions.get;
865
- if (get) {
865
+ if (get || subscribe) {
866
866
  const { waitFor } = syncOptions;
867
867
  const runGet = () => {
868
868
  var _a2;
@@ -955,7 +955,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
955
955
  node.activationState.onChange = onChange;
956
956
  }
957
957
  if (!isSubscribed && syncOptions.subscribe) {
958
- const subscribe = syncOptions.subscribe;
958
+ const subscribe2 = syncOptions.subscribe;
959
959
  isSubscribed = true;
960
960
  const doSubscribe = () => {
961
961
  const subscribeParams = {
@@ -963,17 +963,27 @@ function syncObservable(obs$, syncOptionsOrSynced) {
963
963
  value$: obs$,
964
964
  lastSync,
965
965
  update: (params) => {
966
- state.when(syncState$.isLoaded, () => {
967
- state.when(waitFor || true, () => {
968
- params.mode || (params.mode = syncOptions.mode || "merge");
969
- onChange(params);
970
- });
971
- });
966
+ state.when(
967
+ () => !get || syncState$.isLoaded.get(),
968
+ () => {
969
+ state.when(waitFor || true, () => {
970
+ params.mode || (params.mode = syncOptions.mode || "merge");
971
+ onChange(params);
972
+ if (!syncState$.isLoaded.peek()) {
973
+ syncState$.assign({
974
+ isLoaded: syncStateValue.numPendingRemoteLoads < 1,
975
+ error: void 0,
976
+ isGetting: syncStateValue.numPendingGets > 0
977
+ });
978
+ }
979
+ });
980
+ }
981
+ );
972
982
  },
973
983
  refresh: () => state.when(syncState$.isLoaded, sync),
974
984
  onError: (error) => onGetError(error, { source: "subscribe", subscribeParams })
975
985
  };
976
- unsubscribe = subscribe(subscribeParams);
986
+ unsubscribe = subscribe2(subscribeParams);
977
987
  };
978
988
  if (waitFor) {
979
989
  state.whenReady(waitFor, doSubscribe);
@@ -982,85 +992,87 @@ function syncObservable(obs$, syncOptionsOrSynced) {
982
992
  }
983
993
  }
984
994
  const existingValue = getNodeValue(node);
985
- const onError = (error) => onGetError(error, { getParams, source: "get" });
986
- const getParams = {
987
- node,
988
- value$: obs$,
989
- value: state.isFunction(existingValue) || (existingValue == null ? void 0 : existingValue[symbolLinked]) ? void 0 : existingValue,
990
- mode: syncOptions.mode,
991
- refresh: sync,
992
- options: syncOptions,
993
- lastSync,
994
- updateLastSync: (lastSync2) => getParams.lastSync = lastSync2,
995
- onError,
996
- retryNum: 0,
997
- cancelRetry: false
998
- };
999
- let modeBeforeReset = void 0;
1000
- const beforeGetParams = {
1001
- value: getParams.value,
1002
- lastSync,
1003
- pendingChanges: pending && !state.isEmpty(pending) ? pending : void 0,
1004
- clearPendingChanges: async () => {
1005
- localState.pendingChanges = {};
1006
- await updateMetadataImmediate(obs$, localState, syncState$, syncOptions, {
1007
- pending: localState.pendingChanges
1008
- });
1009
- },
1010
- resetCache: () => {
1011
- var _a3;
1012
- modeBeforeReset = getParams.mode;
1013
- getParams.mode = "set";
1014
- return (_a3 = syncStateValue.resetPersistence) == null ? void 0 : _a3.call(syncStateValue);
1015
- },
1016
- cancel: false
1017
- };
1018
- (_a2 = syncOptions.onBeforeGet) == null ? void 0 : _a2.call(syncOptions, beforeGetParams);
1019
- if (!beforeGetParams.cancel) {
1020
- syncState$.assign({
1021
- numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
1022
- isGetting: true
1023
- });
1024
- const got = runWithRetry(
1025
- getParams,
1026
- syncOptions.retry,
1027
- (retryEvent) => {
1028
- const params = getParams;
1029
- params.cancelRetry = retryEvent.cancelRetry;
1030
- params.retryNum = retryEvent.retryNum;
1031
- return get(params);
1032
- },
1033
- onError
1034
- );
1035
- const numGets = node.numGets = (node.numGets || 0) + 1;
1036
- const handle = (value) => {
1037
- syncState$.numPendingGets.set((v) => v - 1);
1038
- if (isWaitingForLoad) {
1039
- isWaitingForLoad = false;
1040
- syncStateValue.numPendingRemoteLoads--;
1041
- }
1042
- if (numGets >= (node.getNumResolved || 0)) {
1043
- node.getNumResolved = node.numGets;
1044
- onChange({
1045
- value,
1046
- lastSync: getParams.lastSync,
1047
- mode: getParams.mode
995
+ if (get) {
996
+ const onError = (error) => onGetError(error, { getParams, source: "get" });
997
+ const getParams = {
998
+ node,
999
+ value$: obs$,
1000
+ value: state.isFunction(existingValue) || (existingValue == null ? void 0 : existingValue[symbolLinked]) ? void 0 : existingValue,
1001
+ mode: syncOptions.mode,
1002
+ refresh: sync,
1003
+ options: syncOptions,
1004
+ lastSync,
1005
+ updateLastSync: (lastSync2) => getParams.lastSync = lastSync2,
1006
+ onError,
1007
+ retryNum: 0,
1008
+ cancelRetry: false
1009
+ };
1010
+ let modeBeforeReset = void 0;
1011
+ const beforeGetParams = {
1012
+ value: getParams.value,
1013
+ lastSync,
1014
+ pendingChanges: pending && !state.isEmpty(pending) ? pending : void 0,
1015
+ clearPendingChanges: async () => {
1016
+ localState.pendingChanges = {};
1017
+ await updateMetadataImmediate(obs$, localState, syncState$, syncOptions, {
1018
+ pending: localState.pendingChanges
1048
1019
  });
1049
- }
1050
- if (modeBeforeReset) {
1051
- getParams.mode = modeBeforeReset;
1052
- modeBeforeReset = void 0;
1053
- }
1020
+ },
1021
+ resetCache: () => {
1022
+ var _a3;
1023
+ modeBeforeReset = getParams.mode;
1024
+ getParams.mode = "set";
1025
+ return (_a3 = syncStateValue.resetPersistence) == null ? void 0 : _a3.call(syncStateValue);
1026
+ },
1027
+ cancel: false
1028
+ };
1029
+ (_a2 = syncOptions.onBeforeGet) == null ? void 0 : _a2.call(syncOptions, beforeGetParams);
1030
+ if (!beforeGetParams.cancel) {
1054
1031
  syncState$.assign({
1055
- isLoaded: syncStateValue.numPendingRemoteLoads < 1,
1056
- error: void 0,
1057
- isGetting: syncStateValue.numPendingGets > 0
1032
+ numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
1033
+ isGetting: true
1058
1034
  });
1059
- };
1060
- if (state.isPromise(got)) {
1061
- got.then(handle).catch(onError);
1062
- } else {
1063
- handle(got);
1035
+ const got = runWithRetry(
1036
+ getParams,
1037
+ syncOptions.retry,
1038
+ (retryEvent) => {
1039
+ const params = getParams;
1040
+ params.cancelRetry = retryEvent.cancelRetry;
1041
+ params.retryNum = retryEvent.retryNum;
1042
+ return get(params);
1043
+ },
1044
+ onError
1045
+ );
1046
+ const numGets = node.numGets = (node.numGets || 0) + 1;
1047
+ const handle = (value) => {
1048
+ syncState$.numPendingGets.set((v) => v - 1);
1049
+ if (isWaitingForLoad) {
1050
+ isWaitingForLoad = false;
1051
+ syncStateValue.numPendingRemoteLoads--;
1052
+ }
1053
+ if (numGets >= (node.getNumResolved || 0)) {
1054
+ node.getNumResolved = node.numGets;
1055
+ onChange({
1056
+ value,
1057
+ lastSync: getParams.lastSync,
1058
+ mode: getParams.mode
1059
+ });
1060
+ }
1061
+ if (modeBeforeReset) {
1062
+ getParams.mode = modeBeforeReset;
1063
+ modeBeforeReset = void 0;
1064
+ }
1065
+ syncState$.assign({
1066
+ isLoaded: syncStateValue.numPendingRemoteLoads < 1,
1067
+ error: void 0,
1068
+ isGetting: syncStateValue.numPendingGets > 0
1069
+ });
1070
+ };
1071
+ if (state.isPromise(got)) {
1072
+ got.then(handle).catch(onError);
1073
+ } else {
1074
+ handle(got);
1075
+ }
1064
1076
  }
1065
1077
  }
1066
1078
  };
@@ -1132,7 +1144,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
1132
1144
  return true;
1133
1145
  };
1134
1146
  state.when(onAllPersistLoaded, function() {
1135
- if (syncOptions.get && syncOptions.syncMode === "auto") {
1147
+ if ((syncOptions.get || syncOptions.subscribe) && syncOptions.syncMode === "auto") {
1136
1148
  sync();
1137
1149
  }
1138
1150
  if ((syncOptions == null ? void 0 : syncOptions.set) || (syncOptions == null ? void 0 : syncOptions.persist)) {
package/sync.mjs CHANGED
@@ -846,7 +846,8 @@ function syncObservable(obs$, syncOptionsOrSynced) {
846
846
  localState.isApplyingPending = false;
847
847
  }
848
848
  };
849
- if (syncOptions.get) {
849
+ const { get, subscribe } = syncOptions;
850
+ if (get || subscribe) {
850
851
  sync = async () => {
851
852
  var _a;
852
853
  if (isSynced && (!getNodeValue(getNode(syncState$)).isSyncEnabled || shouldIgnoreUnobserved(node, sync))) {
@@ -859,8 +860,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
859
860
  }
860
861
  const lastSync = (_a = metadatas.get(obs$)) == null ? void 0 : _a.lastSync;
861
862
  const pending = localState.pendingChanges;
862
- const get = syncOptions.get;
863
- if (get) {
863
+ if (get || subscribe) {
864
864
  const { waitFor } = syncOptions;
865
865
  const runGet = () => {
866
866
  var _a2;
@@ -953,7 +953,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
953
953
  node.activationState.onChange = onChange;
954
954
  }
955
955
  if (!isSubscribed && syncOptions.subscribe) {
956
- const subscribe = syncOptions.subscribe;
956
+ const subscribe2 = syncOptions.subscribe;
957
957
  isSubscribed = true;
958
958
  const doSubscribe = () => {
959
959
  const subscribeParams = {
@@ -961,17 +961,27 @@ function syncObservable(obs$, syncOptionsOrSynced) {
961
961
  value$: obs$,
962
962
  lastSync,
963
963
  update: (params) => {
964
- when(syncState$.isLoaded, () => {
965
- when(waitFor || true, () => {
966
- params.mode || (params.mode = syncOptions.mode || "merge");
967
- onChange(params);
968
- });
969
- });
964
+ when(
965
+ () => !get || syncState$.isLoaded.get(),
966
+ () => {
967
+ when(waitFor || true, () => {
968
+ params.mode || (params.mode = syncOptions.mode || "merge");
969
+ onChange(params);
970
+ if (!syncState$.isLoaded.peek()) {
971
+ syncState$.assign({
972
+ isLoaded: syncStateValue.numPendingRemoteLoads < 1,
973
+ error: void 0,
974
+ isGetting: syncStateValue.numPendingGets > 0
975
+ });
976
+ }
977
+ });
978
+ }
979
+ );
970
980
  },
971
981
  refresh: () => when(syncState$.isLoaded, sync),
972
982
  onError: (error) => onGetError(error, { source: "subscribe", subscribeParams })
973
983
  };
974
- unsubscribe = subscribe(subscribeParams);
984
+ unsubscribe = subscribe2(subscribeParams);
975
985
  };
976
986
  if (waitFor) {
977
987
  whenReady(waitFor, doSubscribe);
@@ -980,85 +990,87 @@ function syncObservable(obs$, syncOptionsOrSynced) {
980
990
  }
981
991
  }
982
992
  const existingValue = getNodeValue(node);
983
- const onError = (error) => onGetError(error, { getParams, source: "get" });
984
- const getParams = {
985
- node,
986
- value$: obs$,
987
- value: isFunction(existingValue) || (existingValue == null ? void 0 : existingValue[symbolLinked]) ? void 0 : existingValue,
988
- mode: syncOptions.mode,
989
- refresh: sync,
990
- options: syncOptions,
991
- lastSync,
992
- updateLastSync: (lastSync2) => getParams.lastSync = lastSync2,
993
- onError,
994
- retryNum: 0,
995
- cancelRetry: false
996
- };
997
- let modeBeforeReset = void 0;
998
- const beforeGetParams = {
999
- value: getParams.value,
1000
- lastSync,
1001
- pendingChanges: pending && !isEmpty(pending) ? pending : void 0,
1002
- clearPendingChanges: async () => {
1003
- localState.pendingChanges = {};
1004
- await updateMetadataImmediate(obs$, localState, syncState$, syncOptions, {
1005
- pending: localState.pendingChanges
1006
- });
1007
- },
1008
- resetCache: () => {
1009
- var _a3;
1010
- modeBeforeReset = getParams.mode;
1011
- getParams.mode = "set";
1012
- return (_a3 = syncStateValue.resetPersistence) == null ? void 0 : _a3.call(syncStateValue);
1013
- },
1014
- cancel: false
1015
- };
1016
- (_a2 = syncOptions.onBeforeGet) == null ? void 0 : _a2.call(syncOptions, beforeGetParams);
1017
- if (!beforeGetParams.cancel) {
1018
- syncState$.assign({
1019
- numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
1020
- isGetting: true
1021
- });
1022
- const got = runWithRetry(
1023
- getParams,
1024
- syncOptions.retry,
1025
- (retryEvent) => {
1026
- const params = getParams;
1027
- params.cancelRetry = retryEvent.cancelRetry;
1028
- params.retryNum = retryEvent.retryNum;
1029
- return get(params);
1030
- },
1031
- onError
1032
- );
1033
- const numGets = node.numGets = (node.numGets || 0) + 1;
1034
- const handle = (value) => {
1035
- syncState$.numPendingGets.set((v) => v - 1);
1036
- if (isWaitingForLoad) {
1037
- isWaitingForLoad = false;
1038
- syncStateValue.numPendingRemoteLoads--;
1039
- }
1040
- if (numGets >= (node.getNumResolved || 0)) {
1041
- node.getNumResolved = node.numGets;
1042
- onChange({
1043
- value,
1044
- lastSync: getParams.lastSync,
1045
- mode: getParams.mode
993
+ if (get) {
994
+ const onError = (error) => onGetError(error, { getParams, source: "get" });
995
+ const getParams = {
996
+ node,
997
+ value$: obs$,
998
+ value: isFunction(existingValue) || (existingValue == null ? void 0 : existingValue[symbolLinked]) ? void 0 : existingValue,
999
+ mode: syncOptions.mode,
1000
+ refresh: sync,
1001
+ options: syncOptions,
1002
+ lastSync,
1003
+ updateLastSync: (lastSync2) => getParams.lastSync = lastSync2,
1004
+ onError,
1005
+ retryNum: 0,
1006
+ cancelRetry: false
1007
+ };
1008
+ let modeBeforeReset = void 0;
1009
+ const beforeGetParams = {
1010
+ value: getParams.value,
1011
+ lastSync,
1012
+ pendingChanges: pending && !isEmpty(pending) ? pending : void 0,
1013
+ clearPendingChanges: async () => {
1014
+ localState.pendingChanges = {};
1015
+ await updateMetadataImmediate(obs$, localState, syncState$, syncOptions, {
1016
+ pending: localState.pendingChanges
1046
1017
  });
1047
- }
1048
- if (modeBeforeReset) {
1049
- getParams.mode = modeBeforeReset;
1050
- modeBeforeReset = void 0;
1051
- }
1018
+ },
1019
+ resetCache: () => {
1020
+ var _a3;
1021
+ modeBeforeReset = getParams.mode;
1022
+ getParams.mode = "set";
1023
+ return (_a3 = syncStateValue.resetPersistence) == null ? void 0 : _a3.call(syncStateValue);
1024
+ },
1025
+ cancel: false
1026
+ };
1027
+ (_a2 = syncOptions.onBeforeGet) == null ? void 0 : _a2.call(syncOptions, beforeGetParams);
1028
+ if (!beforeGetParams.cancel) {
1052
1029
  syncState$.assign({
1053
- isLoaded: syncStateValue.numPendingRemoteLoads < 1,
1054
- error: void 0,
1055
- isGetting: syncStateValue.numPendingGets > 0
1030
+ numPendingGets: (syncStateValue.numPendingGets || 0) + 1,
1031
+ isGetting: true
1056
1032
  });
1057
- };
1058
- if (isPromise$1(got)) {
1059
- got.then(handle).catch(onError);
1060
- } else {
1061
- handle(got);
1033
+ const got = runWithRetry(
1034
+ getParams,
1035
+ syncOptions.retry,
1036
+ (retryEvent) => {
1037
+ const params = getParams;
1038
+ params.cancelRetry = retryEvent.cancelRetry;
1039
+ params.retryNum = retryEvent.retryNum;
1040
+ return get(params);
1041
+ },
1042
+ onError
1043
+ );
1044
+ const numGets = node.numGets = (node.numGets || 0) + 1;
1045
+ const handle = (value) => {
1046
+ syncState$.numPendingGets.set((v) => v - 1);
1047
+ if (isWaitingForLoad) {
1048
+ isWaitingForLoad = false;
1049
+ syncStateValue.numPendingRemoteLoads--;
1050
+ }
1051
+ if (numGets >= (node.getNumResolved || 0)) {
1052
+ node.getNumResolved = node.numGets;
1053
+ onChange({
1054
+ value,
1055
+ lastSync: getParams.lastSync,
1056
+ mode: getParams.mode
1057
+ });
1058
+ }
1059
+ if (modeBeforeReset) {
1060
+ getParams.mode = modeBeforeReset;
1061
+ modeBeforeReset = void 0;
1062
+ }
1063
+ syncState$.assign({
1064
+ isLoaded: syncStateValue.numPendingRemoteLoads < 1,
1065
+ error: void 0,
1066
+ isGetting: syncStateValue.numPendingGets > 0
1067
+ });
1068
+ };
1069
+ if (isPromise$1(got)) {
1070
+ got.then(handle).catch(onError);
1071
+ } else {
1072
+ handle(got);
1073
+ }
1062
1074
  }
1063
1075
  }
1064
1076
  };
@@ -1130,7 +1142,7 @@ function syncObservable(obs$, syncOptionsOrSynced) {
1130
1142
  return true;
1131
1143
  };
1132
1144
  when(onAllPersistLoaded, function() {
1133
- if (syncOptions.get && syncOptions.syncMode === "auto") {
1145
+ if ((syncOptions.get || syncOptions.subscribe) && syncOptions.syncMode === "auto") {
1134
1146
  sync();
1135
1147
  }
1136
1148
  if ((syncOptions == null ? void 0 : syncOptions.set) || (syncOptions == null ? void 0 : syncOptions.persist)) {