@ember-data/store 4.2.0-beta.0 → 4.3.0

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 (30) hide show
  1. package/addon/-private/identifiers/cache.ts +3 -16
  2. package/addon/-private/identifiers/is-stable-identifier.ts +2 -2
  3. package/addon/-private/index.ts +1 -3
  4. package/addon/-private/system/core-store.ts +192 -958
  5. package/addon/-private/system/ds-model-store.ts +20 -111
  6. package/addon/-private/system/fetch-manager.ts +11 -5
  7. package/addon/-private/system/model/internal-model.ts +92 -345
  8. package/addon/-private/system/model/shim-model-class.ts +15 -16
  9. package/addon/-private/system/model/states.js +2 -14
  10. package/addon/-private/system/record-array-manager.js +21 -70
  11. package/addon/-private/system/record-arrays/adapter-populated-record-array.js +1 -20
  12. package/addon/-private/system/record-arrays/record-array.js +1 -8
  13. package/addon/-private/system/record-data-for.ts +10 -8
  14. package/addon/-private/system/record-notification-manager.ts +11 -10
  15. package/addon/-private/system/references/belongs-to.ts +32 -67
  16. package/addon/-private/system/references/has-many.ts +21 -41
  17. package/addon/-private/system/references/record.ts +15 -27
  18. package/addon/-private/system/references/reference.ts +4 -11
  19. package/addon/-private/system/schema-definition-service.ts +2 -2
  20. package/addon/-private/system/snapshot.ts +28 -48
  21. package/addon/-private/system/store/finders.js +2 -96
  22. package/addon/-private/system/store/internal-model-factory.ts +34 -28
  23. package/addon/-private/system/store/record-data-store-wrapper.ts +28 -36
  24. package/addon/-private/system/weak-cache.ts +125 -0
  25. package/addon/-private/ts-interfaces/fetch-manager.ts +4 -0
  26. package/addon/-private/ts-interfaces/identifier.ts +2 -2
  27. package/addon/-private/ts-interfaces/minimum-adapter-interface.ts +0 -1
  28. package/addon/-private/ts-interfaces/schema-definition-service.ts +2 -2
  29. package/package.json +16 -16
  30. package/addon/-private/system/deprecated-evented.js +0 -92
@@ -1,15 +1,13 @@
1
1
  import { getOwner, setOwner } from '@ember/application';
2
- import { assert, deprecate } from '@ember/debug';
2
+ import { assert } from '@ember/debug';
3
3
  import EmberError from '@ember/error';
4
- import { get } from '@ember/object';
5
4
  import { isPresent } from '@ember/utils';
6
5
  import { DEBUG } from '@glimmer/env';
7
6
 
8
- import { CUSTOM_MODEL_CLASS } from '@ember-data/canary-features';
9
7
  import type DSModelClass from '@ember-data/model';
10
8
 
11
9
  import type { DSModel } from '../ts-interfaces/ds-model';
12
- import type { StableRecordIdentifier } from '../ts-interfaces/identifier';
10
+ import type { RecordIdentifier, StableRecordIdentifier } from '../ts-interfaces/identifier';
13
11
  import type { RecordDataRecordWrapper } from '../ts-interfaces/record-data-record-wrapper';
14
12
  import type { RelationshipsSchema } from '../ts-interfaces/record-data-schemas';
15
13
  import type { SchemaDefinitionService } from '../ts-interfaces/schema-definition-service';
@@ -69,7 +67,7 @@ class Store extends CoreStore {
69
67
  // for factorFor factory/class split
70
68
  let klass = maybeFactory && maybeFactory.class ? maybeFactory.class : maybeFactory;
71
69
  if (!klass || !klass.isModel) {
72
- if (!CUSTOM_MODEL_CLASS || !this.getSchemaDefinitionService().doesTypeExist(modelName)) {
70
+ if (!this.getSchemaDefinitionService().doesTypeExist(modelName)) {
73
71
  throw new EmberError(`No model was found for '${modelName}' and no schema handles the type`);
74
72
  }
75
73
  return getShimClass(this, modelName);
@@ -103,83 +101,26 @@ class Store extends CoreStore {
103
101
  typeof modelName === 'string'
104
102
  );
105
103
 
106
- if (CUSTOM_MODEL_CLASS) {
107
- return this.getSchemaDefinitionService().doesTypeExist(modelName);
108
- } else {
109
- assert(`You need to pass a model name to the store's hasModelFor method`, isPresent(modelName));
110
- assert(
111
- `Passing classes to store methods has been removed. Please pass a dasherized string instead of ${modelName}`,
112
- typeof modelName === 'string'
113
- );
114
- let normalizedModelName = normalizeModelName(modelName);
115
- let factory = getModelFactory(this, this._modelFactoryCache, normalizedModelName);
116
-
117
- return factory !== null;
118
- }
104
+ return this.getSchemaDefinitionService().doesTypeExist(modelName);
119
105
  }
120
106
 
121
107
  _relationshipMetaFor(modelName: string, id: string | null, key: string) {
122
- if (CUSTOM_MODEL_CLASS) {
123
- return this._relationshipsDefinitionFor(modelName)[key];
124
- } else {
125
- let modelClass = this.modelFor(modelName);
126
- let relationshipsByName = get(modelClass, 'relationshipsByName');
127
- return relationshipsByName.get(key);
128
- }
108
+ return this._relationshipsDefinitionFor({ type: modelName })[key];
129
109
  }
130
110
 
131
- _attributesDefinitionFor(modelName: string, identifier?: StableRecordIdentifier) {
132
- if (CUSTOM_MODEL_CLASS) {
133
- if (identifier) {
134
- return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
135
- } else {
136
- return this.getSchemaDefinitionService().attributesDefinitionFor(modelName);
137
- }
138
- } else {
139
- let attributes = this._attributesDefCache[modelName];
140
-
141
- if (attributes === undefined) {
142
- let modelClass = this.modelFor(modelName);
143
- let attributeMap = get(modelClass, 'attributes');
144
-
145
- attributes = Object.create(null);
146
- attributeMap.forEach((meta, name) => (attributes[name] = meta));
147
- this._attributesDefCache[modelName] = attributes;
148
- }
149
-
150
- return attributes;
151
- }
111
+ _attributesDefinitionFor(identifier: RecordIdentifier | { type: string }) {
112
+ return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
152
113
  }
153
114
 
154
- _relationshipsDefinitionFor(modelName: string, identifier?: StableRecordIdentifier): RelationshipsSchema {
155
- if (CUSTOM_MODEL_CLASS) {
156
- if (identifier) {
157
- return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
158
- } else {
159
- return this.getSchemaDefinitionService().relationshipsDefinitionFor(modelName);
160
- }
161
- } else {
162
- let relationships = this._relationshipsDefCache[modelName];
163
-
164
- if (relationships === undefined) {
165
- let modelClass = this.modelFor(modelName);
166
- relationships = get(modelClass, 'relationshipsObject') || null;
167
- this._relationshipsDefCache[modelName] = relationships;
168
- }
169
-
170
- return relationships;
171
- }
115
+ _relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
116
+ return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
172
117
  }
173
118
 
174
119
  getSchemaDefinitionService(): SchemaDefinitionService {
175
- if (CUSTOM_MODEL_CLASS) {
176
- if (!this._schemaDefinitionService) {
177
- this._schemaDefinitionService = new DSModelSchemaDefinitionService(this);
178
- }
179
- return this._schemaDefinitionService;
180
- } else {
181
- throw 'schema service is only available when custom model class feature flag is on';
120
+ if (!this._schemaDefinitionService) {
121
+ this._schemaDefinitionService = new DSModelSchemaDefinitionService(this);
182
122
  }
123
+ return this._schemaDefinitionService;
183
124
  }
184
125
  }
185
126
 
@@ -188,48 +129,16 @@ let assertDestroyedStoreOnly: Function;
188
129
 
189
130
  if (DEBUG) {
190
131
  assertDestroyingStore = function assertDestroyedStore(store, method) {
191
- if (!store.shouldAssertMethodCallsOnDestroyedStore) {
192
- deprecate(
193
- `Attempted to call store.${method}(), but the store instance has already been destroyed.`,
194
- !(store.isDestroying || store.isDestroyed),
195
- {
196
- id: 'ember-data:method-calls-on-destroyed-store',
197
- until: '3.8',
198
- for: '@ember-data/store',
199
- since: {
200
- available: '3.8',
201
- enabled: '3.8',
202
- },
203
- }
204
- );
205
- } else {
206
- assert(
207
- `Attempted to call store.${method}(), but the store instance has already been destroyed.`,
208
- !(store.isDestroying || store.isDestroyed)
209
- );
210
- }
132
+ assert(
133
+ `Attempted to call store.${method}(), but the store instance has already been destroyed.`,
134
+ !(store.isDestroying || store.isDestroyed)
135
+ );
211
136
  };
212
137
  assertDestroyedStoreOnly = function assertDestroyedStoreOnly(store, method) {
213
- if (!store.shouldAssertMethodCallsOnDestroyedStore) {
214
- deprecate(
215
- `Attempted to call store.${method}(), but the store instance has already been destroyed.`,
216
- !store.isDestroyed,
217
- {
218
- id: 'ember-data:method-calls-on-destroyed-store',
219
- until: '3.8',
220
- for: '@ember-data/store',
221
- since: {
222
- available: '3.8',
223
- enabled: '3.8',
224
- },
225
- }
226
- );
227
- } else {
228
- assert(
229
- `Attempted to call store.${method}(), but the store instance has already been destroyed.`,
230
- !store.isDestroyed
231
- );
232
- }
138
+ assert(
139
+ `Attempted to call store.${method}(), but the store instance has already been destroyed.`,
140
+ !store.isDestroyed
141
+ );
233
142
  };
234
143
  }
235
144
 
@@ -20,6 +20,7 @@ import type { PrivateSnapshot } from './snapshot';
20
20
  import Snapshot from './snapshot';
21
21
  import { _bind, _guard, _objectIsAlive, guardDestroyedStore } from './store/common';
22
22
  import { normalizeResponseHelper } from './store/serializer-response';
23
+ import WeakCache from './weak-cache';
23
24
 
24
25
  function payloadIsNotBlank(adapterPayload): boolean {
25
26
  if (Array.isArray(adapterPayload)) {
@@ -455,7 +456,7 @@ export default class FetchManager {
455
456
  let identifiers = new Array(totalItems);
456
457
  let seeking: { [id: string]: PendingFetchItem } = Object.create(null);
457
458
 
458
- let optionsMap = new WeakMap<RecordIdentifier, Dict<unknown>>();
459
+ let optionsMap = new WeakCache<RecordIdentifier, Dict<unknown>>(DEBUG ? 'fetch-options' : '');
459
460
 
460
461
  for (let i = 0; i < totalItems; i++) {
461
462
  let pendingItem = pendingFetchItems[i];
@@ -501,10 +502,10 @@ export default class FetchManager {
501
502
  }
502
503
  }
503
504
 
504
- getPendingFetch(identifier: StableRecordIdentifier) {
505
- let pendingRequests = this.requestCache
506
- .getPendingRequestsForRecord(identifier)
507
- .filter((req) => req.type === 'query');
505
+ getPendingFetch(identifier: StableRecordIdentifier, options) {
506
+ let pendingRequests = this.requestCache.getPendingRequestsForRecord(identifier).filter((req) => {
507
+ return req.type === 'query' && isSameRequest(options, req.request.data[0].options);
508
+ });
508
509
 
509
510
  if (pendingRequests.length > 0) {
510
511
  return pendingRequests[0][RequestPromise];
@@ -532,3 +533,8 @@ function assertIsString(id: string | null): asserts id is string {
532
533
  }
533
534
  }
534
535
  }
536
+
537
+ // this function helps resolve whether we have a pending request that we should use instead
538
+ function isSameRequest(options: Dict<unknown> = {}, reqOptions: Dict<unknown> = {}) {
539
+ return options.include === reqOptions.include;
540
+ }