@legendapp/state 3.0.0-alpha.2 → 3.0.0-alpha.3

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 (5) hide show
  1. package/index.js +15 -5
  2. package/index.mjs +15 -5
  3. package/package.json +1 -1
  4. package/sync.js +17 -18
  5. package/sync.mjs +18 -19
package/index.js CHANGED
@@ -337,13 +337,18 @@ function mergeIntoObservable(target, ...sources) {
337
337
  beginBatch();
338
338
  globalState.isMerging = true;
339
339
  for (let i = 0; i < sources.length; i++) {
340
- target = _mergeIntoObservable(target, sources[i]);
340
+ target = _mergeIntoObservable(
341
+ target,
342
+ sources[i],
343
+ /*assign*/
344
+ i < sources.length - 1
345
+ );
341
346
  }
342
347
  globalState.isMerging = false;
343
348
  endBatch();
344
349
  return target;
345
350
  }
346
- function _mergeIntoObservable(target, source) {
351
+ function _mergeIntoObservable(target, source, assign2) {
347
352
  var _a;
348
353
  if (isObservable(source)) {
349
354
  source = source.peek();
@@ -365,17 +370,22 @@ function _mergeIntoObservable(target, source) {
365
370
  const targetChild = target[key];
366
371
  if ((isObj || isArr) && targetChild && (needsSet || !isEmpty(targetChild))) {
367
372
  if (!needsSet && (!targetChild || (isObj ? !isObject(targetChild) : !isArray(targetChild)))) {
368
- target[key] = sourceValue;
373
+ target[key] = assign2 ? isArr ? [...sourceValue] : { ...sourceValue } : sourceValue;
369
374
  } else {
370
375
  _mergeIntoObservable(targetChild, sourceValue);
371
376
  }
372
377
  } else {
373
- needsSet ? targetChild.set(sourceValue) : target[key] = sourceValue;
378
+ if (needsSet) {
379
+ targetChild.set(sourceValue);
380
+ } else {
381
+ const toSet = isObject(sourceValue) ? { ...sourceValue } : isArray(sourceValue) ? [...sourceValue] : sourceValue;
382
+ target[key] = toSet;
383
+ }
374
384
  }
375
385
  }
376
386
  }
377
387
  } else if (source !== void 0) {
378
- needsSet ? target.set(source) : target = source;
388
+ needsSet ? target.set(source) : target = assign2 ? isArray(source) ? [...source] : { ...source } : source;
379
389
  }
380
390
  return target;
381
391
  }
package/index.mjs CHANGED
@@ -335,13 +335,18 @@ function mergeIntoObservable(target, ...sources) {
335
335
  beginBatch();
336
336
  globalState.isMerging = true;
337
337
  for (let i = 0; i < sources.length; i++) {
338
- target = _mergeIntoObservable(target, sources[i]);
338
+ target = _mergeIntoObservable(
339
+ target,
340
+ sources[i],
341
+ /*assign*/
342
+ i < sources.length - 1
343
+ );
339
344
  }
340
345
  globalState.isMerging = false;
341
346
  endBatch();
342
347
  return target;
343
348
  }
344
- function _mergeIntoObservable(target, source) {
349
+ function _mergeIntoObservable(target, source, assign2) {
345
350
  var _a;
346
351
  if (isObservable(source)) {
347
352
  source = source.peek();
@@ -363,17 +368,22 @@ function _mergeIntoObservable(target, source) {
363
368
  const targetChild = target[key];
364
369
  if ((isObj || isArr) && targetChild && (needsSet || !isEmpty(targetChild))) {
365
370
  if (!needsSet && (!targetChild || (isObj ? !isObject(targetChild) : !isArray(targetChild)))) {
366
- target[key] = sourceValue;
371
+ target[key] = assign2 ? isArr ? [...sourceValue] : { ...sourceValue } : sourceValue;
367
372
  } else {
368
373
  _mergeIntoObservable(targetChild, sourceValue);
369
374
  }
370
375
  } else {
371
- needsSet ? targetChild.set(sourceValue) : target[key] = sourceValue;
376
+ if (needsSet) {
377
+ targetChild.set(sourceValue);
378
+ } else {
379
+ const toSet = isObject(sourceValue) ? { ...sourceValue } : isArray(sourceValue) ? [...sourceValue] : sourceValue;
380
+ target[key] = toSet;
381
+ }
372
382
  }
373
383
  }
374
384
  }
375
385
  } else if (source !== void 0) {
376
- needsSet ? target.set(source) : target = source;
386
+ needsSet ? target.set(source) : target = assign2 ? isArray(source) ? [...source] : { ...source } : source;
377
387
  }
378
388
  return target;
379
389
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@legendapp/state",
3
- "version": "3.0.0-alpha.2",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "legend-state",
5
5
  "sideEffects": false,
6
6
  "private": false,
package/sync.js CHANGED
@@ -205,19 +205,16 @@ async function updateMetadataImmediate(value$, localState, syncState, syncOption
205
205
  const { pluginPersist } = localState;
206
206
  const { table, config } = parseLocalConfig(syncOptions == null ? void 0 : syncOptions.persist);
207
207
  const oldMetadata = metadatas.get(value$);
208
- const { lastSync, pending } = newMetadata;
209
- const needsUpdate = pending || lastSync && (!oldMetadata || lastSync !== oldMetadata.lastSync);
210
- if (needsUpdate) {
211
- const metadata = Object.assign({}, oldMetadata, newMetadata);
212
- metadatas.set(value$, metadata);
213
- if (pluginPersist) {
214
- await pluginPersist.setMetadata(table, metadata, config);
215
- }
216
- if (lastSync) {
217
- syncState.assign({
218
- lastSync
219
- });
220
- }
208
+ const { lastSync } = newMetadata;
209
+ const metadata = Object.assign({}, oldMetadata, newMetadata);
210
+ metadatas.set(value$, metadata);
211
+ if (pluginPersist) {
212
+ await pluginPersist.setMetadata(table, metadata, config);
213
+ }
214
+ if (lastSync) {
215
+ syncState.assign({
216
+ lastSync
217
+ });
221
218
  }
222
219
  }
223
220
  function updateMetadata(value$, localState, syncState, syncOptions, newMetadata) {
@@ -692,11 +689,13 @@ function syncObservable(obs$, syncOptionsOrSynced) {
692
689
  if ((process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") && (!obs$ || !node)) {
693
690
  throw new Error("[legend-state] syncObservable called with undefined observable");
694
691
  }
695
- syncOptions = {
696
- syncMode: "auto",
697
- ...observableSyncConfiguration,
698
- ...removeNullUndefined(syncOptions || {})
699
- };
692
+ syncOptions = state.mergeIntoObservable(
693
+ {
694
+ syncMode: "auto"
695
+ },
696
+ observableSyncConfiguration,
697
+ removeNullUndefined(syncOptions || {})
698
+ );
700
699
  const localState = {};
701
700
  let sync;
702
701
  const syncState = node.state = state.observable({
package/sync.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { isObject, isDate, isNullOrUndefined, isString, endBatch, beginBatch, isFunction, observable, when, linked, internal, isPromise, mergeIntoObservable, isEmpty, shouldIgnoreUnobserved, whenReady, constructObjectWithPath, deconstructObjectWithPath, setAtPath, isArray } from '@legendapp/state';
1
+ import { isObject, isDate, isNullOrUndefined, isString, endBatch, beginBatch, isFunction, mergeIntoObservable, observable, when, linked, internal, isPromise, isEmpty, shouldIgnoreUnobserved, whenReady, constructObjectWithPath, deconstructObjectWithPath, setAtPath, isArray } from '@legendapp/state';
2
2
 
3
3
  // sync.ts
4
4
 
@@ -203,19 +203,16 @@ async function updateMetadataImmediate(value$, localState, syncState, syncOption
203
203
  const { pluginPersist } = localState;
204
204
  const { table, config } = parseLocalConfig(syncOptions == null ? void 0 : syncOptions.persist);
205
205
  const oldMetadata = metadatas.get(value$);
206
- const { lastSync, pending } = newMetadata;
207
- const needsUpdate = pending || lastSync && (!oldMetadata || lastSync !== oldMetadata.lastSync);
208
- if (needsUpdate) {
209
- const metadata = Object.assign({}, oldMetadata, newMetadata);
210
- metadatas.set(value$, metadata);
211
- if (pluginPersist) {
212
- await pluginPersist.setMetadata(table, metadata, config);
213
- }
214
- if (lastSync) {
215
- syncState.assign({
216
- lastSync
217
- });
218
- }
206
+ const { lastSync } = newMetadata;
207
+ const metadata = Object.assign({}, oldMetadata, newMetadata);
208
+ metadatas.set(value$, metadata);
209
+ if (pluginPersist) {
210
+ await pluginPersist.setMetadata(table, metadata, config);
211
+ }
212
+ if (lastSync) {
213
+ syncState.assign({
214
+ lastSync
215
+ });
219
216
  }
220
217
  }
221
218
  function updateMetadata(value$, localState, syncState, syncOptions, newMetadata) {
@@ -690,11 +687,13 @@ function syncObservable(obs$, syncOptionsOrSynced) {
690
687
  if ((process.env.NODE_ENV === "development" || process.env.NODE_ENV === "test") && (!obs$ || !node)) {
691
688
  throw new Error("[legend-state] syncObservable called with undefined observable");
692
689
  }
693
- syncOptions = {
694
- syncMode: "auto",
695
- ...observableSyncConfiguration,
696
- ...removeNullUndefined(syncOptions || {})
697
- };
690
+ syncOptions = mergeIntoObservable(
691
+ {
692
+ syncMode: "auto"
693
+ },
694
+ observableSyncConfiguration,
695
+ removeNullUndefined(syncOptions || {})
696
+ );
698
697
  const localState = {};
699
698
  let sync;
700
699
  const syncState = node.state = observable({