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

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,7 +4,7 @@ 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';
7
+ export { computeSelector, constructObjectWithPath, deconstructObjectWithPath, getObservableIndex, isObservableValueReady, mergeIntoObservable, opaqueObject, setAtPath, setInObservableAtPath, setSilently, } from './src/helpers';
8
8
  export { hasOwnProperty, isArray, isBoolean, isEmpty, isFunction, isObject, isPrimitive, isPromise, isString, isSymbol, } from './src/is';
9
9
  export { observable, observablePrimitive, syncState } from './src/observable';
10
10
  export * from './src/observableInterfaces';
package/index.js CHANGED
@@ -73,24 +73,11 @@ const globalState = {
73
73
  function isObservable(obs) {
74
74
  return !!obs && !!obs[symbolGetNode];
75
75
  }
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
76
  function getNode(obs) {
90
77
  return obs && obs[symbolGetNode];
91
78
  }
92
79
  function setNodeValue(node, newValue) {
93
- var _a;
80
+ var _a, _b, _c;
94
81
  const parentNode = (_a = node.parent) !== null && _a !== void 0 ? _a : node;
95
82
  const key = node.parent ? node.key : '_';
96
83
  const isDelete = newValue === symbolDelete;
@@ -105,28 +92,30 @@ function setNodeValue(node, newValue) {
105
92
  // Compute newValue if newValue is a function or an observable
106
93
  newValue = !parentNode.isAssigning && isFunc ? newValue(prevValue) : newValue;
107
94
  // If setting an observable, set a link to the observable instead
108
- if (isObservable(newValue) && !isComputed(newValue)) {
95
+ if (isObservable(newValue)) {
109
96
  const val = newValue;
110
97
  node.lazy = true;
111
98
  node.lazyFn = () => val;
112
99
  newValue = undefined;
113
100
  }
114
- try {
115
- parentNode.isSetting = (parentNode.isSetting || 0) + 1;
116
- // Save the new value
117
- if (isDelete) {
118
- delete parentValue[key];
101
+ if (!globalState.isMerging ||
102
+ prevValue === undefined ||
103
+ isFunction(prevValue) ||
104
+ !((_c = (_b = node.parent) === null || _b === void 0 ? void 0 : _b.functions) === null || _c === void 0 ? void 0 : _c.get(key))) {
105
+ try {
106
+ parentNode.isSetting = (parentNode.isSetting || 0) + 1;
107
+ // Save the new value
108
+ if (isDelete) {
109
+ delete parentValue[key];
110
+ }
111
+ else {
112
+ parentValue[key] = newValue;
113
+ }
119
114
  }
120
- else {
121
- parentValue[key] = newValue;
115
+ finally {
116
+ parentNode.isSetting--;
122
117
  }
123
118
  }
124
- finally {
125
- parentNode.isSetting--;
126
- }
127
- if (parentNode.root.locked && parentNode.root.set) {
128
- parentNode.root.set(parentNode.root._);
129
- }
130
119
  return { prevValue, newValue, parentValue };
131
120
  }
132
121
  const arrNodeKeys = [];
@@ -210,18 +199,11 @@ function findIDKey(obj, node) {
210
199
  }
211
200
  return idKey;
212
201
  }
213
- function extractFunction(node, key, fnOrComputed, computedChildNode) {
202
+ function extractFunction(node, key, fnOrComputed) {
214
203
  if (!node.functions) {
215
204
  node.functions = new Map();
216
205
  }
217
206
  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
207
  }
226
208
 
227
209
  function activated(params) {
@@ -524,13 +506,6 @@ function opaqueObject(value) {
524
506
  }
525
507
  return value;
526
508
  }
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
509
  function setAtPath(obj, path, pathTypes, value, fullObj, restore) {
535
510
  let o = obj;
536
511
  let oFull = fullObj;
@@ -686,7 +661,6 @@ function onChange(node, callback, options = {}) {
686
661
  node.listeners = listeners;
687
662
  }
688
663
  }
689
- checkActivate(node);
690
664
  const listener = {
691
665
  listener: callback,
692
666
  track: trackingType,
@@ -1025,6 +999,7 @@ if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
1025
999
  function collectionSetter(node, target, prop, ...args) {
1026
1000
  var _a;
1027
1001
  if (prop === 'push' && args.length === 1) {
1002
+ // Fast path for push to just append to the end
1028
1003
  setKey(node, target.length + '', args[0]);
1029
1004
  }
1030
1005
  else {
@@ -1266,7 +1241,7 @@ const proxyHandler = {
1266
1241
  if (p === symbolGetNode) {
1267
1242
  return node;
1268
1243
  }
1269
- const value = peek(node);
1244
+ let value = peek(node);
1270
1245
  // If this node is linked to another observable then forward to the target's handler.
1271
1246
  // The exception is onChange because it needs to listen to this node for changes.
1272
1247
  // This needs to be below peek because it activates there.
@@ -1303,14 +1278,6 @@ const proxyHandler = {
1303
1278
  }
1304
1279
  };
1305
1280
  }
1306
- if (node.isComputed) {
1307
- if (node.proxyFn && !fn) {
1308
- return node.proxyFn(p);
1309
- }
1310
- else {
1311
- checkActivate(node);
1312
- }
1313
- }
1314
1281
  const property = observableProperties.get(p);
1315
1282
  if (property) {
1316
1283
  return property.get(node);
@@ -1329,7 +1296,7 @@ const proxyHandler = {
1329
1296
  }
1330
1297
  }
1331
1298
  // /TODOV3 Remove this
1332
- const vProp = value === null || value === void 0 ? void 0 : value[p];
1299
+ let vProp = value === null || value === void 0 ? void 0 : value[p];
1333
1300
  if (isObject(value) && value[symbolOpaque]) {
1334
1301
  return vProp;
1335
1302
  }
@@ -1342,6 +1309,11 @@ const proxyHandler = {
1342
1309
  return getProxy(node, p, fnOrComputed);
1343
1310
  }
1344
1311
  }
1312
+ if (value === undefined && vProp === undefined && (ArrayModifiers.has(p) || ArrayLoopers.has(p))) {
1313
+ value = [];
1314
+ setNodeValue(node, value);
1315
+ vProp = value[p];
1316
+ }
1345
1317
  // Handle function calls
1346
1318
  if (isFunction(vProp)) {
1347
1319
  if (isArray(value)) {
@@ -1483,20 +1455,6 @@ function setKey(node, key, newValue, level) {
1483
1455
  }
1484
1456
  }
1485
1457
  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
1458
  if (node.parent && !getNodeValue(node)) {
1501
1459
  return set(node, { [key]: newValue });
1502
1460
  }
@@ -1546,7 +1504,13 @@ function deleteFn(node, key) {
1546
1504
  key = node.key;
1547
1505
  node = node.parent;
1548
1506
  }
1549
- setKey(node, key !== null && key !== void 0 ? key : '_', symbolDelete, /*level*/ -1);
1507
+ const value = getNodeValue(node);
1508
+ if (isArray(value)) {
1509
+ collectionSetter(node, value, 'splice', key, 1);
1510
+ }
1511
+ else {
1512
+ setKey(node, key !== null && key !== void 0 ? key : '_', symbolDelete, /*level*/ -1);
1513
+ }
1550
1514
  }
1551
1515
  function handlerMapSet(node, p, value) {
1552
1516
  const vProp = value === null || value === void 0 ? void 0 : value[p];
@@ -1672,7 +1636,7 @@ function extractFunctionOrComputed(node, obj, k, v) {
1672
1636
  extractPromise(childNode, v);
1673
1637
  setNodeValue(childNode, undefined);
1674
1638
  }
1675
- else if (isObservable(v) && !isComputed(v)) {
1639
+ else if (isObservable(v)) {
1676
1640
  const value = getNodeValue(node);
1677
1641
  value[k] = () => v;
1678
1642
  extractFunction(node, k, value[k]);
@@ -1689,16 +1653,9 @@ function extractFunctionOrComputed(node, obj, k, v) {
1689
1653
  }
1690
1654
  }
1691
1655
  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)) {
1656
+ if (isObservable(v)) {
1697
1657
  extractFunction(node, k, v);
1698
1658
  }
1699
- else {
1700
- return true;
1701
- }
1702
1659
  }
1703
1660
  }
1704
1661
  function get(node, options) {
@@ -1720,12 +1677,6 @@ function peek(node) {
1720
1677
  const lazyFn = node.lazyFn;
1721
1678
  delete node.lazy;
1722
1679
  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
1680
  value = activateNodeFunction(node, lazyFn);
1730
1681
  }
1731
1682
  }
@@ -1736,9 +1687,6 @@ function peek(node) {
1736
1687
  }
1737
1688
  }
1738
1689
  }
1739
- // TODOV3 Remove legacy computed
1740
- // Check if computed needs to activate
1741
- checkActivate(node);
1742
1690
  return value;
1743
1691
  }
1744
1692
  function reactivateNode(node, lazyFn) {
@@ -2125,87 +2073,15 @@ function syncState(obs) {
2125
2073
  }
2126
2074
  globalState.isLoadingRemote$ = observable(false);
2127
2075
 
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;
2076
+ function computed(compute, set) {
2077
+ // @ts-expect-error asdf
2078
+ return observable(set
2079
+ ? activated({
2080
+ // @ts-expect-error asdf
2081
+ get: compute,
2082
+ onSet: ({ value }) => set(value),
2083
+ })
2084
+ : compute);
2209
2085
  }
2210
2086
 
2211
2087
  function configureLegendState({ observableFunctions, observableProperties: observableProperties$1, }) {
@@ -2259,32 +2135,14 @@ function event() {
2259
2135
  }
2260
2136
 
2261
2137
  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;
2138
+ return observable(activated({
2139
+ lookup: (key) => set
2140
+ ? activated({
2141
+ get: () => get(key),
2142
+ onSet: ({ value }) => set(key, value),
2143
+ })
2144
+ : get(key),
2145
+ }));
2288
2146
  }
2289
2147
 
2290
2148
  const internal = {
@@ -2310,7 +2168,6 @@ exports.activated = activated;
2310
2168
  exports.batch = batch;
2311
2169
  exports.beginBatch = beginBatch;
2312
2170
  exports.beginTracking = beginTracking;
2313
- exports.checkActivate = checkActivate;
2314
2171
  exports.computeSelector = computeSelector;
2315
2172
  exports.computed = computed;
2316
2173
  exports.configureLegendState = configureLegendState;
@@ -2338,7 +2195,6 @@ exports.isPrimitive = isPrimitive;
2338
2195
  exports.isPromise = isPromise;
2339
2196
  exports.isString = isString;
2340
2197
  exports.isSymbol = isSymbol;
2341
- exports.lockObservable = lockObservable;
2342
2198
  exports.mergeIntoObservable = mergeIntoObservable;
2343
2199
  exports.observable = observable;
2344
2200
  exports.observablePrimitive = observablePrimitive;