@legendapp/state 3.0.0-beta.22 → 3.0.0-beta.23

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.mts CHANGED
@@ -113,6 +113,7 @@ declare function setNodeValue(node: NodeInfo, newValue: any): {
113
113
  declare function getNodeValue(node: NodeInfo): any;
114
114
  declare function ensureNodeValue(node: NodeInfo): any;
115
115
  declare function findIDKey(obj: unknown | undefined, node: NodeInfo): string | ((value: any) => string) | undefined;
116
+ declare function getKeys(obj: Record<any, any> | Array<any> | undefined, isArr: boolean, isMap: boolean, isSet: boolean): string[];
116
117
 
117
118
  type TrackingType = undefined | true | symbol;
118
119
  interface GetOptions {
@@ -397,6 +398,7 @@ declare const internal: {
397
398
  ensureNodeValue: typeof ensureNodeValue;
398
399
  findIDKey: typeof findIDKey;
399
400
  get: typeof get;
401
+ getKeys: typeof getKeys;
400
402
  getNode: typeof getNode;
401
403
  getNodeValue: typeof getNodeValue;
402
404
  getPathType: typeof getPathType;
package/index.d.ts CHANGED
@@ -113,6 +113,7 @@ declare function setNodeValue(node: NodeInfo, newValue: any): {
113
113
  declare function getNodeValue(node: NodeInfo): any;
114
114
  declare function ensureNodeValue(node: NodeInfo): any;
115
115
  declare function findIDKey(obj: unknown | undefined, node: NodeInfo): string | ((value: any) => string) | undefined;
116
+ declare function getKeys(obj: Record<any, any> | Array<any> | undefined, isArr: boolean, isMap: boolean, isSet: boolean): string[];
116
117
 
117
118
  type TrackingType = undefined | true | symbol;
118
119
  interface GetOptions {
@@ -397,6 +398,7 @@ declare const internal: {
397
398
  ensureNodeValue: typeof ensureNodeValue;
398
399
  findIDKey: typeof findIDKey;
399
400
  get: typeof get;
401
+ getKeys: typeof getKeys;
400
402
  getNode: typeof getNode;
401
403
  getNodeValue: typeof getNodeValue;
402
404
  getPathType: typeof getPathType;
package/index.js CHANGED
@@ -257,6 +257,9 @@ function extractFunction(node, key, fnOrComputed) {
257
257
  function equals(a, b) {
258
258
  return a === b || isDate(a) && isDate(b) && +a === +b;
259
259
  }
260
+ function getKeys(obj, isArr, isMap2, isSet2) {
261
+ return isArr ? void 0 : obj ? isSet2 ? Array.from(obj) : isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
262
+ }
260
263
 
261
264
  // src/ObservableHint.ts
262
265
  function addSymbol(value, symbol) {
@@ -593,6 +596,7 @@ function computeChangesAtNode(changesInBatch, node, isFromPersist, isFromSync, v
593
596
  if (!isArraySubset(changes[0].path, change.path)) {
594
597
  changes.push(change);
595
598
  changeInBatch.level = Math.min(changeInBatch.level, level);
599
+ changeInBatch.whenOptimizedOnlyIf || (changeInBatch.whenOptimizedOnlyIf = whenOptimizedOnlyIf);
596
600
  }
597
601
  } else {
598
602
  changesInBatch.set(node, {
@@ -1180,9 +1184,6 @@ function collectionSetter(node, target, prop, ...args) {
1180
1184
  return ret;
1181
1185
  }
1182
1186
  }
1183
- function getKeys(obj, isArr, isMap2, isSet2) {
1184
- return isArr ? void 0 : obj ? isSet2 ? Array.from(obj) : isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
1185
- }
1186
1187
  function updateNodes(parent, obj, prevValue) {
1187
1188
  var _a, _b, _c;
1188
1189
  if ((process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") && typeof __devUpdateNodes !== "undefined" && isObject(obj)) {
@@ -2340,6 +2341,7 @@ var internal = {
2340
2341
  ensureNodeValue,
2341
2342
  findIDKey,
2342
2343
  get,
2344
+ getKeys,
2343
2345
  getNode,
2344
2346
  getNodeValue,
2345
2347
  getPathType,
package/index.mjs CHANGED
@@ -255,6 +255,9 @@ function extractFunction(node, key, fnOrComputed) {
255
255
  function equals(a, b) {
256
256
  return a === b || isDate(a) && isDate(b) && +a === +b;
257
257
  }
258
+ function getKeys(obj, isArr, isMap2, isSet2) {
259
+ return isArr ? void 0 : obj ? isSet2 ? Array.from(obj) : isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
260
+ }
258
261
 
259
262
  // src/ObservableHint.ts
260
263
  function addSymbol(value, symbol) {
@@ -591,6 +594,7 @@ function computeChangesAtNode(changesInBatch, node, isFromPersist, isFromSync, v
591
594
  if (!isArraySubset(changes[0].path, change.path)) {
592
595
  changes.push(change);
593
596
  changeInBatch.level = Math.min(changeInBatch.level, level);
597
+ changeInBatch.whenOptimizedOnlyIf || (changeInBatch.whenOptimizedOnlyIf = whenOptimizedOnlyIf);
594
598
  }
595
599
  } else {
596
600
  changesInBatch.set(node, {
@@ -1178,9 +1182,6 @@ function collectionSetter(node, target, prop, ...args) {
1178
1182
  return ret;
1179
1183
  }
1180
1184
  }
1181
- function getKeys(obj, isArr, isMap2, isSet2) {
1182
- return isArr ? void 0 : obj ? isSet2 ? Array.from(obj) : isMap2 ? Array.from(obj.keys()) : Object.keys(obj) : [];
1183
- }
1184
1185
  function updateNodes(parent, obj, prevValue) {
1185
1186
  var _a, _b, _c;
1186
1187
  if ((process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") && typeof __devUpdateNodes !== "undefined" && isObject(obj)) {
@@ -2338,6 +2339,7 @@ var internal = {
2338
2339
  ensureNodeValue,
2339
2340
  findIDKey,
2340
2341
  get,
2342
+ getKeys,
2341
2343
  getNode,
2342
2344
  getNodeValue,
2343
2345
  getPathType,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-beta.22",
3
+ "version": "3.0.0-beta.23",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
@@ -4,7 +4,7 @@ var state = require('@legendapp/state');
4
4
  var sync = require('@legendapp/state/sync');
5
5
 
6
6
  // src/sync-plugins/crud.ts
7
- var { clone } = state.internal;
7
+ var { clone, getKeys } = state.internal;
8
8
  var { waitForSet, runWithRetry } = sync.internal;
9
9
  function transformOut(data, transform) {
10
10
  return transform ? transform(clone(data)) : data;
@@ -25,6 +25,17 @@ function computeLastSync(data, fieldUpdatedAt, fieldCreatedAt) {
25
25
  }
26
26
  return newLastSync;
27
27
  }
28
+ function arrayToRecord(arr, keyField) {
29
+ const record = {};
30
+ if (arr == null ? void 0 : arr.length) {
31
+ for (let i = 0; i < arr.length; i++) {
32
+ const v = arr[i];
33
+ const key = v[keyField];
34
+ record[key] = v;
35
+ }
36
+ }
37
+ return record;
38
+ }
28
39
  function retrySet(params, retry, action, itemKey, itemValue, change, queuedRetries, actionFn, saveResult) {
29
40
  if (action === "delete") {
30
41
  if (queuedRetries.create.has(itemKey)) {
@@ -194,6 +205,7 @@ function syncedCrud(props) {
194
205
  ) : itemValue;
195
206
  };
196
207
  changes.forEach((change) => {
208
+ var _a, _b;
197
209
  const { path, prevAtPath, valueAtPath, pathTypes } = change;
198
210
  if (asType === "value") {
199
211
  if (value) {
@@ -240,15 +252,28 @@ function syncedCrud(props) {
240
252
  } else {
241
253
  let itemsChanged = [];
242
254
  if (path.length === 0) {
243
- const changed = asMap ? Array.from(valueAtPath.entries()) : Object.entries(valueAtPath);
244
- for (let i = 0; i < changed.length; i++) {
245
- const [key, value2] = changed[i];
246
- const prev = prevAtPath ? asMap ? prevAtPath.get(key) : prevAtPath[key] : void 0;
255
+ const valueAsObject = asArray ? arrayToRecord(valueAtPath, fieldId) : valueAtPath;
256
+ const prevAsObject = asArray ? arrayToRecord(prevAtPath, fieldId) : prevAtPath;
257
+ const keys = getKeys(valueAsObject, false, asMap, false);
258
+ const keysPrev = getKeys(prevAsObject, false, asMap, false);
259
+ const keysSet = new Set(keys);
260
+ const length = ((_a = keys || valueAsObject) == null ? void 0 : _a.length) || 0;
261
+ const lengthPrev = ((_b = keysPrev || prevAsObject) == null ? void 0 : _b.length) || 0;
262
+ for (let i = 0; i < lengthPrev; i++) {
263
+ const key = keysPrev[i];
264
+ if (!keysSet.has(key)) {
265
+ deletes.add(prevAsObject[key]);
266
+ }
267
+ }
268
+ for (let i = 0; i < length; i++) {
269
+ const key = keys[i];
270
+ const value2 = asMap ? valueAsObject.get(key) : valueAsObject[key];
271
+ const prev = prevAsObject ? asMap ? prevAsObject.get(key) : prevAsObject[key] : void 0;
247
272
  if (state.isNullOrUndefined(value2) && !state.isNullOrUndefined(prev)) {
248
273
  deletes.add(prev);
249
274
  return false;
250
275
  } else {
251
- const isDiff = !prevAtPath || !sync.deepEqual(value2, prev);
276
+ const isDiff = !prevAsObject || !sync.deepEqual(value2, prev);
252
277
  if (isDiff) {
253
278
  itemsChanged.push([getUpdateValue(value2, prev), prev, value2]);
254
279
  }
@@ -2,7 +2,7 @@ import { isPromise, isNullOrUndefined, applyChanges, setAtPath, symbolDelete, is
2
2
  import { synced, deepEqual, internal as internal$1, diffObjects } from '@legendapp/state/sync';
3
3
 
4
4
  // src/sync-plugins/crud.ts
5
- var { clone } = internal;
5
+ var { clone, getKeys } = internal;
6
6
  var { waitForSet, runWithRetry } = internal$1;
7
7
  function transformOut(data, transform) {
8
8
  return transform ? transform(clone(data)) : data;
@@ -23,6 +23,17 @@ function computeLastSync(data, fieldUpdatedAt, fieldCreatedAt) {
23
23
  }
24
24
  return newLastSync;
25
25
  }
26
+ function arrayToRecord(arr, keyField) {
27
+ const record = {};
28
+ if (arr == null ? void 0 : arr.length) {
29
+ for (let i = 0; i < arr.length; i++) {
30
+ const v = arr[i];
31
+ const key = v[keyField];
32
+ record[key] = v;
33
+ }
34
+ }
35
+ return record;
36
+ }
26
37
  function retrySet(params, retry, action, itemKey, itemValue, change, queuedRetries, actionFn, saveResult) {
27
38
  if (action === "delete") {
28
39
  if (queuedRetries.create.has(itemKey)) {
@@ -192,6 +203,7 @@ function syncedCrud(props) {
192
203
  ) : itemValue;
193
204
  };
194
205
  changes.forEach((change) => {
206
+ var _a, _b;
195
207
  const { path, prevAtPath, valueAtPath, pathTypes } = change;
196
208
  if (asType === "value") {
197
209
  if (value) {
@@ -238,15 +250,28 @@ function syncedCrud(props) {
238
250
  } else {
239
251
  let itemsChanged = [];
240
252
  if (path.length === 0) {
241
- const changed = asMap ? Array.from(valueAtPath.entries()) : Object.entries(valueAtPath);
242
- for (let i = 0; i < changed.length; i++) {
243
- const [key, value2] = changed[i];
244
- const prev = prevAtPath ? asMap ? prevAtPath.get(key) : prevAtPath[key] : void 0;
253
+ const valueAsObject = asArray ? arrayToRecord(valueAtPath, fieldId) : valueAtPath;
254
+ const prevAsObject = asArray ? arrayToRecord(prevAtPath, fieldId) : prevAtPath;
255
+ const keys = getKeys(valueAsObject, false, asMap, false);
256
+ const keysPrev = getKeys(prevAsObject, false, asMap, false);
257
+ const keysSet = new Set(keys);
258
+ const length = ((_a = keys || valueAsObject) == null ? void 0 : _a.length) || 0;
259
+ const lengthPrev = ((_b = keysPrev || prevAsObject) == null ? void 0 : _b.length) || 0;
260
+ for (let i = 0; i < lengthPrev; i++) {
261
+ const key = keysPrev[i];
262
+ if (!keysSet.has(key)) {
263
+ deletes.add(prevAsObject[key]);
264
+ }
265
+ }
266
+ for (let i = 0; i < length; i++) {
267
+ const key = keys[i];
268
+ const value2 = asMap ? valueAsObject.get(key) : valueAsObject[key];
269
+ const prev = prevAsObject ? asMap ? prevAsObject.get(key) : prevAsObject[key] : void 0;
245
270
  if (isNullOrUndefined(value2) && !isNullOrUndefined(prev)) {
246
271
  deletes.add(prev);
247
272
  return false;
248
273
  } else {
249
- const isDiff = !prevAtPath || !deepEqual(value2, prev);
274
+ const isDiff = !prevAsObject || !deepEqual(value2, prev);
250
275
  if (isDiff) {
251
276
  itemsChanged.push([getUpdateValue(value2, prev), prev, value2]);
252
277
  }