@legendapp/state 2.2.0-next.37 → 2.2.0-next.39

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.d.ts CHANGED
@@ -4,8 +4,8 @@ export { computed } from './src/computed';
4
4
  export { configureLegendState } from './src/config';
5
5
  export { event } from './src/event';
6
6
  export { isObservable } from './src/globals';
7
- export { computeSelector, constructObjectWithPath, deconstructObjectWithPath, getObservableIndex, isObservableValueReady, lockObservable, mergeIntoObservable, opaqueObject, setAtPath, setInObservableAtPath, setSilently, } from './src/helpers';
8
- export { hasOwnProperty, isArray, isBoolean, isEmpty, isFunction, isObject, isPrimitive, isPromise, isString, isSymbol, } from './src/is';
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';
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
@@ -43,6 +43,9 @@ function isEmpty(obj) {
43
43
  }
44
44
  return true;
45
45
  }
46
+ function isNullOrUndefined(value) {
47
+ return value === undefined || value === null;
48
+ }
46
49
  const setPrimitives = new Set(['boolean', 'string', 'number']);
47
50
  /** @internal */
48
51
  function isActualPrimitive(arg) {
@@ -73,24 +76,11 @@ const globalState = {
73
76
  function isObservable(obs) {
74
77
  return !!obs && !!obs[symbolGetNode];
75
78
  }
76
- function isComputed(obs) {
77
- var _a;
78
- return obs && ((_a = obs[symbolGetNode]) === null || _a === void 0 ? void 0 : _a.isComputed);
79
- }
80
- function checkActivate(node) {
81
- var _a;
82
- const root = node.root;
83
- (_a = root.activate) === null || _a === void 0 ? void 0 : _a.call(root);
84
- if (root.toActivate) {
85
- root.toActivate.forEach(checkActivate);
86
- delete root.toActivate;
87
- }
88
- }
89
79
  function getNode(obs) {
90
80
  return obs && obs[symbolGetNode];
91
81
  }
92
82
  function setNodeValue(node, newValue) {
93
- var _a;
83
+ var _a, _b, _c;
94
84
  const parentNode = (_a = node.parent) !== null && _a !== void 0 ? _a : node;
95
85
  const key = node.parent ? node.key : '_';
96
86
  const isDelete = newValue === symbolDelete;
@@ -105,28 +95,30 @@ function setNodeValue(node, newValue) {
105
95
  // Compute newValue if newValue is a function or an observable
106
96
  newValue = !parentNode.isAssigning && isFunc ? newValue(prevValue) : newValue;
107
97
  // If setting an observable, set a link to the observable instead
108
- if (isObservable(newValue) && !isComputed(newValue)) {
98
+ if (isObservable(newValue)) {
109
99
  const val = newValue;
110
100
  node.lazy = true;
111
101
  node.lazyFn = () => val;
112
102
  newValue = undefined;
113
103
  }
114
- try {
115
- parentNode.isSetting = (parentNode.isSetting || 0) + 1;
116
- // Save the new value
117
- if (isDelete) {
118
- delete parentValue[key];
104
+ if (!globalState.isMerging ||
105
+ prevValue === undefined ||
106
+ isFunction(prevValue) ||
107
+ !((_c = (_b = node.parent) === null || _b === void 0 ? void 0 : _b.functions) === null || _c === void 0 ? void 0 : _c.get(key))) {
108
+ try {
109
+ parentNode.isSetting = (parentNode.isSetting || 0) + 1;
110
+ // Save the new value
111
+ if (isDelete) {
112
+ delete parentValue[key];
113
+ }
114
+ else {
115
+ parentValue[key] = newValue;
116
+ }
119
117
  }
120
- else {
121
- parentValue[key] = newValue;
118
+ finally {
119
+ parentNode.isSetting--;
122
120
  }
123
121
  }
124
- finally {
125
- parentNode.isSetting--;
126
- }
127
- if (parentNode.root.locked && parentNode.root.set) {
128
- parentNode.root.set(parentNode.root._);
129
- }
130
122
  return { prevValue, newValue, parentValue };
131
123
  }
132
124
  const arrNodeKeys = [];
@@ -210,18 +202,11 @@ function findIDKey(obj, node) {
210
202
  }
211
203
  return idKey;
212
204
  }
213
- function extractFunction(node, key, fnOrComputed, computedChildNode) {
205
+ function extractFunction(node, key, fnOrComputed) {
214
206
  if (!node.functions) {
215
207
  node.functions = new Map();
216
208
  }
217
209
  node.functions.set(key, fnOrComputed);
218
- if (computedChildNode) {
219
- computedChildNode.parentOther = getChildNode(node, key);
220
- if (!node.root.toActivate) {
221
- node.root.toActivate = [];
222
- }
223
- node.root.toActivate.push(computedChildNode);
224
- }
225
210
  }
226
211
 
227
212
  function activated(params) {
@@ -524,13 +509,6 @@ function opaqueObject(value) {
524
509
  }
525
510
  return value;
526
511
  }
527
- function lockObservable(obs, value) {
528
- var _a;
529
- const root = (_a = getNode(obs)) === null || _a === void 0 ? void 0 : _a.root;
530
- if (root) {
531
- root.locked = value;
532
- }
533
- }
534
512
  function setAtPath(obj, path, pathTypes, value, fullObj, restore) {
535
513
  let o = obj;
536
514
  let oFull = fullObj;
@@ -686,7 +664,6 @@ function onChange(node, callback, options = {}) {
686
664
  node.listeners = listeners;
687
665
  }
688
666
  }
689
- checkActivate(node);
690
667
  const listener = {
691
668
  listener: callback,
692
669
  track: trackingType,
@@ -1025,6 +1002,7 @@ if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
1025
1002
  function collectionSetter(node, target, prop, ...args) {
1026
1003
  var _a;
1027
1004
  if (prop === 'push' && args.length === 1) {
1005
+ // Fast path for push to just append to the end
1028
1006
  setKey(node, target.length + '', args[0]);
1029
1007
  }
1030
1008
  else {
@@ -1266,7 +1244,7 @@ const proxyHandler = {
1266
1244
  if (p === symbolGetNode) {
1267
1245
  return node;
1268
1246
  }
1269
- const value = peek(node);
1247
+ let value = peek(node);
1270
1248
  // If this node is linked to another observable then forward to the target's handler.
1271
1249
  // The exception is onChange because it needs to listen to this node for changes.
1272
1250
  // This needs to be below peek because it activates there.
@@ -1303,14 +1281,6 @@ const proxyHandler = {
1303
1281
  }
1304
1282
  };
1305
1283
  }
1306
- if (node.isComputed) {
1307
- if (node.proxyFn && !fn) {
1308
- return node.proxyFn(p);
1309
- }
1310
- else {
1311
- checkActivate(node);
1312
- }
1313
- }
1314
1284
  const property = observableProperties.get(p);
1315
1285
  if (property) {
1316
1286
  return property.get(node);
@@ -1329,7 +1299,7 @@ const proxyHandler = {
1329
1299
  }
1330
1300
  }
1331
1301
  // /TODOV3 Remove this
1332
- const vProp = value === null || value === void 0 ? void 0 : value[p];
1302
+ let vProp = value === null || value === void 0 ? void 0 : value[p];
1333
1303
  if (isObject(value) && value[symbolOpaque]) {
1334
1304
  return vProp;
1335
1305
  }
@@ -1342,6 +1312,11 @@ const proxyHandler = {
1342
1312
  return getProxy(node, p, fnOrComputed);
1343
1313
  }
1344
1314
  }
1315
+ if (isNullOrUndefined(value) && vProp === undefined && (ArrayModifiers.has(p) || ArrayLoopers.has(p))) {
1316
+ value = [];
1317
+ setNodeValue(node, value);
1318
+ vProp = value[p];
1319
+ }
1345
1320
  // Handle function calls
1346
1321
  if (isFunction(vProp)) {
1347
1322
  if (isArray(value)) {
@@ -1483,20 +1458,6 @@ function setKey(node, key, newValue, level) {
1483
1458
  }
1484
1459
  }
1485
1460
  const isRoot = !node.parent && key === '_';
1486
- // TODOv3 root locking will be removed with old computeds
1487
- if (node.root.locked && !node.root.set) {
1488
- // This happens when modifying a locked observable such as a computed.
1489
- // If merging this could be happening deep in a hierarchy so we don't want to throw errors so we'll just do nothing.
1490
- // This could happen during persistence local load for example.
1491
- if (globalState.isMerging) {
1492
- return isRoot ? getProxy(node) : getProxy(node, key);
1493
- }
1494
- else {
1495
- throw new Error(process.env.NODE_ENV === 'development'
1496
- ? '[legend-state] Cannot modify an observable while it is locked. Please make sure that you unlock the observable before making changes.'
1497
- : '[legend-state] Modified locked observable');
1498
- }
1499
- }
1500
1461
  if (node.parent && !getNodeValue(node)) {
1501
1462
  return set(node, { [key]: newValue });
1502
1463
  }
@@ -1546,7 +1507,13 @@ function deleteFn(node, key) {
1546
1507
  key = node.key;
1547
1508
  node = node.parent;
1548
1509
  }
1549
- setKey(node, key !== null && key !== void 0 ? key : '_', symbolDelete, /*level*/ -1);
1510
+ const value = getNodeValue(node);
1511
+ if (isArray(value)) {
1512
+ collectionSetter(node, value, 'splice', key, 1);
1513
+ }
1514
+ else {
1515
+ setKey(node, key !== null && key !== void 0 ? key : '_', symbolDelete, /*level*/ -1);
1516
+ }
1550
1517
  }
1551
1518
  function handlerMapSet(node, p, value) {
1552
1519
  const vProp = value === null || value === void 0 ? void 0 : value[p];
@@ -1672,7 +1639,7 @@ function extractFunctionOrComputed(node, obj, k, v) {
1672
1639
  extractPromise(childNode, v);
1673
1640
  setNodeValue(childNode, undefined);
1674
1641
  }
1675
- else if (isObservable(v) && !isComputed(v)) {
1642
+ else if (isObservable(v)) {
1676
1643
  const value = getNodeValue(node);
1677
1644
  value[k] = () => v;
1678
1645
  extractFunction(node, k, value[k]);
@@ -1689,16 +1656,9 @@ function extractFunctionOrComputed(node, obj, k, v) {
1689
1656
  }
1690
1657
  }
1691
1658
  else if (typeof v == 'object' && v !== null && v !== undefined) {
1692
- const childNode = getNode(v);
1693
- if (childNode === null || childNode === void 0 ? void 0 : childNode.isComputed) {
1694
- extractFunction(node, k, v, childNode);
1695
- }
1696
- else if (isObservable(v)) {
1659
+ if (isObservable(v)) {
1697
1660
  extractFunction(node, k, v);
1698
1661
  }
1699
- else {
1700
- return true;
1701
- }
1702
1662
  }
1703
1663
  }
1704
1664
  function get(node, options) {
@@ -1720,12 +1680,6 @@ function peek(node) {
1720
1680
  const lazyFn = node.lazyFn;
1721
1681
  delete node.lazy;
1722
1682
  if (isFunction(node) || isFunction(lazyFn)) {
1723
- if (node.parent) {
1724
- const parentValue = getNodeValue(node.parent);
1725
- if (parentValue) {
1726
- delete parentValue[node.key];
1727
- }
1728
- }
1729
1683
  value = activateNodeFunction(node, lazyFn);
1730
1684
  }
1731
1685
  }
@@ -1736,9 +1690,6 @@ function peek(node) {
1736
1690
  }
1737
1691
  }
1738
1692
  }
1739
- // TODOV3 Remove legacy computed
1740
- // Check if computed needs to activate
1741
- checkActivate(node);
1742
1693
  return value;
1743
1694
  }
1744
1695
  function reactivateNode(node, lazyFn) {
@@ -2125,87 +2076,15 @@ function syncState(obs) {
2125
2076
  }
2126
2077
  globalState.isLoadingRemote$ = observable(false);
2127
2078
 
2128
- function computed(compute, set$1) {
2129
- // Create an observable for this computed variable
2130
- const obs = observable();
2131
- lockObservable(obs, true);
2132
- const node = getNode(obs);
2133
- node.isComputed = true;
2134
- let isSetAfterActivated = false;
2135
- const setInner = function (val) {
2136
- const prevNode = node.linkedToNode;
2137
- // If it was previously linked to a node remove self
2138
- // from its linkedFromNodes
2139
- if (prevNode) {
2140
- prevNode.linkedFromNodes.delete(node);
2141
- node.linkedToNode = undefined;
2142
- }
2143
- const { parentOther } = node;
2144
- if (isObservable(val)) {
2145
- // If the computed is a proxy to another observable
2146
- // link it to the target observable
2147
- const linkedNode = getNode(val);
2148
- node.linkedToNode = linkedNode;
2149
- if (!linkedNode.linkedFromNodes) {
2150
- linkedNode.linkedFromNodes = new Set();
2151
- }
2152
- linkedNode.linkedFromNodes.add(node);
2153
- if (node.parentOther) {
2154
- onChange(linkedNode, ({ value }) => {
2155
- setNodeValue(node.parentOther, value);
2156
- }, { initial: true });
2157
- }
2158
- // If the target observable is different then notify for the change
2159
- if (prevNode) {
2160
- const value = getNodeValue(linkedNode);
2161
- const prevValue = getNodeValue(prevNode);
2162
- notify(node, value, prevValue, 0);
2163
- }
2164
- }
2165
- else if (val !== obs.peek()) {
2166
- // Unlock computed node before setting the value
2167
- lockObservable(obs, false);
2168
- const setter = isSetAfterActivated ? set : setNodeValue;
2169
- // Update the computed value
2170
- setter(node, val);
2171
- // If the computed is a child of an observable set the value on it
2172
- if (parentOther) {
2173
- let didUnlock = false;
2174
- if (parentOther.root.locked) {
2175
- parentOther.root.locked = false;
2176
- didUnlock = true;
2177
- }
2178
- setter(parentOther, val);
2179
- if (didUnlock) {
2180
- parentOther.root.locked = true;
2181
- }
2182
- }
2183
- // Re-lock the computed node
2184
- lockObservable(obs, true);
2185
- }
2186
- else if (parentOther) {
2187
- setNodeValue(parentOther, val);
2188
- }
2189
- isSetAfterActivated = true;
2190
- };
2191
- // Lazily activate the observable when get is called
2192
- node.root.activate = () => {
2193
- node.root.activate = undefined;
2194
- observe(compute, ({ value }) => {
2195
- if (isPromise(value)) {
2196
- value.then((v) => setInner(v));
2197
- }
2198
- else {
2199
- setInner(value);
2200
- }
2201
- }, { immediate: true, fromComputed: true });
2202
- };
2203
- if (set$1) {
2204
- node.root.set = (value) => {
2205
- batch(() => set$1(value));
2206
- };
2207
- }
2208
- return obs;
2079
+ function computed(compute, set) {
2080
+ // @ts-expect-error asdf
2081
+ return observable(set
2082
+ ? activated({
2083
+ // @ts-expect-error asdf
2084
+ get: compute,
2085
+ onSet: ({ value }) => set(value),
2086
+ })
2087
+ : compute);
2209
2088
  }
2210
2089
 
2211
2090
  function configureLegendState({ observableFunctions, observableProperties: observableProperties$1, }) {
@@ -2259,32 +2138,14 @@ function event() {
2259
2138
  }
2260
2139
 
2261
2140
  function proxy(get, set) {
2262
- // Create an observable for this computed variable
2263
- const obs = observable({});
2264
- lockObservable(obs, true);
2265
- const mapTargets = new Map();
2266
- const node = getNode(obs);
2267
- node.isComputed = true;
2268
- node.proxyFn = (key) => {
2269
- let target = mapTargets.get(key);
2270
- if (!target) {
2271
- // Note: Coercing typescript to allow undefined for set in computed because we don't want the public interface to allow undefined
2272
- target = computed(() => get(key), (set ? (value) => set(key, value) : undefined));
2273
- mapTargets.set(key, target);
2274
- extractFunction(node, key, target, getNode(target));
2275
- if (node.parentOther) {
2276
- onChange(getNode(target), ({ value, getPrevious }) => {
2277
- const previous = getPrevious();
2278
- // Set the raw value on the proxy's parent
2279
- setNodeValue(node.parentOther, node.root._);
2280
- // Notify the proxy
2281
- notify(getChildNode(node, key), value, previous, 0);
2282
- });
2283
- }
2284
- }
2285
- return target;
2286
- };
2287
- return obs;
2141
+ return observable(activated({
2142
+ lookup: (key) => set
2143
+ ? activated({
2144
+ get: () => get(key),
2145
+ onSet: ({ value }) => set(key, value),
2146
+ })
2147
+ : get(key),
2148
+ }));
2288
2149
  }
2289
2150
 
2290
2151
  const internal = {
@@ -2310,7 +2171,6 @@ exports.activated = activated;
2310
2171
  exports.batch = batch;
2311
2172
  exports.beginBatch = beginBatch;
2312
2173
  exports.beginTracking = beginTracking;
2313
- exports.checkActivate = checkActivate;
2314
2174
  exports.computeSelector = computeSelector;
2315
2175
  exports.computed = computed;
2316
2176
  exports.configureLegendState = configureLegendState;
@@ -2329,8 +2189,10 @@ exports.hasOwnProperty = hasOwnProperty;
2329
2189
  exports.internal = internal;
2330
2190
  exports.isArray = isArray;
2331
2191
  exports.isBoolean = isBoolean;
2192
+ exports.isDate = isDate;
2332
2193
  exports.isEmpty = isEmpty;
2333
2194
  exports.isFunction = isFunction;
2195
+ exports.isNullOrUndefined = isNullOrUndefined;
2334
2196
  exports.isObject = isObject;
2335
2197
  exports.isObservable = isObservable;
2336
2198
  exports.isObservableValueReady = isObservableValueReady;
@@ -2338,7 +2200,6 @@ exports.isPrimitive = isPrimitive;
2338
2200
  exports.isPromise = isPromise;
2339
2201
  exports.isString = isString;
2340
2202
  exports.isSymbol = isSymbol;
2341
- exports.lockObservable = lockObservable;
2342
2203
  exports.mergeIntoObservable = mergeIntoObservable;
2343
2204
  exports.observable = observable;
2344
2205
  exports.observablePrimitive = observablePrimitive;