@ember-data/store 5.4.0-alpha.1 → 5.4.0-alpha.11

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, h as IDENTIFIER_ARRAY_TAG, I as IdentifierArray, M as MUTATE, I as RecordArray, R as RecordArrayManager, g as SOURCE, S as Store, m as StoreMap, _ as _clearCaches, e as coerceId, j as fastPush, i as isStableIdentifier, n as notifyArray, p as peekCache, r as recordIdentifierFor, k as removeRecordDataFor, o as setCacheFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, l as setRecordIdentifier, s as storeFor } from "./store-service-0a03d2a4";
1
+ export { f as AdapterPopulatedRecordArray, C as CacheHandler, h as IDENTIFIER_ARRAY_TAG, I as IdentifierArray, M as MUTATE, I as RecordArray, R as RecordArrayManager, g as SOURCE, S as Store, m as StoreMap, _ as _clearCaches, e as coerceId, j as fastPush, i as isStableIdentifier, n as notifyArray, p as peekCache, r as recordIdentifierFor, k as removeRecordDataFor, o as setCacheFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, l as setRecordIdentifier, s as storeFor } from "./store-service-fc1e0bd5";
package/addon/index.js CHANGED
@@ -1 +1 @@
1
- export { C as CacheHandler, S as default, r as recordIdentifierFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, s as storeFor } from "./store-service-0a03d2a4";
1
+ export { C as CacheHandler, S as default, r as recordIdentifierFor, c as setIdentifierForgetMethod, a as setIdentifierGenerationMethod, d as setIdentifierResetMethod, b as setIdentifierUpdateMethod, s as storeFor } from "./store-service-fc1e0bd5";
@@ -396,6 +396,10 @@ function ensureStringId(id) {
396
396
  // provided for additional debuggability
397
397
  const DEBUG_CLIENT_ORIGINATED = Symbol('record-originated-on-client');
398
398
  const DEBUG_IDENTIFIER_BUCKET = Symbol('identifier-bucket');
399
+ const DEBUG_STALE_CACHE_OWNER = Symbol('warpDriveStaleCache');
400
+
401
+ // also present in production
402
+ const CACHE_OWNER = Symbol('warpDriveCache');
399
403
  function normalizeModelName(type) {
400
404
  if (macroCondition(getOwnConfig().deprecations.DEPRECATE_NON_STRICT_TYPES)) {
401
405
  const result = dasherize(type);
@@ -476,7 +480,7 @@ function hasType(resource) {
476
480
  const IDENTIFIERS = new Set();
477
481
  const DOCUMENTS = new Set();
478
482
  function isStableIdentifier(identifier) {
479
- return IDENTIFIERS.has(identifier);
483
+ return identifier[CACHE_OWNER] !== undefined || IDENTIFIERS.has(identifier);
480
484
  }
481
485
  function isDocumentIdentifier(identifier) {
482
486
  return DOCUMENTS.has(identifier);
@@ -519,6 +523,7 @@ function setIdentifierResetMethod(method) {
519
523
  // Map<type, Map<id, lid>>
520
524
 
521
525
  const NEW_IDENTIFIERS = new Map();
526
+ let IDENTIFIER_CACHE_ID = 0;
522
527
  function updateTypeIdMapping(typeMap, identifier, id) {
523
528
  let idMap = typeMap.get(identifier.type);
524
529
  if (!idMap) {
@@ -601,6 +606,7 @@ class IdentifierCache {
601
606
  this._merge = defaultMergeMethod;
602
607
  this._keyInfoForResource = defaultKeyInfoMethod;
603
608
  this._isDefaultConfig = !configuredGenerationMethod;
609
+ this._id = IDENTIFIER_CACHE_ID++;
604
610
  this._cache = {
605
611
  resources: new Map(),
606
612
  resourcesByType: Object.create(null),
@@ -658,7 +664,7 @@ class IdentifierCache {
658
664
  console.log(`Identifiers: ${lid ? 'no ' : ''}lid ${lid ? lid + ' ' : ''}determined for resource`, resource);
659
665
  }
660
666
  let identifier = /*#__NOINLINE__*/getIdentifierFromLid(this._cache, lid, resource);
661
- if (identifier !== undefined) {
667
+ if (identifier !== null) {
662
668
  if (macroCondition(getOwnConfig().debug.LOG_IDENTIFIERS)) {
663
669
  // eslint-disable-next-line no-console
664
670
  console.groupEnd();
@@ -676,11 +682,13 @@ class IdentifierCache {
676
682
  // if we still don't have an identifier, time to generate one
677
683
  if (shouldGenerate === 2) {
678
684
  resource.lid = lid;
685
+ resource[CACHE_OWNER] = this._id;
679
686
  identifier = /*#__NOINLINE__*/makeStableRecordIdentifier(resource, 'record', false);
680
687
  } else {
681
688
  // we lie a bit here as a memory optimization
682
689
  const keyInfo = this._keyInfoForResource(resource, null);
683
690
  keyInfo.lid = lid;
691
+ keyInfo[CACHE_OWNER] = this._id;
684
692
  identifier = /*#__NOINLINE__*/makeStableRecordIdentifier(keyInfo, 'record', false);
685
693
  }
686
694
  addResourceToCache(this._cache, identifier);
@@ -767,7 +775,8 @@ class IdentifierCache {
767
775
  let identifier = /*#__NOINLINE__*/makeStableRecordIdentifier({
768
776
  id: data.id || null,
769
777
  type: data.type,
770
- lid: newLid
778
+ lid: newLid,
779
+ [CACHE_OWNER]: this._id
771
780
  }, 'record', true);
772
781
 
773
782
  // populate our unique table
@@ -899,6 +908,10 @@ class IdentifierCache {
899
908
  }
900
909
  this._cache.resources.delete(identifier.lid);
901
910
  typeSet.lid.delete(identifier.lid);
911
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
912
+ identifier[DEBUG_STALE_CACHE_OWNER] = identifier[CACHE_OWNER];
913
+ }
914
+ identifier[CACHE_OWNER] = undefined;
902
915
  IDENTIFIERS.delete(identifier);
903
916
  this._forget(identifier, 'record');
904
917
  if (macroCondition(getOwnConfig().debug.LOG_IDENTIFIERS)) {
@@ -929,15 +942,33 @@ function makeStableRecordIdentifier(recordIdentifier, bucket, clientOriginated)
929
942
  get type() {
930
943
  return recordIdentifier.type;
931
944
  },
932
- toString() {
945
+ get [CACHE_OWNER]() {
946
+ return recordIdentifier[CACHE_OWNER];
947
+ },
948
+ set [CACHE_OWNER](value) {
949
+ recordIdentifier[CACHE_OWNER] = value;
950
+ },
951
+ get [DEBUG_STALE_CACHE_OWNER]() {
952
+ return recordIdentifier[DEBUG_STALE_CACHE_OWNER];
953
+ },
954
+ set [DEBUG_STALE_CACHE_OWNER](value) {
955
+ recordIdentifier[DEBUG_STALE_CACHE_OWNER] = value;
956
+ }
957
+ };
958
+ Object.defineProperty(wrapper, 'toString', {
959
+ enumerable: false,
960
+ value: () => {
933
961
  const {
934
962
  type,
935
963
  id,
936
964
  lid
937
965
  } = recordIdentifier;
938
966
  return `${clientOriginated ? '[CLIENT_ORIGINATED] ' : ''}${String(type)}:${String(id)} (${lid})`;
939
- },
940
- toJSON() {
967
+ }
968
+ });
969
+ Object.defineProperty(wrapper, 'toJSON', {
970
+ enumerable: false,
971
+ value: () => {
941
972
  const {
942
973
  type,
943
974
  id,
@@ -949,7 +980,7 @@ function makeStableRecordIdentifier(recordIdentifier, bucket, clientOriginated)
949
980
  lid
950
981
  };
951
982
  }
952
- };
983
+ });
953
984
  wrapper[DEBUG_CLIENT_ORIGINATED] = clientOriginated;
954
985
  wrapper[DEBUG_IDENTIFIER_BUCKET] = bucket;
955
986
  IDENTIFIERS.add(wrapper);
@@ -1022,7 +1053,7 @@ function detectMerge(cache, keyInfo, identifier, data) {
1022
1053
  // we trigger a merge of the identifiers
1023
1054
  // though probably we should just throw an error here
1024
1055
  if (id !== null && id === newId && newType === type && hasLid(data) && data.lid !== lid) {
1025
- return cache.resources.get(data.lid) || false;
1056
+ return getIdentifierFromLid(cache, data.lid, data) || false;
1026
1057
 
1027
1058
  // If the lids are the same, and ids are the same, but types are different we should trigger a merge of the identifiers
1028
1059
  } else if (id !== null && id === newId && newType && newType !== type && hasLid(data) && data.lid === lid) {
@@ -1039,7 +1070,7 @@ function getIdentifierFromLid(cache, lid, resource) {
1039
1070
  // eslint-disable-next-line no-console
1040
1071
  console.log(`Identifiers: cache ${identifier ? 'HIT' : 'MISS'} - Non-Stable ${lid}`, resource);
1041
1072
  }
1042
- return identifier;
1073
+ return identifier || null;
1043
1074
  }
1044
1075
  function addResourceToCache(cache, identifier) {
1045
1076
  cache.resources.set(identifier.lid, identifier);
@@ -2201,6 +2232,65 @@ class CacheManager {
2201
2232
  // Relationships
2202
2233
  // =============
2203
2234
 
2235
+ /**
2236
+ * Query the cache for the changes to relationships of a resource.
2237
+ *
2238
+ * Returns a map of relationship names to RelationshipDiff objects.
2239
+ *
2240
+ * ```ts
2241
+ * type RelationshipDiff =
2242
+ | {
2243
+ kind: 'collection';
2244
+ remoteState: StableRecordIdentifier[];
2245
+ additions: Set<StableRecordIdentifier>;
2246
+ removals: Set<StableRecordIdentifier>;
2247
+ localState: StableRecordIdentifier[];
2248
+ reordered: boolean;
2249
+ }
2250
+ | {
2251
+ kind: 'resource';
2252
+ remoteState: StableRecordIdentifier | null;
2253
+ localState: StableRecordIdentifier | null;
2254
+ };
2255
+ ```
2256
+ *
2257
+ * @method changedRelationships
2258
+ * @public
2259
+ * @param {StableRecordIdentifier} identifier
2260
+ * @returns {Map<string, RelationshipDiff>}
2261
+ */
2262
+ changedRelationships(identifier) {
2263
+ return _classPrivateFieldBase(this, _cache)[_cache].changedRelationships(identifier);
2264
+ }
2265
+
2266
+ /**
2267
+ * Query the cache for whether any mutated attributes exist
2268
+ *
2269
+ * @method hasChangedRelationships
2270
+ * @public
2271
+ * @param {StableRecordIdentifier} identifier
2272
+ * @returns {boolean}
2273
+ */
2274
+ hasChangedRelationships(identifier) {
2275
+ return _classPrivateFieldBase(this, _cache)[_cache].hasChangedRelationships(identifier);
2276
+ }
2277
+
2278
+ /**
2279
+ * Tell the cache to discard any uncommitted mutations to relationships.
2280
+ *
2281
+ * This will also discard the change on any appropriate inverses.
2282
+ *
2283
+ * This method is a candidate to become a mutation
2284
+ *
2285
+ * @method rollbackRelationships
2286
+ * @public
2287
+ * @param {StableRecordIdentifier} identifier
2288
+ * @returns {string[]} the names of relationships that were restored
2289
+ */
2290
+ rollbackRelationships(identifier) {
2291
+ return _classPrivateFieldBase(this, _cache)[_cache].rollbackRelationships(identifier);
2292
+ }
2293
+
2204
2294
  /**
2205
2295
  * Query the cache for the current state of a relationship property
2206
2296
  *
@@ -2818,7 +2908,7 @@ let IdentifierArray = (_class3 = class IdentifierArray {
2818
2908
  }
2819
2909
  this.isUpdating = true;
2820
2910
  let updatingPromise = this._update();
2821
- updatingPromise.finally(() => {
2911
+ void updatingPromise.finally(() => {
2822
2912
  this._updatingPromise = null;
2823
2913
  if (this.isDestroying || this.isDestroyed) {
2824
2914
  return;
@@ -4031,7 +4121,7 @@ class Store extends EmberObject {
4031
4121
  // to avoid conflicts.
4032
4122
 
4033
4123
  if (properties.id === null || properties.id === undefined) {
4034
- let adapter = this.adapterFor(modelName);
4124
+ let adapter = this.adapterFor(modelName, true);
4035
4125
  if (adapter && adapter.generateIdForRecord) {
4036
4126
  properties.id = adapter.generateIdForRecord(this, modelName, properties);
4037
4127
  } else {
@@ -5298,7 +5388,8 @@ class Store extends EmberObject {
5298
5388
  @param {String} modelName
5299
5389
  @return Adapter
5300
5390
  */
5301
- adapterFor(modelName) {
5391
+
5392
+ adapterFor(modelName, _allowMissing) {
5302
5393
  if (macroCondition(getOwnConfig().env.DEBUG)) {
5303
5394
  assertDestroyingStore(this, 'adapterFor');
5304
5395
  }
@@ -5328,7 +5419,7 @@ class Store extends EmberObject {
5328
5419
  _adapterCache.application = adapter;
5329
5420
  return adapter;
5330
5421
  }
5331
- assert(`No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`);
5422
+ assert(`No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`, _allowMissing);
5332
5423
  }
5333
5424
 
5334
5425
  /**