@legendapp/state 2.2.0-next.33 → 2.2.0-next.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/index.js CHANGED
@@ -15,7 +15,10 @@ function isFunction(obj) {
15
15
  }
16
16
  function isPrimitive(arg) {
17
17
  const type = typeof arg;
18
- return arg !== undefined && type !== 'object' && type !== 'function';
18
+ return arg !== undefined && (isDate(arg) || (type !== 'object' && type !== 'function'));
19
+ }
20
+ function isDate(obj) {
21
+ return obj instanceof Date;
19
22
  }
20
23
  function isSymbol(obj) {
21
24
  return typeof obj === 'symbol';
@@ -104,7 +107,8 @@ function setNodeValue(node, newValue) {
104
107
  // If setting an observable, set a link to the observable instead
105
108
  if (isObservable(newValue) && !isComputed(newValue)) {
106
109
  const val = newValue;
107
- node.lazy = () => val;
110
+ node.lazy = true;
111
+ node.lazyFn = () => val;
108
112
  newValue = undefined;
109
113
  }
110
114
  try {
@@ -140,12 +144,6 @@ function getNodeValue(node) {
140
144
  }
141
145
  return child;
142
146
  }
143
- const cloneFunction = (originalFunction) => {
144
- const length = originalFunction.length;
145
- return length > 1
146
- ? (arg1, arg2) => originalFunction(arg1, arg2)
147
- : (...args) => originalFunction(...args);
148
- };
149
147
  function getChildNode(node, key, asFunction) {
150
148
  var _a;
151
149
  // Get the child by key
@@ -158,20 +156,16 @@ function getChildNode(node, key, asFunction) {
158
156
  key,
159
157
  lazy: true,
160
158
  };
161
- if (asFunction) {
162
- child = Object.assign(cloneFunction(asFunction), child);
163
- }
164
- else {
165
- if (node.activationState) {
166
- const { lookup } = node.activationState;
167
- if (lookup) {
168
- child = Object.assign(lookup.bind(node, key), child);
169
- if (isFunction(child)) {
170
- extractFunction(node, key, child);
171
- }
172
- }
159
+ if (node.activationState) {
160
+ const { lookup } = node.activationState;
161
+ if (lookup) {
162
+ asFunction = lookup.bind(node, key);
173
163
  }
174
164
  }
165
+ if (asFunction) {
166
+ child = Object.assign(() => { }, child);
167
+ child.lazyFn = asFunction;
168
+ }
175
169
  if (!node.children) {
176
170
  node.children = new Map();
177
171
  }
@@ -194,17 +188,19 @@ function ensureNodeValue(node) {
194
188
  }
195
189
  function findIDKey(obj, node) {
196
190
  var _a, _b;
197
- let idKey = isObject(obj)
198
- ? 'id' in obj
199
- ? 'id'
200
- : 'key' in obj
201
- ? 'key'
202
- : '_id' in obj
203
- ? '_id'
204
- : '__id' in obj
205
- ? '__id'
206
- : undefined
207
- : undefined;
191
+ let idKey = isObservable(obj)
192
+ ? undefined
193
+ : isObject(obj)
194
+ ? 'id' in obj
195
+ ? 'id'
196
+ : 'key' in obj
197
+ ? 'key'
198
+ : '_id' in obj
199
+ ? '_id'
200
+ : '__id' in obj
201
+ ? '__id'
202
+ : undefined
203
+ : undefined;
208
204
  if (!idKey && node.parent) {
209
205
  const k = node.key + '_keyExtractor';
210
206
  const keyExtractor = (_b = (_a = node.functions) === null || _a === void 0 ? void 0 : _a.get(k)) !== null && _b !== void 0 ? _b : getNodeValue(node.parent)[node.key + '_keyExtractor'];
@@ -215,11 +211,9 @@ function findIDKey(obj, node) {
215
211
  return idKey;
216
212
  }
217
213
  function extractFunction(node, key, fnOrComputed, computedChildNode) {
218
- var _a;
219
214
  if (!node.functions) {
220
215
  node.functions = new Map();
221
216
  }
222
- (_a = node.children) === null || _a === void 0 ? void 0 : _a.delete(key);
223
217
  node.functions.set(key, fnOrComputed);
224
218
  if (computedChildNode) {
225
219
  computedChildNode.parentOther = getChildNode(node, key);
@@ -494,7 +488,8 @@ function createObservable(value, makePrimitive, extractPromise, createObject, cr
494
488
  lazy: true,
495
489
  };
496
490
  if (valueIsFunction) {
497
- node = Object.assign(cloneFunction(value), node);
491
+ node = Object.assign(() => { }, node);
492
+ node.lazyFn = value;
498
493
  }
499
494
  const prim = makePrimitive || isActualPrimitive(value);
500
495
  const obs = prim
@@ -960,7 +955,6 @@ function runWithRetry(node, state, fn) {
960
955
  resolve(val);
961
956
  })
962
957
  .catch(() => {
963
- node.activationState.persistedRetry = true;
964
958
  state.attemptNum++;
965
959
  if (timeoutRetry) {
966
960
  clearTimeout(timeoutRetry);
@@ -971,6 +965,9 @@ function runWithRetry(node, state, fn) {
971
965
  run();
972
966
  });
973
967
  }
968
+ })
969
+ .finally(() => {
970
+ node.activationState.persistedRetry = false;
974
971
  });
975
972
  };
976
973
  run();
@@ -1153,7 +1150,11 @@ function updateNodes(parent, obj, prevValue) {
1153
1150
  ? idField(value)
1154
1151
  : value[idField]
1155
1152
  : undefined;
1156
- let child = getChildNode(parent, key);
1153
+ let child = getChildNode(parent, key, isFunction(value) ? value : undefined);
1154
+ if (child.linkedToNode && (isFunction(value) || isObservable(value))) {
1155
+ reactivateNode(child, value);
1156
+ peek(child);
1157
+ }
1157
1158
  // Detect moves within an array. Need to move the original proxy to the new position to keep
1158
1159
  // the proxy stable, so that listeners to this node will be unaffected by the array shift.
1159
1160
  if (isArr && id !== undefined) {
@@ -1183,7 +1184,7 @@ function updateNodes(parent, obj, prevValue) {
1183
1184
  if (isDiff) {
1184
1185
  // Array has a new / modified element
1185
1186
  // If object iterate through its children
1186
- if (isFunction(value)) {
1187
+ if (isFunction(value) || isObservable(value)) {
1187
1188
  extractFunctionOrComputed(parent, obj, key, value);
1188
1189
  }
1189
1190
  else if (isPrimitive(value)) {
@@ -1259,6 +1260,7 @@ const proxyHandler = {
1259
1260
  // The exception is onChange because it needs to listen to this node for changes.
1260
1261
  // This needs to be below peek because it activates there.
1261
1262
  if (node.linkedToNode && p !== 'onChange') {
1263
+ updateTracking(node);
1262
1264
  return proxyHandler.get(node.linkedToNode, p, receiver);
1263
1265
  }
1264
1266
  if (value instanceof Map || value instanceof WeakMap || value instanceof Set || value instanceof WeakSet) {
@@ -1442,7 +1444,7 @@ const proxyHandler = {
1442
1444
  },
1443
1445
  apply(target, thisArg, argArray) {
1444
1446
  // If it's a function call it as a function
1445
- return Reflect.apply(target, thisArg, argArray);
1447
+ return Reflect.apply(target.lazyFn || target, thisArg, argArray);
1446
1448
  },
1447
1449
  };
1448
1450
  function set(node, newValue) {
@@ -1675,6 +1677,9 @@ function extractFunctionOrComputed(node, obj, k, v) {
1675
1677
  if (childNode === null || childNode === void 0 ? void 0 : childNode.isComputed) {
1676
1678
  extractFunction(node, k, v, childNode);
1677
1679
  }
1680
+ else if (isObservable(v)) {
1681
+ extractFunction(node, k, v);
1682
+ }
1678
1683
  else {
1679
1684
  return true;
1680
1685
  }
@@ -1696,6 +1701,7 @@ function peek(node) {
1696
1701
  // If node is not yet lazily computed go do that
1697
1702
  const lazy = node.lazy;
1698
1703
  if (lazy) {
1704
+ const lazyFn = node.lazyFn;
1699
1705
  delete node.lazy;
1700
1706
  if (isFunction(node) || isFunction(lazy)) {
1701
1707
  if (node.parent) {
@@ -1704,7 +1710,7 @@ function peek(node) {
1704
1710
  delete parentValue[node.key];
1705
1711
  }
1706
1712
  }
1707
- value = activateNodeFunction(node, lazy);
1713
+ value = activateNodeFunction(node, lazyFn);
1708
1714
  }
1709
1715
  }
1710
1716
  if (lazy || node.needsExtract) {
@@ -1718,13 +1724,23 @@ function peek(node) {
1718
1724
  checkActivate(node);
1719
1725
  return value;
1720
1726
  }
1727
+ function reactivateNode(node, lazyFn) {
1728
+ var _a, _b;
1729
+ (_a = node.activatedObserveDispose) === null || _a === void 0 ? void 0 : _a.call(node);
1730
+ node.activatedObserveDispose = undefined;
1731
+ (_b = node.linkedToNodeDispose) === null || _b === void 0 ? void 0 : _b.call(node);
1732
+ node.linkedToNodeDispose = undefined;
1733
+ node.linkedToNode = undefined;
1734
+ node.lazyFn = lazyFn;
1735
+ node.lazy = true;
1736
+ }
1721
1737
  function activateNodeFunction(node, lazyFn) {
1722
1738
  // let prevTarget$: Observable<any>;
1723
1739
  // let curTarget$: Observable<any>;
1724
1740
  let update;
1725
1741
  let wasPromise;
1726
1742
  let ignoreThisUpdate;
1727
- const activateFn = (isFunction(node) ? node : lazyFn);
1743
+ const activateFn = lazyFn;
1728
1744
  const doRetry = () => { var _a; return (_a = node.state) === null || _a === void 0 ? void 0 : _a.refreshNum.set((v) => v + 1); };
1729
1745
  let activatedValue;
1730
1746
  let disposes = [];
@@ -1733,8 +1749,8 @@ function activateNodeFunction(node, lazyFn) {
1733
1749
  node.dirtyFn = refreshFn;
1734
1750
  globalState.dirtyNodes.add(node);
1735
1751
  }
1736
- observe(() => {
1737
- var _a, _b, _c, _d;
1752
+ node.activatedObserveDispose = observe(() => {
1753
+ var _a, _b, _c, _d, _e, _f;
1738
1754
  // const params = createNodeActivationParams(node);
1739
1755
  // Run the function at this node
1740
1756
  let value = activateFn();
@@ -1766,22 +1782,28 @@ function activateNodeFunction(node, lazyFn) {
1766
1782
  else if (node.activationState) {
1767
1783
  if (!node.activationState.persistedRetry && !node.activationState.waitFor) {
1768
1784
  const activated = node.activationState;
1769
- // TODO Should this have lastSync and value somehow?
1770
- value =
1771
- (_c = (_b = activated.get) === null || _b === void 0 ? void 0 : _b.call(activated, {
1772
- updateLastSync: noop,
1773
- setMode: noop,
1774
- lastSync: undefined,
1775
- value: undefined,
1776
- refresh: doRetry,
1777
- })) !== null && _c !== void 0 ? _c : activated.initial;
1785
+ if ((_c = (_b = node.state) === null || _b === void 0 ? void 0 : _b.peek()) === null || _c === void 0 ? void 0 : _c.sync) {
1786
+ node.state.sync();
1787
+ ignoreThisUpdate = true;
1788
+ }
1789
+ else {
1790
+ value =
1791
+ (_e = (_d = activated.get) === null || _d === void 0 ? void 0 : _d.call(activated, {
1792
+ updateLastSync: noop,
1793
+ setMode: noop,
1794
+ lastSync: undefined,
1795
+ value: undefined,
1796
+ refresh: doRetry,
1797
+ })) !== null && _e !== void 0 ? _e : activated.initial;
1798
+ }
1778
1799
  }
1779
1800
  else {
1780
1801
  ignoreThisUpdate = true;
1781
1802
  }
1782
1803
  }
1804
+ // value is undefined if it's in a persisted retry
1783
1805
  wasPromise = wasPromise || isPromise(value);
1784
- get(getNode((_d = node.state) === null || _d === void 0 ? void 0 : _d.refreshNum));
1806
+ get(getNode((_f = node.state) === null || _f === void 0 ? void 0 : _f.refreshNum));
1785
1807
  return value;
1786
1808
  }, (e) => {
1787
1809
  if (!ignoreThisUpdate) {
@@ -1897,9 +1919,10 @@ const activateNodeBase = (globalState.activateNode = function activateNodeBase(n
1897
1919
  };
1898
1920
  return new Promise((resolve, reject) => {
1899
1921
  isSetting = true;
1922
+ let isProm = false;
1900
1923
  batch(() => {
1901
1924
  try {
1902
- return onSet({
1925
+ const val = onSet({
1903
1926
  value,
1904
1927
  changes,
1905
1928
  getPrevious,
@@ -1910,13 +1933,19 @@ const activateNodeBase = (globalState.activateNode = function activateNodeBase(n
1910
1933
  cancelRetry,
1911
1934
  fromSubscribe: isSettingFromSubscribe,
1912
1935
  });
1936
+ isProm = isPromise(val);
1937
+ if (isProm) {
1938
+ val.then(resolve).catch(reject);
1939
+ }
1913
1940
  }
1914
1941
  catch (e) {
1915
1942
  reject(e);
1916
1943
  }
1917
1944
  }, () => {
1918
- isSetting = false;
1919
- resolve();
1945
+ if (!isProm) {
1946
+ isSetting = false;
1947
+ resolve();
1948
+ }
1920
1949
  });
1921
1950
  });
1922
1951
  });
@@ -1982,6 +2011,7 @@ const activateNodeBase = (globalState.activateNode = function activateNodeBase(n
1982
2011
  return { update, value };
1983
2012
  });
1984
2013
  function setToObservable(node, value) {
2014
+ var _a;
1985
2015
  // If the computed is a proxy to another observable
1986
2016
  // link it to the target observable
1987
2017
  const linkedNode = getNode(value);
@@ -1993,8 +2023,9 @@ function setToObservable(node, value) {
1993
2023
  }
1994
2024
  linkedNode.linkedFromNodes.add(node);
1995
2025
  peek(linkedNode);
1996
- onChange(linkedNode, ({ value: newValue }) => {
1997
- value = newValue;
2026
+ (_a = node.linkedToNodeDispose) === null || _a === void 0 ? void 0 : _a.call(node);
2027
+ node.linkedToNodeDispose = onChange(linkedNode, () => {
2028
+ value = peek(linkedNode);
1998
2029
  set(node, value);
1999
2030
  }, { initial: true });
2000
2031
  // If the target observable is different then notify for the change