@legendapp/state 2.2.0-next.74 → 2.2.0-next.76

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.
Files changed (60) hide show
  1. package/helpers/time.d.ts +2 -2
  2. package/index.d.ts +1 -1
  3. package/index.js +82 -31
  4. package/index.js.map +1 -1
  5. package/index.mjs +81 -32
  6. package/index.mjs.map +1 -1
  7. package/package.json +16 -1
  8. package/persist.js +122 -129
  9. package/persist.js.map +1 -1
  10. package/persist.mjs +122 -129
  11. package/persist.mjs.map +1 -1
  12. package/react.js +5 -5
  13. package/react.js.map +1 -1
  14. package/react.mjs +6 -6
  15. package/react.mjs.map +1 -1
  16. package/src/ObservableObject.ts +34 -15
  17. package/src/batching.ts +9 -3
  18. package/src/computed.ts +4 -2
  19. package/src/globals.ts +17 -7
  20. package/src/helpers.ts +3 -3
  21. package/src/history/undoRedo.ts +111 -0
  22. package/src/is.ts +7 -0
  23. package/src/observableInterfaces.ts +6 -5
  24. package/src/observableTypes.ts +5 -0
  25. package/src/observe.ts +1 -1
  26. package/src/react/For.tsx +6 -6
  27. package/src/sync/activateSyncedNode.ts +9 -25
  28. package/src/sync/syncHelpers.ts +53 -12
  29. package/src/sync/syncObservable.ts +117 -101
  30. package/src/sync-plugins/crud.ts +384 -0
  31. package/src/sync-plugins/fetch.ts +57 -27
  32. package/src/sync-plugins/keel.ts +447 -0
  33. package/src/sync-plugins/supabase.ts +225 -0
  34. package/src/syncTypes.ts +12 -6
  35. package/src/when.ts +6 -1
  36. package/sync-plugins/crud.d.ts +40 -0
  37. package/sync-plugins/crud.js +275 -0
  38. package/sync-plugins/crud.js.map +1 -0
  39. package/sync-plugins/crud.mjs +271 -0
  40. package/sync-plugins/crud.mjs.map +1 -0
  41. package/sync-plugins/fetch.d.ts +8 -7
  42. package/sync-plugins/fetch.js +34 -12
  43. package/sync-plugins/fetch.js.map +1 -1
  44. package/sync-plugins/fetch.mjs +35 -13
  45. package/sync-plugins/fetch.mjs.map +1 -1
  46. package/sync-plugins/keel.d.ts +91 -0
  47. package/sync-plugins/keel.js +278 -0
  48. package/sync-plugins/keel.js.map +1 -0
  49. package/sync-plugins/keel.mjs +274 -0
  50. package/sync-plugins/keel.mjs.map +1 -0
  51. package/sync-plugins/supabase.d.ts +32 -0
  52. package/sync-plugins/supabase.js +134 -0
  53. package/sync-plugins/supabase.js.map +1 -0
  54. package/sync-plugins/supabase.mjs +131 -0
  55. package/sync-plugins/supabase.mjs.map +1 -0
  56. package/sync.d.ts +1 -0
  57. package/sync.js +157 -127
  58. package/sync.js.map +1 -1
  59. package/sync.mjs +156 -129
  60. package/sync.mjs.map +1 -1
package/helpers/time.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- declare const currentTime: import("../observableTypes").ObservablePrimitive<Date>;
2
- declare const currentDay: import("../observableTypes").ObservablePrimitive<Date>;
1
+ declare const currentTime: import("@legendapp/state").ObservablePrimitive<Date>;
2
+ declare const currentDay: import("@legendapp/state").ObservablePrimitive<Date>;
3
3
  export { currentTime, currentDay };
package/index.d.ts CHANGED
@@ -5,7 +5,7 @@ export { configureLegendState } from './src/config';
5
5
  export { event } from './src/event';
6
6
  export { isObservable } from './src/globals';
7
7
  export { computeSelector, constructObjectWithPath, deconstructObjectWithPath, getObservableIndex, isObservableValueReady, mergeIntoObservable, opaqueObject, setAtPath, setInObservableAtPath, setSilently, } from './src/helpers';
8
- export { hasOwnProperty, isArray, isBoolean, isDate, isEmpty, isFunction, isNullOrUndefined, isObject, isPrimitive, isPromise, isString, isSymbol, } from './src/is';
8
+ export { hasOwnProperty, isArray, isBoolean, isDate, isEmpty, isFunction, isMap, isNullOrUndefined, isNumber, isObject, isPrimitive, isPromise, isString, isSymbol, } from './src/is';
9
9
  export { observable, observablePrimitive, syncState } from './src/observable';
10
10
  export * from './src/observableInterfaces';
11
11
  export * from './src/observableTypes';
package/index.js CHANGED
@@ -29,6 +29,13 @@ function isBoolean(obj) {
29
29
  function isPromise(obj) {
30
30
  return obj instanceof Promise;
31
31
  }
32
+ function isMap(obj) {
33
+ return obj instanceof Map;
34
+ }
35
+ function isNumber(obj) {
36
+ const n = obj;
37
+ return n - n < 1;
38
+ }
32
39
  function isEmpty(obj) {
33
40
  // Looping and returning false on the first property is faster than Object.keys(obj).length === 0
34
41
  // https://jsbench.me/qfkqv692c8
@@ -73,10 +80,10 @@ const globalState = {
73
80
  reviver: undefined,
74
81
  };
75
82
  function getPathType(value) {
76
- return isArray(value) ? 'array' : value instanceof Map ? 'map' : value instanceof Set ? 'set' : 'object';
83
+ return isArray(value) ? 'array' : isMap(value) ? 'map' : value instanceof Set ? 'set' : 'object';
77
84
  }
78
85
  function replacer(key, value) {
79
- if (value instanceof Map) {
86
+ if (isMap(value)) {
80
87
  return {
81
88
  __LSType: 'Map',
82
89
  value: Array.from(value.entries()), // or with spread: value: [...value]
@@ -152,12 +159,24 @@ function setNodeValue(node, newValue) {
152
159
  !((_c = (_b = node.parent) === null || _b === void 0 ? void 0 : _b.functions) === null || _c === void 0 ? void 0 : _c.get(key))) {
153
160
  try {
154
161
  parentNode.isSetting = (parentNode.isSetting || 0) + 1;
162
+ const useMapFn = isMap(parentValue);
155
163
  // Save the new value
156
164
  if (isDelete) {
157
- delete parentValue[key];
165
+ if (useMapFn) {
166
+ parentValue.delete(key);
167
+ }
168
+ else {
169
+ delete parentValue[key];
170
+ }
158
171
  }
159
172
  else {
160
- parentValue[key] = newValue;
173
+ const useMapFn = isMap(parentValue);
174
+ if (useMapFn) {
175
+ parentValue.set(key, newValue);
176
+ }
177
+ else {
178
+ parentValue[key] = newValue;
179
+ }
161
180
  }
162
181
  }
163
182
  finally {
@@ -177,7 +196,7 @@ function getNodeValue(node) {
177
196
  let child = node.root._;
178
197
  for (let i = count - 1; child && i >= 0; i--) {
179
198
  const key = arrNodeKeys[i];
180
- child = key !== 'size' && (child instanceof Map || child instanceof WeakMap) ? child.get(key) : child[key];
199
+ child = key !== 'size' && (isMap(child) || child instanceof WeakMap) ? child.get(key) : child[key];
181
200
  }
182
201
  return child;
183
202
  }
@@ -279,6 +298,8 @@ function isArraySubset(mainArr, subsetArr) {
279
298
  function createPreviousHandlerInner(value, changes) {
280
299
  try {
281
300
  // Clones the current state and inject the previous data at the changed path
301
+ // TODO: Is this behavior similar to setAtPath or mergeIntoObservable so one
302
+ // of those could be used here?
282
303
  let cloned = value ? clone(value) : {};
283
304
  for (let i = 0; i < changes.length; i++) {
284
305
  const { path, prevAtPath } = changes[i];
@@ -289,7 +310,7 @@ function createPreviousHandlerInner(value, changes) {
289
310
  o = o[path[i]];
290
311
  }
291
312
  const key = path[i];
292
- if (o instanceof Map) {
313
+ if (isMap(o)) {
293
314
  o.set(key, prevAtPath);
294
315
  }
295
316
  else {
@@ -432,9 +453,11 @@ function runBatch() {
432
453
  const dirtyNodes = Array.from(globalState.dirtyNodes);
433
454
  globalState.dirtyNodes.clear();
434
455
  dirtyNodes.forEach((node) => {
435
- var _a;
436
- (_a = node.dirtyFn) === null || _a === void 0 ? void 0 : _a.call(node);
437
- node.dirtyFn = undefined;
456
+ const dirtyFn = node.dirtyFn;
457
+ if (dirtyFn) {
458
+ node.dirtyFn = undefined;
459
+ dirtyFn();
460
+ }
438
461
  });
439
462
  // Save batch locally and reset _batchMap first because a new batch could begin while looping over callbacks.
440
463
  // This can happen with computeds for example.
@@ -501,6 +524,10 @@ function getNodeAtPath(obj, path) {
501
524
  return o;
502
525
  }
503
526
 
527
+ function linked(params) {
528
+ return (() => ({ [symbolLinked]: params }));
529
+ }
530
+
504
531
  function createObservable(value, makePrimitive, extractPromise, createObject, createPrimitive) {
505
532
  if (isObservable(value)) {
506
533
  return value;
@@ -539,7 +566,7 @@ function computeSelector(selector, e, retainObservable) {
539
566
  function getObservableIndex(obs) {
540
567
  const node = getNode(obs);
541
568
  const n = +node.key;
542
- return n - n < 1 ? +n : -1;
569
+ return isNumber(n) ? n : -1;
543
570
  }
544
571
  function opaqueObject(value) {
545
572
  if (value) {
@@ -999,7 +1026,13 @@ function _when(predicate, effect, checkReady) {
999
1026
  // Create a wrapping fn that calls the effect if predicate returns true
1000
1027
  function run(e) {
1001
1028
  const ret = computeSelector(predicate);
1002
- if (!isPromise(ret) && (checkReady ? isObservableValueReady(ret) : ret)) {
1029
+ if (isPromise(ret)) {
1030
+ value = ret;
1031
+ // We want value to be the Promise but return undefined
1032
+ // so it doesn't run the effect with the Promise as the value
1033
+ return undefined;
1034
+ }
1035
+ else if (!isPromise(ret) && (checkReady ? isObservableValueReady(ret) : ret)) {
1003
1036
  value = ret;
1004
1037
  // Set cancel so that observe does not track anymore
1005
1038
  e.cancel = true;
@@ -1136,9 +1169,9 @@ function updateNodes(parent, obj, prevValue) {
1136
1169
  const isArr = isArray(obj);
1137
1170
  let prevChildrenById;
1138
1171
  let moved;
1139
- const isMap = obj instanceof Map;
1140
- const isPrevMap = prevValue instanceof Map;
1141
- const keys = getKeys(obj, isArr, isMap);
1172
+ const isCurMap = isMap(obj);
1173
+ const isPrevMap = isMap(prevValue);
1174
+ const keys = getKeys(obj, isArr, isCurMap);
1142
1175
  const keysPrev = getKeys(prevValue, isArr, isPrevMap);
1143
1176
  const length = ((_a = (keys || obj)) === null || _a === void 0 ? void 0 : _a.length) || 0;
1144
1177
  const lengthPrev = ((_b = (keysPrev || prevValue)) === null || _b === void 0 ? void 0 : _b.length) || 0;
@@ -1211,7 +1244,7 @@ function updateNodes(parent, obj, prevValue) {
1211
1244
  let didMove = false;
1212
1245
  for (let i = 0; i < length; i++) {
1213
1246
  const key = isArr ? i + '' : keys[i];
1214
- let value = isMap ? obj.get(key) : obj[key];
1247
+ let value = isCurMap ? obj.get(key) : obj[key];
1215
1248
  const prev = isPrevMap ? prevValue === null || prevValue === void 0 ? void 0 : prevValue.get(key) : prevValue === null || prevValue === void 0 ? void 0 : prevValue[key];
1216
1249
  let isDiff = !equals(value, prev);
1217
1250
  if (isDiff) {
@@ -1225,7 +1258,7 @@ function updateNodes(parent, obj, prevValue) {
1225
1258
  const valueNode = getNode(value);
1226
1259
  if ((existingChild === null || existingChild === void 0 ? void 0 : existingChild.linkedToNode) === valueNode) {
1227
1260
  const targetValue = getNodeValue(valueNode);
1228
- isMap ? obj.set(key, targetValue) : (obj[key] = targetValue);
1261
+ isCurMap ? obj.set(key, targetValue) : (obj[key] = targetValue);
1229
1262
  continue;
1230
1263
  }
1231
1264
  const obs = value;
@@ -1243,7 +1276,7 @@ function updateNodes(parent, obj, prevValue) {
1243
1276
  const prevChild = id !== undefined ? prevChildrenById === null || prevChildrenById === void 0 ? void 0 : prevChildrenById.get(id) : undefined;
1244
1277
  if (!prevChild) {
1245
1278
  // This id was not in the array before so it does not need to notify children
1246
- isDiff = false;
1279
+ // It does need to notify itself so isDiff should remain.
1247
1280
  hasADiff = true;
1248
1281
  }
1249
1282
  else if (prevChild !== undefined && prevChild.key !== key) {
@@ -1363,7 +1396,7 @@ const proxyHandler = {
1363
1396
  if (targetNode && p !== 'onChange') {
1364
1397
  return proxyHandler.get(targetNode, p, receiver);
1365
1398
  }
1366
- if (value instanceof Map || value instanceof WeakMap || value instanceof Set || value instanceof WeakSet) {
1399
+ if (isMap(value) || value instanceof WeakMap || value instanceof Set || value instanceof WeakSet) {
1367
1400
  const ret = handlerMapSet(node, p, value);
1368
1401
  if (ret !== undefined) {
1369
1402
  return ret;
@@ -1583,13 +1616,22 @@ function assign(node, value) {
1583
1616
  if (isPrimitive(node.root._)) {
1584
1617
  node.root._ = {};
1585
1618
  }
1586
- // Set inAssign to allow setting on safe observables
1587
- node.isAssigning = (node.isAssigning || 0) + 1;
1588
- try {
1589
- Object.assign(proxy, value);
1619
+ if (isMap(value)) {
1620
+ const currentValue = getNodeValue(node);
1621
+ if (isMap(currentValue)) {
1622
+ value.forEach((value, key) => currentValue.set(key, value));
1623
+ }
1590
1624
  }
1591
- finally {
1592
- node.isAssigning--;
1625
+ else {
1626
+ // Set inAssign to allow setting on safe observables
1627
+ node.isAssigning = (node.isAssigning || 0) + 1;
1628
+ try {
1629
+ // TODO: If current value is a Map how to assign into the Map?
1630
+ Object.assign(proxy, value);
1631
+ }
1632
+ finally {
1633
+ node.isAssigning--;
1634
+ }
1593
1635
  }
1594
1636
  endBatch();
1595
1637
  return proxy;
@@ -1631,6 +1673,9 @@ function handlerMapSet(node, p, value) {
1631
1673
  }
1632
1674
  return ret;
1633
1675
  }
1676
+ else if (l === 1 && isMap(value)) {
1677
+ set(node, a);
1678
+ }
1634
1679
  }
1635
1680
  else if (p === 'delete') {
1636
1681
  if (l > 0) {
@@ -1763,9 +1808,10 @@ function peek(node) {
1763
1808
  }
1764
1809
  function peekInternal(node, activateRecursive) {
1765
1810
  if (node.dirtyFn) {
1766
- node.dirtyFn();
1767
- globalState.dirtyNodes.delete(node);
1811
+ const dirtyFn = node.dirtyFn;
1768
1812
  node.dirtyFn = undefined;
1813
+ globalState.dirtyNodes.delete(node);
1814
+ dirtyFn();
1769
1815
  }
1770
1816
  let value = getNodeValue(node);
1771
1817
  value = checkLazy(node, value, !!activateRecursive);
@@ -1819,6 +1865,7 @@ function activateNodeFunction(node, lazyFn) {
1819
1865
  let update;
1820
1866
  let wasPromise;
1821
1867
  let ignoreThisUpdate;
1868
+ let isFirst = true;
1822
1869
  const activateFn = lazyFn;
1823
1870
  let activatedValue;
1824
1871
  let disposes = [];
@@ -1829,6 +1876,12 @@ function activateNodeFunction(node, lazyFn) {
1829
1876
  }
1830
1877
  node.activatedObserveDispose = observe(() => {
1831
1878
  var _a, _b, _c, _d;
1879
+ // Set it to undefined so that the activation function gets undefined
1880
+ // if it peeks itself
1881
+ if (isFirst) {
1882
+ isFirst = false;
1883
+ setNodeValue(node, undefined);
1884
+ }
1832
1885
  // Run the function at this node
1833
1886
  let value = activateFn();
1834
1887
  // If target is an observable, make this node a link to it
@@ -2102,11 +2155,7 @@ function syncState(obs) {
2102
2155
  }
2103
2156
 
2104
2157
  function computed(get, set) {
2105
- return observable(set ? () => ({ [symbolLinked]: { get, set: ({ value }) => set(value) } }) : get);
2106
- }
2107
-
2108
- function linked(params) {
2109
- return (() => ({ [symbolLinked]: params }));
2158
+ return observable(set ? linked({ get: get, set: ({ value }) => set(value) }) : get);
2110
2159
  }
2111
2160
 
2112
2161
  function configureLegendState({ observableFunctions, observableProperties: observableProperties$1, jsonReplacer, jsonReviver, }) {
@@ -2282,7 +2331,9 @@ exports.isBoolean = isBoolean;
2282
2331
  exports.isDate = isDate;
2283
2332
  exports.isEmpty = isEmpty;
2284
2333
  exports.isFunction = isFunction;
2334
+ exports.isMap = isMap;
2285
2335
  exports.isNullOrUndefined = isNullOrUndefined;
2336
+ exports.isNumber = isNumber;
2286
2337
  exports.isObject = isObject;
2287
2338
  exports.isObservable = isObservable;
2288
2339
  exports.isObservableValueReady = isObservableValueReady;