@legendapp/state 3.0.0-beta.31 → 3.0.0-beta.32

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/README.md CHANGED
@@ -72,7 +72,7 @@ Legend-State includes a powerful [sync and persistence system](../../usage/persi
72
72
  Local persistence plugins for the browser and React Native are included, with sync plugins for [Keel](https://www.keel.so), [Supabase](https://www.supabase.com), [TanStack Query](https://tanstack.com/query), and `fetch`.
73
73
 
74
74
  ```js
75
- const state$ = observable(
75
+ const state$ = observable({
76
76
  users: syncedKeel({
77
77
  list: queries.getUsers,
78
78
  create: mutations.createUsers,
@@ -87,7 +87,7 @@ const state$ = observable(
87
87
  }),
88
88
  // direct link to my user within the users observable
89
89
  me: () => state$.users['myuid']
90
- )
90
+ })
91
91
 
92
92
  observe(() => {
93
93
  // get() activates through to state$.users and starts syncing.
package/index.d.mts CHANGED
@@ -111,6 +111,7 @@ declare function setNodeValue(node: NodeInfo, newValue: any): {
111
111
  parentValue: any;
112
112
  };
113
113
  declare function getNodeValue(node: NodeInfo): any;
114
+ declare function getChildNode(node: NodeInfo, key: string, asFunction?: Function): NodeInfo;
114
115
  declare function ensureNodeValue(node: NodeInfo): any;
115
116
  declare function findIDKey(obj: unknown | undefined, node: NodeInfo): string | ((value: any) => string) | undefined;
116
117
  declare function getKeys(obj: Record<any, any> | Array<any> | undefined, isArr: boolean, isMap: boolean, isSet: boolean): string[];
@@ -418,6 +419,7 @@ declare const internal: {
418
419
  ensureNodeValue: typeof ensureNodeValue;
419
420
  findIDKey: typeof findIDKey;
420
421
  get: typeof get;
422
+ getChildNode: typeof getChildNode;
421
423
  getKeys: typeof getKeys;
422
424
  getNode: typeof getNode;
423
425
  getNodeValue: typeof getNodeValue;
package/index.d.ts CHANGED
@@ -111,6 +111,7 @@ declare function setNodeValue(node: NodeInfo, newValue: any): {
111
111
  parentValue: any;
112
112
  };
113
113
  declare function getNodeValue(node: NodeInfo): any;
114
+ declare function getChildNode(node: NodeInfo, key: string, asFunction?: Function): NodeInfo;
114
115
  declare function ensureNodeValue(node: NodeInfo): any;
115
116
  declare function findIDKey(obj: unknown | undefined, node: NodeInfo): string | ((value: any) => string) | undefined;
116
117
  declare function getKeys(obj: Record<any, any> | Array<any> | undefined, isArr: boolean, isMap: boolean, isSet: boolean): string[];
@@ -418,6 +419,7 @@ declare const internal: {
418
419
  ensureNodeValue: typeof ensureNodeValue;
419
420
  findIDKey: typeof findIDKey;
420
421
  get: typeof get;
422
+ getChildNode: typeof getChildNode;
421
423
  getKeys: typeof getKeys;
422
424
  getNode: typeof getNode;
423
425
  getNodeValue: typeof getNodeValue;
package/index.js CHANGED
@@ -2460,6 +2460,7 @@ var internal = {
2460
2460
  ensureNodeValue,
2461
2461
  findIDKey,
2462
2462
  get,
2463
+ getChildNode,
2463
2464
  getKeys,
2464
2465
  getNode,
2465
2466
  getNodeValue,
package/index.mjs CHANGED
@@ -2458,6 +2458,7 @@ var internal = {
2458
2458
  ensureNodeValue,
2459
2459
  findIDKey,
2460
2460
  get,
2461
+ getChildNode,
2461
2462
  getKeys,
2462
2463
  getNode,
2463
2464
  getNodeValue,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-beta.31",
3
+ "version": "3.0.0-beta.32",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
package/react.js CHANGED
@@ -527,15 +527,15 @@ function useObserve(selector, reactionOrOptions, options) {
527
527
  }
528
528
  const deps = options == null ? void 0 : options.deps;
529
529
  const depsObs$ = deps ? useObservable(deps) : void 0;
530
- if (depsObs$) {
531
- depsObs$.set(deps);
532
- }
533
530
  const ref = React.useRef({});
534
531
  ref.current.selector = deps ? () => {
535
532
  depsObs$ == null ? void 0 : depsObs$.get();
536
533
  return state.computeSelector(selector);
537
534
  } : selector;
538
535
  ref.current.reaction = reaction;
536
+ if (depsObs$) {
537
+ depsObs$.set(deps);
538
+ }
539
539
  if (!ref.current.dispose) {
540
540
  ref.current.dispose = state.observe(
541
541
  (e) => state.computeSelector(ref.current.selector, void 0, e),
@@ -559,13 +559,13 @@ function useObserveEffect(selector, reactionOrOptions, options) {
559
559
  } else {
560
560
  options = reactionOrOptions;
561
561
  }
562
+ const ref = React.useRef({ selector });
563
+ ref.current = { selector, reaction };
562
564
  const deps = options == null ? void 0 : options.deps;
563
565
  const depsObs$ = deps ? useObservable(deps) : void 0;
564
566
  if (depsObs$) {
565
567
  depsObs$.set(deps);
566
568
  }
567
- const ref = React.useRef({ selector });
568
- ref.current = { selector, reaction };
569
569
  useMountOnce(
570
570
  () => state.observe(
571
571
  (e) => {
package/react.mjs CHANGED
@@ -521,15 +521,15 @@ function useObserve(selector, reactionOrOptions, options) {
521
521
  }
522
522
  const deps = options == null ? void 0 : options.deps;
523
523
  const depsObs$ = deps ? useObservable(deps) : void 0;
524
- if (depsObs$) {
525
- depsObs$.set(deps);
526
- }
527
524
  const ref = useRef({});
528
525
  ref.current.selector = deps ? () => {
529
526
  depsObs$ == null ? void 0 : depsObs$.get();
530
527
  return computeSelector(selector);
531
528
  } : selector;
532
529
  ref.current.reaction = reaction;
530
+ if (depsObs$) {
531
+ depsObs$.set(deps);
532
+ }
533
533
  if (!ref.current.dispose) {
534
534
  ref.current.dispose = observe(
535
535
  (e) => computeSelector(ref.current.selector, void 0, e),
@@ -553,13 +553,13 @@ function useObserveEffect(selector, reactionOrOptions, options) {
553
553
  } else {
554
554
  options = reactionOrOptions;
555
555
  }
556
+ const ref = useRef({ selector });
557
+ ref.current = { selector, reaction };
556
558
  const deps = options == null ? void 0 : options.deps;
557
559
  const depsObs$ = deps ? useObservable(deps) : void 0;
558
560
  if (depsObs$) {
559
561
  depsObs$.set(deps);
560
562
  }
561
- const ref = useRef({ selector });
562
- ref.current = { selector, reaction };
563
563
  useMountOnce(
564
564
  () => observe(
565
565
  (e) => {
@@ -4,16 +4,18 @@ 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, getKeys } = state.internal;
7
+ var { clone, getKeys, setNodeValue, getChildNode } = state.internal;
8
8
  var { waitForSet, runWithRetry } = sync.internal;
9
9
  function transformOut(data, transform) {
10
10
  return transform ? transform(clone(data)) : data;
11
11
  }
12
- function ensureId(obj, fieldId, generateId) {
12
+ function ensureId(obj, fieldId, generateId, node) {
13
13
  if (!obj[fieldId]) {
14
14
  obj[fieldId] = generateId();
15
+ if (node) {
16
+ setNodeValue(node, obj);
17
+ }
15
18
  }
16
- return obj[fieldId];
17
19
  }
18
20
  function computeLastSync(data, fieldUpdatedAt, fieldCreatedAt) {
19
21
  let newLastSync = 0;
@@ -210,11 +212,11 @@ function syncedCrud(props) {
210
212
  const { path, prevAtPath, valueAtPath, pathTypes } = change;
211
213
  if (asType === "value") {
212
214
  if (value) {
213
- let id = value == null ? void 0 : value[fieldId];
214
215
  let isCreate = fieldCreatedAt ? !value[fieldCreatedAt] : !prevAtPath;
215
- if (state.isNullOrUndefined(id) && generateId) {
216
- id = ensureId(value, fieldId, generateId);
216
+ if (state.isNullOrUndefined(value[fieldId]) && generateId) {
217
+ ensureId(value, fieldId, generateId, node);
217
218
  }
219
+ const id = value[fieldId];
218
220
  if (!state.isNullOrUndefined(id)) {
219
221
  changesById.set(id, change);
220
222
  if (pendingCreates.has(id)) {
@@ -253,6 +255,14 @@ function syncedCrud(props) {
253
255
  } else {
254
256
  let itemsChanged = [];
255
257
  if (path.length === 0) {
258
+ if (asArray && generateId) {
259
+ for (let i = 0; i < valueAtPath.length; i++) {
260
+ const value2 = valueAtPath[i];
261
+ if (value2 && !value2[fieldId]) {
262
+ ensureId(value2, fieldId, generateId, getChildNode(node, i + ""));
263
+ }
264
+ }
265
+ }
256
266
  const valueAsObject = asArray ? arrayToRecord(valueAtPath, fieldId) : valueAtPath;
257
267
  const prevAsObject = asArray ? arrayToRecord(prevAtPath, fieldId) : prevAtPath;
258
268
  const keys = getKeys(valueAsObject, false, asMap, false);
@@ -289,6 +299,9 @@ function syncedCrud(props) {
289
299
  changesById.set(prevAtPath[fieldId], change);
290
300
  }
291
301
  } else {
302
+ if (generateId) {
303
+ ensureId(itemValue, fieldId, generateId, getChildNode(node, itemKey + ""));
304
+ }
292
305
  const previous = state.setAtPath(
293
306
  clone(itemValue),
294
307
  path.slice(1),
@@ -301,9 +314,6 @@ function syncedCrud(props) {
301
314
  itemsChanged == null ? void 0 : itemsChanged.forEach(([item, prev, fullValue]) => {
302
315
  const isCreate = !pendingCreates.has(item[fieldId]) && (fieldCreatedAt ? !item[fieldCreatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : fieldUpdatedAt ? !item[fieldUpdatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : state.isNullOrUndefined(prev));
303
316
  if (isCreate) {
304
- if (generateId) {
305
- ensureId(item, fieldId, generateId);
306
- }
307
317
  if (!item[fieldId]) {
308
318
  console.error("[legend-state]: added item without an id");
309
319
  }
@@ -2,16 +2,18 @@ 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, getKeys } = internal;
5
+ var { clone, getKeys, setNodeValue, getChildNode } = internal;
6
6
  var { waitForSet, runWithRetry } = internal$1;
7
7
  function transformOut(data, transform) {
8
8
  return transform ? transform(clone(data)) : data;
9
9
  }
10
- function ensureId(obj, fieldId, generateId) {
10
+ function ensureId(obj, fieldId, generateId, node) {
11
11
  if (!obj[fieldId]) {
12
12
  obj[fieldId] = generateId();
13
+ if (node) {
14
+ setNodeValue(node, obj);
15
+ }
13
16
  }
14
- return obj[fieldId];
15
17
  }
16
18
  function computeLastSync(data, fieldUpdatedAt, fieldCreatedAt) {
17
19
  let newLastSync = 0;
@@ -208,11 +210,11 @@ function syncedCrud(props) {
208
210
  const { path, prevAtPath, valueAtPath, pathTypes } = change;
209
211
  if (asType === "value") {
210
212
  if (value) {
211
- let id = value == null ? void 0 : value[fieldId];
212
213
  let isCreate = fieldCreatedAt ? !value[fieldCreatedAt] : !prevAtPath;
213
- if (isNullOrUndefined(id) && generateId) {
214
- id = ensureId(value, fieldId, generateId);
214
+ if (isNullOrUndefined(value[fieldId]) && generateId) {
215
+ ensureId(value, fieldId, generateId, node);
215
216
  }
217
+ const id = value[fieldId];
216
218
  if (!isNullOrUndefined(id)) {
217
219
  changesById.set(id, change);
218
220
  if (pendingCreates.has(id)) {
@@ -251,6 +253,14 @@ function syncedCrud(props) {
251
253
  } else {
252
254
  let itemsChanged = [];
253
255
  if (path.length === 0) {
256
+ if (asArray && generateId) {
257
+ for (let i = 0; i < valueAtPath.length; i++) {
258
+ const value2 = valueAtPath[i];
259
+ if (value2 && !value2[fieldId]) {
260
+ ensureId(value2, fieldId, generateId, getChildNode(node, i + ""));
261
+ }
262
+ }
263
+ }
254
264
  const valueAsObject = asArray ? arrayToRecord(valueAtPath, fieldId) : valueAtPath;
255
265
  const prevAsObject = asArray ? arrayToRecord(prevAtPath, fieldId) : prevAtPath;
256
266
  const keys = getKeys(valueAsObject, false, asMap, false);
@@ -287,6 +297,9 @@ function syncedCrud(props) {
287
297
  changesById.set(prevAtPath[fieldId], change);
288
298
  }
289
299
  } else {
300
+ if (generateId) {
301
+ ensureId(itemValue, fieldId, generateId, getChildNode(node, itemKey + ""));
302
+ }
290
303
  const previous = setAtPath(
291
304
  clone(itemValue),
292
305
  path.slice(1),
@@ -299,9 +312,6 @@ function syncedCrud(props) {
299
312
  itemsChanged == null ? void 0 : itemsChanged.forEach(([item, prev, fullValue]) => {
300
313
  const isCreate = !pendingCreates.has(item[fieldId]) && (fieldCreatedAt ? !item[fieldCreatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : fieldUpdatedAt ? !item[fieldUpdatedAt] && !(prev == null ? void 0 : prev[fieldCreatedAt]) : isNullOrUndefined(prev));
301
314
  if (isCreate) {
302
- if (generateId) {
303
- ensureId(item, fieldId, generateId);
304
- }
305
315
  if (!item[fieldId]) {
306
316
  console.error("[legend-state]: added item without an id");
307
317
  }