@ember-data/store 4.10.0-beta.0 → 4.10.0-beta.2

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.
@@ -423,16 +423,21 @@ export class InstanceCache {
423
423
  }
424
424
  }
425
425
 
426
+ let removeFromRecordArray = true;
426
427
  if (recordData) {
428
+ removeFromRecordArray = !recordData.isDeletionCommitted(identifier);
427
429
  recordData.unloadRecord(identifier);
428
430
  this.__instances.recordData.delete(identifier);
429
431
  removeRecordDataFor(identifier);
430
432
  } else {
433
+ removeFromRecordArray = false;
431
434
  this.disconnect(identifier);
432
435
  }
433
436
 
434
437
  this.store._fetchManager.clearEntries(identifier);
435
- this.store.recordArrayManager.identifierRemoved(identifier);
438
+ if (removeFromRecordArray) {
439
+ this.store.recordArrayManager.identifierRemoved(identifier);
440
+ }
436
441
  if (LOG_INSTANCE_CACHE) {
437
442
  // eslint-disable-next-line no-console
438
443
  console.log(`InstanceCache: unloaded RecordData for ${String(identifier)}`);
@@ -357,7 +357,10 @@ function sync(array: IdentifierArray, changes: Map<StableRecordIdentifier, 'add'
357
357
  // state = array[SOURCE] = [];
358
358
  } else {
359
359
  removes.forEach((i) => {
360
- state.splice(state.indexOf(i), 1);
360
+ const index = state.indexOf(i);
361
+ if (index !== -1) {
362
+ state.splice(index, 1);
363
+ }
361
364
  });
362
365
  }
363
366
  }
@@ -119,7 +119,7 @@ export class NonSingletonRecordDataManager implements RecordData {
119
119
  // called by something V1
120
120
  if (!isStableIdentifier(identifier)) {
121
121
  data = identifier as JsonApiResource;
122
- hasRecord = data as boolean;
122
+ hasRecord = data as unknown as boolean;
123
123
  identifier = this.#identifier;
124
124
  }
125
125
  if (this.#isDeprecated(recordData)) {
@@ -114,6 +114,33 @@ interface PrivateState {
114
114
  links: Links | PaginationLinks | null;
115
115
  meta: Dict<unknown> | null;
116
116
  }
117
+ type ForEachCB = (record: RecordInstance, index: number, context: IdentifierArray) => void;
118
+ function safeForEach(
119
+ instance: IdentifierArray,
120
+ arr: StableRecordIdentifier[],
121
+ store: Store,
122
+ callback: ForEachCB,
123
+ target: unknown
124
+ ) {
125
+ if (target === undefined) {
126
+ target = null;
127
+ }
128
+ // clone to prevent mutation
129
+ arr = arr.slice();
130
+ assert('`forEach` expects a function as first argument.', typeof callback === 'function');
131
+
132
+ // because we retrieveLatest above we need not worry if array is mutated during iteration
133
+ // by unloadRecord/rollbackAttributes
134
+ // push/add/removeObject may still be problematic
135
+ // but this is a more traditionally expected forEach bug.
136
+ const length = arr.length; // we need to access length to ensure we are consumed
137
+
138
+ for (let index = 0; index < length; index++) {
139
+ callback.call(target, store._instanceCache.getRecord(arr[index]), index, instance);
140
+ }
141
+
142
+ return instance;
143
+ }
117
144
 
118
145
  /**
119
146
  A record array is an array that contains records of a certain type (or modelName).
@@ -241,15 +268,25 @@ class IdentifierArray {
241
268
  let fn = boundFns.get(prop);
242
269
 
243
270
  if (fn === undefined) {
244
- fn = function () {
245
- subscribe(_TAG);
246
- // array functions must run through Reflect to work properly
247
- // binding via other means will not work.
248
- transaction = true;
249
- let result = Reflect.apply(target[prop] as ProxiedMethod, receiver, arguments) as unknown;
250
- transaction = false;
251
- return result;
252
- };
271
+ if (prop === 'forEach') {
272
+ fn = function () {
273
+ subscribe(_TAG);
274
+ transaction = true;
275
+ let result = safeForEach(receiver, target, store, arguments[0] as ForEachCB, arguments[1]);
276
+ transaction = false;
277
+ return result;
278
+ };
279
+ } else {
280
+ fn = function () {
281
+ subscribe(_TAG);
282
+ // array functions must run through Reflect to work properly
283
+ // binding via other means will not work.
284
+ transaction = true;
285
+ let result = Reflect.apply(target[prop] as ProxiedMethod, receiver, arguments) as unknown;
286
+ transaction = false;
287
+ return result;
288
+ };
289
+ }
253
290
 
254
291
  boundFns.set(prop, fn);
255
292
  }
@@ -271,7 +308,7 @@ class IdentifierArray {
271
308
  const args: unknown[] = Array.prototype.slice.call(arguments);
272
309
  assert(`Cannot start a new array transaction while a previous transaction is underway`, !transaction);
273
310
  transaction = true;
274
- let result = Reflect.apply(target[prop] as ProxiedMethod, receiver, args) as unknown;
311
+ let result: unknown = Reflect.apply(target[prop] as ProxiedMethod, receiver, args);
275
312
  self[MUTATE]!(prop as string, args, result);
276
313
  addToTransaction(_TAG);
277
314
  // TODO handle cache updates
@@ -658,13 +695,21 @@ if (DEPRECATE_ARRAY_LIKE) {
658
695
 
659
696
  IdentifierArray.prototype.removeObject = function (obj: RecordInstance) {
660
697
  deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'removeObject', 'splice');
661
- this.splice(this.indexOf(obj), 1);
698
+ const index = this.indexOf(obj);
699
+ if (index !== -1) {
700
+ this.splice(index, 1);
701
+ }
662
702
  return this;
663
703
  };
664
704
 
665
705
  IdentifierArray.prototype.removeObjects = function (objs: RecordInstance[]) {
666
706
  deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'removeObjects', 'splice');
667
- objs.forEach((obj) => this.splice(this.indexOf(obj), 1));
707
+ objs.forEach((obj) => {
708
+ const index = this.indexOf(obj);
709
+ if (index !== -1) {
710
+ this.splice(index, 1);
711
+ }
712
+ });
668
713
  return this;
669
714
  };
670
715
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ember-data/store",
3
- "version": "4.10.0-beta.0",
3
+ "version": "4.10.0-beta.2",
4
4
  "description": "The core of EmberData. Provides the Store service which coordinates the cache with the network and presentation layers.",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -14,9 +14,9 @@
14
14
  "author": "",
15
15
  "directories": {},
16
16
  "peerDependencies": {
17
- "@ember-data/model": "4.10.0-beta.0",
18
- "@ember-data/record-data": "4.10.0-beta.0",
19
- "@ember-data/tracking": "4.10.0-beta.0",
17
+ "@ember-data/model": "4.10.0-beta.2",
18
+ "@ember-data/record-data": "4.10.0-beta.2",
19
+ "@ember-data/tracking": "4.10.0-beta.2",
20
20
  "@ember/string": "^3.0.0",
21
21
  "@glimmer/tracking": "^1.1.2"
22
22
  },
@@ -37,10 +37,10 @@
37
37
  }
38
38
  },
39
39
  "dependencies": {
40
- "@ember-data/canary-features": "4.10.0-beta.0",
41
- "@ember-data/private-build-infra": "4.10.0-beta.0",
40
+ "@ember-data/canary-features": "4.10.0-beta.2",
41
+ "@ember-data/private-build-infra": "4.10.0-beta.2",
42
42
  "@embroider/macros": "^1.10.0",
43
- "ember-auto-import": "^2.4.3",
43
+ "ember-auto-import": "^2.5.0",
44
44
  "ember-cached-decorator-polyfill": "^1.0.1",
45
45
  "ember-cli-babel": "^7.26.11"
46
46
  },
@@ -57,5 +57,5 @@
57
57
  "volta": {
58
58
  "extends": "../../package.json"
59
59
  },
60
- "packageManager": "pnpm@7.15.0"
60
+ "packageManager": "pnpm@7.18.0"
61
61
  }