@ember-data/store 4.12.4 → 4.12.6

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/addon/-private.js CHANGED
@@ -1 +1 @@
1
- export { f as AdapterPopulatedRecordArray, C as CacheHandler, j as IDENTIFIER_ARRAY_TAG, I as IdentifierArray, M as MUTATE, I as RecordArray, R as RecordArrayManager, h as SOURCE, S as Store, _ as _clearCaches, e as coerceId, k as fastPush, i as isStableIdentifier, n as normalizeModelName, g as notifyArray, p as peekCache, r as recordIdentifierFor, l as removeRecordDataFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, s as storeFor } from "./index-7883b099";
1
+ export { f as AdapterPopulatedRecordArray, C as CacheHandler, j as IDENTIFIER_ARRAY_TAG, I as IdentifierArray, M as MUTATE, I as RecordArray, R as RecordArrayManager, h as SOURCE, S as Store, _ as _clearCaches, e as coerceId, k as fastPush, i as isStableIdentifier, n as normalizeModelName, g as notifyArray, p as peekCache, r as recordIdentifierFor, l as removeRecordDataFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, s as storeFor } from "./index-ecc2c2e5";
@@ -1332,7 +1332,7 @@ function legacyCachePut(store, doc) {
1332
1332
  * `recordDataFor` function supplied to the store
1333
1333
  * hook `instantiateRecord`, and the `recordDataFor`
1334
1334
  * method on the `CacheStoreWrapper`. It is not
1335
- * directly instantiatable.
1335
+ * directly instantiable.
1336
1336
  *
1337
1337
  * It handles translating between cache versions when
1338
1338
  * necessary, for instance when a Store is configured
@@ -1412,7 +1412,7 @@ class NonSingletonCacheManager {
1412
1412
  * semantics, `put` has `replace` semantics similar to
1413
1413
  * the `http` method `PUT`
1414
1414
  *
1415
- * the individually cacheabl
1415
+ * the individually cacheable
1416
1416
  * e resource data it may contain
1417
1417
  * should upsert, but the document data surrounding it should
1418
1418
  * fully replace any existing information
@@ -1517,7 +1517,7 @@ class NonSingletonCacheManager {
1517
1517
  * An implementation might want to do this because
1518
1518
  * de-referencing records which read from their own
1519
1519
  * blob is generally safer because the record does
1520
- * not require retainining connections to the Store
1520
+ * not require retaining connections to the Store
1521
1521
  * and Cache to present data on a per-field basis.
1522
1522
  *
1523
1523
  * This generally takes the place of `getAttr` as
@@ -1746,7 +1746,7 @@ class NonSingletonCacheManager {
1746
1746
  // ================
1747
1747
 
1748
1748
  /**
1749
- * [LIFECYLCE] Signal to the cache that a new record has been instantiated on the client
1749
+ * [LIFECYCLE] Signal to the cache that a new record has been instantiated on the client
1750
1750
  *
1751
1751
  * It returns properties from options that should be set on the record during the create
1752
1752
  * process. This return value behavior is deprecated.
@@ -2599,7 +2599,7 @@ class LegacyWrapper {
2599
2599
  if (!isStableIdentifier(type)) {
2600
2600
  // we also deprecate create capability. This behavior was problematic because
2601
2601
  // there's no outside association between this RecordData and an Identifier.
2602
- // It's likely a mistake when we hit this codepath, but we said in an early
2602
+ // It's likely a mistake when we hit this code-path, but we said in an early
2603
2603
  // RFC we'd allow this.
2604
2604
  // With V2 we are enforcing someone to use the record-data and identifier-cache APIs to
2605
2605
  // create a new identifier and then call clientDidCreate on the RecordData
@@ -2717,6 +2717,8 @@ class V2CacheStoreWrapper {
2717
2717
  if (this._store._cbs) {
2718
2718
  this._store._schedule('notify', () => this._flushNotifications());
2719
2719
  } else {
2720
+ // TODO @runspired determine if relationship mutations should schedule
2721
+ // into join/run vs immediate flush
2720
2722
  this._flushNotifications();
2721
2723
  }
2722
2724
  }
@@ -2949,10 +2951,14 @@ class InstanceCache {
2949
2951
  until: '5.0'
2950
2952
  });
2951
2953
  }
2954
+ assert(`Cannot create a record for ${identifier.type + ':' + String(identifier.id)} (${identifier.lid}) as no resource data exists`,
2955
+ // @ts-expect-error managedVersion is private and debug only
2956
+ Boolean(cache.managedVersion === '1' || cache.peek(identifier)));
2952
2957
  record = this.store.instantiateRecord(identifier, properties || {},
2953
2958
  // @ts-expect-error
2954
2959
  this.__cacheFor, this.store.notifications);
2955
2960
  } else {
2961
+ assert(`Cannot create a record for ${identifier.type + ':' + String(identifier.id)} (${identifier.lid}) as no resource data exists`, cache.peek(identifier));
2956
2962
  record = this.store.instantiateRecord(identifier, properties || {});
2957
2963
  }
2958
2964
  setRecordIdentifier(record, identifier);
@@ -4005,7 +4011,7 @@ let IdentifierArray = (_class3 = class IdentifierArray {
4005
4011
  this[IS_COLLECTION] = true;
4006
4012
  this[SOURCE] = void 0;
4007
4013
  // eslint-disable-next-line @typescript-eslint/no-this-alias
4008
- let self = this;
4014
+ const self = this;
4009
4015
  this.modelName = options.type;
4010
4016
  this.store = options.store;
4011
4017
  this._manager = options.manager;
@@ -4050,7 +4056,7 @@ let IdentifierArray = (_class3 = class IdentifierArray {
4050
4056
  fn = function () {
4051
4057
  subscribe(_TAG);
4052
4058
  transaction = true;
4053
- let result = safeForEach(receiver, target, store, arguments[0], arguments[1]);
4059
+ const result = safeForEach(receiver, target, store, arguments[0], arguments[1]);
4054
4060
  transaction = false;
4055
4061
  return result;
4056
4062
  };
@@ -4060,7 +4066,7 @@ let IdentifierArray = (_class3 = class IdentifierArray {
4060
4066
  // array functions must run through Reflect to work properly
4061
4067
  // binding via other means will not work.
4062
4068
  transaction = true;
4063
- let result = Reflect.apply(target[prop], receiver, arguments);
4069
+ const result = Reflect.apply(target[prop], receiver, arguments);
4064
4070
  transaction = false;
4065
4071
  return result;
4066
4072
  };
@@ -4082,10 +4088,7 @@ let IdentifierArray = (_class3 = class IdentifierArray {
4082
4088
  const args = Array.prototype.slice.call(arguments);
4083
4089
  assert(`Cannot start a new array transaction while a previous transaction is underway`, !transaction);
4084
4090
  transaction = true;
4085
- let result = Reflect.apply(target[prop], receiver, args);
4086
- self[MUTATE](prop, args, result);
4087
- addToTransaction(_TAG);
4088
- // TODO handle cache updates
4091
+ const result = self[MUTATE](target, receiver, prop, args, _TAG);
4089
4092
  transaction = false;
4090
4093
  return result;
4091
4094
  };
@@ -4123,13 +4126,11 @@ let IdentifierArray = (_class3 = class IdentifierArray {
4123
4126
  }
4124
4127
  return target[prop];
4125
4128
  },
4126
- set(target, prop, value) {
4129
+ set(target, prop, value, receiver) {
4127
4130
  if (prop === 'length') {
4128
4131
  if (!transaction && value === 0) {
4129
4132
  transaction = true;
4130
- addToTransaction(_TAG);
4131
- Reflect.set(target, prop, value);
4132
- self[MUTATE]('length 0', []);
4133
+ self[MUTATE](target, receiver, 'length 0', [], _TAG);
4133
4134
  transaction = false;
4134
4135
  return true;
4135
4136
  } else if (transaction) {
@@ -4147,8 +4148,21 @@ let IdentifierArray = (_class3 = class IdentifierArray {
4147
4148
  return true;
4148
4149
  }
4149
4150
  let index = convertToInt(prop);
4151
+
4152
+ // we do not allow "holey" arrays and so if the index is
4153
+ // greater than length then we will disallow setting it.
4154
+ // however, there is a special case for "unshift" with more than
4155
+ // one item being inserted since current items will be moved to the
4156
+ // new indices first.
4157
+ // we "loosely" detect this by just checking whether we are in
4158
+ // a transaction.
4150
4159
  if (index === null || index > target.length) {
4151
- if (prop in self) {
4160
+ if (index !== null && transaction) {
4161
+ const identifier = recordIdentifierFor(value);
4162
+ assert(`Cannot set index ${index} past the end of the array.`, isStableIdentifier(identifier));
4163
+ target[index] = identifier;
4164
+ return true;
4165
+ } else if (prop in self) {
4152
4166
  self[prop] = value;
4153
4167
  return true;
4154
4168
  }
@@ -4160,10 +4174,27 @@ let IdentifierArray = (_class3 = class IdentifierArray {
4160
4174
  }
4161
4175
  let original = target[index];
4162
4176
  let newIdentifier = extractIdentifierFromRecord$1(value);
4163
- target[index] = newIdentifier;
4177
+ assert(`Expected a record`, isStableIdentifier(newIdentifier));
4178
+ // We generate "transactions" whenever a setter method on the array
4179
+ // is called and might bulk update multiple array cells. Fundamentally,
4180
+ // all array operations decompose into individual cell replacements.
4181
+ // e.g. a push is really a "replace cell at next index with new value"
4182
+ // or a splice is "shift all values left/right by X and set out of new
4183
+ // bounds cells to undefined"
4184
+ //
4185
+ // so, if we are in a transaction, then this is not a user generated change
4186
+ // but one generated by a setter method. In this case we want to only apply
4187
+ // the change to the target array and not call the MUTATE method.
4188
+ // If there is no transaction though, then this means the user themselves has
4189
+ // directly changed the value of a specific index and we need to thus generate
4190
+ // a mutation for that change.
4191
+ // e.g. "arr.push(newVal)" is handled by a "addToRelatedRecords" mutation within
4192
+ // a transaction.
4193
+ // while "arr[arr.length] = newVal;" is handled by this replace cell code path.
4164
4194
  if (!transaction) {
4165
- self[MUTATE]('replace cell', [index, original, newIdentifier]);
4166
- addToTransaction(_TAG);
4195
+ self[MUTATE](target, receiver, 'replace cell', [index, original, newIdentifier], _TAG);
4196
+ } else {
4197
+ target[index] = newIdentifier;
4167
4198
  }
4168
4199
  return true;
4169
4200
  },
@@ -4406,9 +4437,9 @@ if (macroCondition(getOwnConfig().deprecations.DEPRECATE_ARRAY_LIKE)) {
4406
4437
  let arrIndex = Math.sign(index) === -1 ? this.length + index : index;
4407
4438
  return this[arrIndex];
4408
4439
  };
4409
- IdentifierArray.prototype.objectsAt = function (indeces) {
4440
+ IdentifierArray.prototype.objectsAt = function (indices) {
4410
4441
  deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'objectsAt', 'at');
4411
- return indeces.map(index => this.objectAt(index));
4442
+ return indices.map(index => this.objectAt(index));
4412
4443
  };
4413
4444
  IdentifierArray.prototype.removeAt = function (index) {
4414
4445
  deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'removeAt', 'splice');