@nymphjs/client 1.0.0-beta.131 → 1.0.0-beta.133

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.
@@ -36,7 +36,8 @@ export type NymphOptions = {
36
36
  * same class and GUID. This also means that whenever an entity is returned
37
37
  * from the server, the single instance in memory will be refreshed. This
38
38
  * could have annoying results, like destroying dirty data (the dreaded
39
- * triple-D).
39
+ * triple-D). The dirty data problem can be partially mitigated with
40
+ * weakCacheKeepDirtyData.
40
41
  *
41
42
  * This could also be a potential source of memory leaks. Although the
42
43
  * entities themselves are referenced weakly so they get garbage collected,
@@ -48,6 +49,17 @@ export type NymphOptions = {
48
49
  * can help to synchronize them correctly and avoid data conflicts.
49
50
  */
50
51
  weakCache?: boolean;
52
+ /**
53
+ * Keep dirty data after receiving unrelated updates from the server.
54
+ *
55
+ * When an entity in the weak cache is updated from the server due to an
56
+ * unrelated query (ie, not because it was just saved), all of the properties
57
+ * that are currently marked dirty will not be overwritten by the new data.
58
+ *
59
+ * This could potentially have negative consequences if your data is
60
+ * complicated and interrelated.
61
+ */
62
+ weakCacheKeepDirtyData?: boolean;
51
63
  /**
52
64
  * Whether to renew tokens when a request is made.
53
65
  *
package/src/PubSub.ts CHANGED
@@ -3,6 +3,7 @@ import type { NymphOptions, Options, Selector } from './Nymph.types.js';
3
3
  import type { EntityInstanceType } from './Entity.js';
4
4
  import type {
5
5
  EntityConstructor,
6
+ EntityData,
6
7
  EntityInterface,
7
8
  EntityJson,
8
9
  } from './Entity.types.js';
@@ -219,7 +220,7 @@ export default class PubSub {
219
220
  const newResolve = (args: any) => {
220
221
  if (Array.isArray(args)) {
221
222
  if (args.length) {
222
- entity.$init(args[0].toJSON());
223
+ entity.$init(args[0].toJSON(), false);
223
224
  } else {
224
225
  entity.guid = null;
225
226
  }
@@ -231,7 +232,7 @@ export default class PubSub {
231
232
  if ('removed' in update) {
232
233
  entity.guid = null;
233
234
  } else {
234
- entity.$init(update.data);
235
+ entity.$init(update.data, false);
235
236
  }
236
237
  }
237
238
 
@@ -387,7 +388,9 @@ export default class PubSub {
387
388
  if (typeof callback === 'function') {
388
389
  callback(
389
390
  set
390
- ? data.data.map((e: EntityJson) => this.nymph.initEntity(e))
391
+ ? data.data.map((e: EntityJson) =>
392
+ this.nymph.initEntity(e, false),
393
+ )
391
394
  : data,
392
395
  );
393
396
  }
@@ -629,8 +632,8 @@ export default class PubSub {
629
632
  }
630
633
 
631
634
  public updateArray(
632
- current: EntityInterface[],
633
- update: PubSubUpdate<EntityInterface[]>,
635
+ current: (EntityInterface & EntityData)[],
636
+ update: PubSubUpdate<(EntityInterface & EntityData)[]>,
634
637
  ) {
635
638
  if (Array.isArray(update)) {
636
639
  const newArr = [...update];
@@ -641,8 +644,8 @@ export default class PubSub {
641
644
  return;
642
645
  }
643
646
 
644
- const idMap: { [k: string]: EntityInterface } = {};
645
- const newEntities: EntityInterface[] = [];
647
+ const idMap: { [k: string]: EntityInterface & EntityData } = {};
648
+ const newEntities: (EntityInterface & EntityData)[] = [];
646
649
  while (current.length) {
647
650
  const first = current.shift();
648
651
  if (!first) {
@@ -668,7 +671,10 @@ export default class PubSub {
668
671
  current.push(entity);
669
672
  } else if (idMap[guid].mdate !== entity.mdate) {
670
673
  // It was modified.
671
- idMap[guid].$init(entity.toJSON());
674
+ const json = entity.toJSON();
675
+ if (!Array.isArray(json)) {
676
+ idMap[guid].$init(json, false);
677
+ }
672
678
  current.push(idMap[guid]);
673
679
  } else {
674
680
  // Item wasn't modified.
@@ -691,24 +697,24 @@ export default class PubSub {
691
697
  }
692
698
 
693
699
  // Get the entity.
694
- let entity: EntityInterface | null = null;
700
+ let entity: (EntityInterface & EntityData) | null = null;
695
701
  if ('added' in curUpdate) {
696
702
  // Check for it in the array already.
697
703
  for (let i = 0; i < current.length; i++) {
698
704
  if (current[i] != null && current[i].guid === curUpdate.added) {
699
- entity = current.splice(i, 1)[0].$init(curUpdate.data);
705
+ entity = current.splice(i, 1)[0].$init(curUpdate.data, false);
700
706
  }
701
707
  }
702
708
  if (entity == null) {
703
709
  // A new entity.
704
- entity = this.nymph.initEntity(curUpdate.data);
710
+ entity = this.nymph.initEntity(curUpdate.data, false);
705
711
  }
706
712
  }
707
713
  if ('updated' in curUpdate) {
708
714
  // Extract it from the array.
709
715
  for (let i = 0; i < current.length; i++) {
710
716
  if (current[i] != null && current[i].guid === curUpdate.updated) {
711
- entity = current.splice(i, 1)[0].$init(curUpdate.data);
717
+ entity = current.splice(i, 1)[0].$init(curUpdate.data, false);
712
718
  }
713
719
  }
714
720
  }