@ember-data/store 4.6.1 → 4.7.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.
- package/addon/-debug/index.js +35 -13
- package/addon/-private/{identifier-cache.ts → caches/identifier-cache.ts} +148 -73
- package/addon/-private/caches/instance-cache.ts +690 -0
- package/addon/-private/{record-data-for.ts → caches/record-data-for.ts} +2 -7
- package/addon/-private/index.ts +44 -24
- package/addon/-private/{model → legacy-model-support}/record-reference.ts +15 -13
- package/addon/-private/{schema-definition-service.ts → legacy-model-support/schema-definition-service.ts} +13 -9
- package/addon/-private/{model → legacy-model-support}/shim-model-class.ts +18 -11
- package/addon/-private/managers/record-array-manager.ts +377 -0
- package/addon/-private/managers/record-data-manager.ts +845 -0
- package/addon/-private/managers/record-data-store-wrapper.ts +421 -0
- package/addon/-private/managers/record-notification-manager.ts +109 -0
- package/addon/-private/network/fetch-manager.ts +567 -0
- package/addon/-private/{finders.js → network/finders.js} +14 -17
- package/addon/-private/{request-cache.ts → network/request-cache.ts} +21 -18
- package/addon/-private/{snapshot-record-array.ts → network/snapshot-record-array.ts} +14 -31
- package/addon/-private/{snapshot.ts → network/snapshot.ts} +40 -49
- package/addon/-private/{promise-proxies.ts → proxies/promise-proxies.ts} +76 -15
- package/addon/-private/{promise-proxy-base.js → proxies/promise-proxy-base.js} +0 -0
- package/addon/-private/record-arrays/identifier-array.ts +924 -0
- package/addon/-private/{core-store.ts → store-service.ts} +574 -215
- package/addon/-private/{coerce-id.ts → utils/coerce-id.ts} +1 -1
- package/addon/-private/{common.js → utils/common.js} +1 -2
- package/addon/-private/utils/construct-resource.ts +2 -2
- package/addon/-private/{identifer-debug-consts.ts → utils/identifer-debug-consts.ts} +0 -0
- package/addon/-private/utils/is-non-empty-string.ts +1 -1
- package/addon/-private/{normalize-model-name.ts → utils/normalize-model-name.ts} +1 -3
- package/addon/-private/utils/promise-record.ts +5 -6
- package/addon/-private/{serializer-response.ts → utils/serializer-response.ts} +2 -2
- package/addon/-private/utils/uuid-polyfill.ts +73 -0
- package/package.json +12 -8
- package/addon/-private/backburner.js +0 -25
- package/addon/-private/errors-utils.js +0 -146
- package/addon/-private/fetch-manager.ts +0 -597
- package/addon/-private/identity-map.ts +0 -54
- package/addon/-private/instance-cache.ts +0 -387
- package/addon/-private/internal-model-factory.ts +0 -359
- package/addon/-private/internal-model-map.ts +0 -121
- package/addon/-private/model/internal-model.ts +0 -602
- package/addon/-private/record-array-manager.ts +0 -444
- package/addon/-private/record-arrays/adapter-populated-record-array.ts +0 -130
- package/addon/-private/record-arrays/record-array.ts +0 -318
- package/addon/-private/record-data-store-wrapper.ts +0 -243
- package/addon/-private/record-notification-manager.ts +0 -73
- package/addon/-private/weak-cache.ts +0 -125
|
@@ -1,597 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @module @ember-data/store
|
|
3
|
-
*/
|
|
4
|
-
import { assert, deprecate, warn } from '@ember/debug';
|
|
5
|
-
import { _backburner as emberBackburner } from '@ember/runloop';
|
|
6
|
-
import { DEBUG } from '@glimmer/env';
|
|
7
|
-
|
|
8
|
-
import { default as RSVP, resolve } from 'rsvp';
|
|
9
|
-
|
|
10
|
-
import { DEPRECATE_RSVP_PROMISE } from '@ember-data/private-build-infra/deprecations';
|
|
11
|
-
import type { CollectionResourceDocument, SingleResourceDocument } from '@ember-data/types/q/ember-data-json-api';
|
|
12
|
-
import type { FindRecordQuery, Request, SaveRecordMutation } from '@ember-data/types/q/fetch-manager';
|
|
13
|
-
import type {
|
|
14
|
-
RecordIdentifier,
|
|
15
|
-
StableExistingRecordIdentifier,
|
|
16
|
-
StableRecordIdentifier,
|
|
17
|
-
} from '@ember-data/types/q/identifier';
|
|
18
|
-
import type { MinimumSerializerInterface } from '@ember-data/types/q/minimum-serializer-interface';
|
|
19
|
-
import type { FindOptions } from '@ember-data/types/q/store';
|
|
20
|
-
import type { Dict } from '@ember-data/types/q/utils';
|
|
21
|
-
|
|
22
|
-
import coerceId from './coerce-id';
|
|
23
|
-
import { _bind, _guard, _objectIsAlive, guardDestroyedStore } from './common';
|
|
24
|
-
import type Store from './core-store';
|
|
25
|
-
import { errorsArrayToHash } from './errors-utils';
|
|
26
|
-
import ShimModelClass from './model/shim-model-class';
|
|
27
|
-
import RequestCache from './request-cache';
|
|
28
|
-
import { normalizeResponseHelper } from './serializer-response';
|
|
29
|
-
import Snapshot from './snapshot';
|
|
30
|
-
import WeakCache from './weak-cache';
|
|
31
|
-
|
|
32
|
-
function payloadIsNotBlank(adapterPayload): boolean {
|
|
33
|
-
if (Array.isArray(adapterPayload)) {
|
|
34
|
-
return true;
|
|
35
|
-
} else {
|
|
36
|
-
return Object.keys(adapterPayload || {}).length !== 0;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
type AdapterErrors = Error & { errors?: string[]; isAdapterError?: true };
|
|
41
|
-
type SerializerWithParseErrors = MinimumSerializerInterface & {
|
|
42
|
-
extractErrors?(store: Store, modelClass: ShimModelClass, error: AdapterErrors, recordId: string | null): any;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export const SaveOp: unique symbol = Symbol('SaveOp');
|
|
46
|
-
|
|
47
|
-
export type FetchMutationOptions = FindOptions & { [SaveOp]: 'createRecord' | 'deleteRecord' | 'updateRecord' };
|
|
48
|
-
|
|
49
|
-
interface PendingFetchItem {
|
|
50
|
-
identifier: StableExistingRecordIdentifier;
|
|
51
|
-
queryRequest: Request;
|
|
52
|
-
resolver: RSVP.Deferred<any>;
|
|
53
|
-
options: FindOptions;
|
|
54
|
-
trace?: any;
|
|
55
|
-
promise: Promise<StableRecordIdentifier>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
interface PendingSaveItem {
|
|
59
|
-
resolver: RSVP.Deferred<any>;
|
|
60
|
-
snapshot: Snapshot;
|
|
61
|
-
identifier: RecordIdentifier;
|
|
62
|
-
options: FetchMutationOptions;
|
|
63
|
-
queryRequest: Request;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Manages the state of network requests initiated by the store
|
|
68
|
-
*
|
|
69
|
-
* @class FetchManager
|
|
70
|
-
* @private
|
|
71
|
-
*/
|
|
72
|
-
export default class FetchManager {
|
|
73
|
-
declare isDestroyed: boolean;
|
|
74
|
-
declare requestCache: RequestCache;
|
|
75
|
-
// saves which are pending in the runloop
|
|
76
|
-
declare _pendingSave: PendingSaveItem[];
|
|
77
|
-
// fetches pending in the runloop, waiting to be coalesced
|
|
78
|
-
declare _pendingFetch: Map<string, PendingFetchItem[]>;
|
|
79
|
-
|
|
80
|
-
constructor(private _store: Store) {
|
|
81
|
-
// used to keep track of all the find requests that need to be coalesced
|
|
82
|
-
this._pendingFetch = new Map();
|
|
83
|
-
this._pendingSave = [];
|
|
84
|
-
this.requestCache = new RequestCache();
|
|
85
|
-
this.isDestroyed = false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
This method is called by `record.save`, and gets passed a
|
|
90
|
-
resolver for the promise that `record.save` returns.
|
|
91
|
-
|
|
92
|
-
It schedules saving to happen at the end of the run loop.
|
|
93
|
-
|
|
94
|
-
@internal
|
|
95
|
-
*/
|
|
96
|
-
scheduleSave(identifier: RecordIdentifier, options: FetchMutationOptions): Promise<null | SingleResourceDocument> {
|
|
97
|
-
let promiseLabel = 'DS: Model#save ' + this;
|
|
98
|
-
let resolver = RSVP.defer<null | SingleResourceDocument>(promiseLabel);
|
|
99
|
-
let query: SaveRecordMutation = {
|
|
100
|
-
op: 'saveRecord',
|
|
101
|
-
recordIdentifier: identifier,
|
|
102
|
-
options,
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
let queryRequest: Request = {
|
|
106
|
-
data: [query],
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
let snapshot = new Snapshot(options, identifier, this._store);
|
|
110
|
-
let pendingSaveItem = {
|
|
111
|
-
snapshot: snapshot,
|
|
112
|
-
resolver: resolver,
|
|
113
|
-
identifier,
|
|
114
|
-
options,
|
|
115
|
-
queryRequest,
|
|
116
|
-
};
|
|
117
|
-
this._pendingSave.push(pendingSaveItem);
|
|
118
|
-
emberBackburner.scheduleOnce('actions', this, this._flushPendingSaves);
|
|
119
|
-
|
|
120
|
-
this.requestCache.enqueue(resolver.promise, pendingSaveItem.queryRequest);
|
|
121
|
-
|
|
122
|
-
return resolver.promise;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
_flushPendingSave(pending: PendingSaveItem) {
|
|
126
|
-
let { snapshot, resolver, identifier, options } = pending;
|
|
127
|
-
let adapter = this._store.adapterFor(identifier.type);
|
|
128
|
-
let operation = options[SaveOp];
|
|
129
|
-
|
|
130
|
-
let internalModel = snapshot._internalModel;
|
|
131
|
-
let modelName = snapshot.modelName;
|
|
132
|
-
let store = this._store;
|
|
133
|
-
let modelClass = store.modelFor(modelName);
|
|
134
|
-
|
|
135
|
-
assert(`You tried to update a record but you have no adapter (for ${modelName})`, adapter);
|
|
136
|
-
assert(
|
|
137
|
-
`You tried to update a record but your adapter (for ${modelName}) does not implement '${operation}'`,
|
|
138
|
-
typeof adapter[operation] === 'function'
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
let promise = resolve().then(() => adapter[operation](store, modelClass, snapshot));
|
|
142
|
-
let serializer: SerializerWithParseErrors | null = store.serializerFor(modelName);
|
|
143
|
-
let label = `DS: Extract and notify about ${operation} completion of ${internalModel}`;
|
|
144
|
-
|
|
145
|
-
assert(
|
|
146
|
-
`Your adapter's '${operation}' method must return a value, but it returned 'undefined'`,
|
|
147
|
-
promise !== undefined
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
promise = _guard(guardDestroyedStore(promise, store, label), _bind(_objectIsAlive, internalModel)).then(
|
|
151
|
-
(adapterPayload) => {
|
|
152
|
-
if (!_objectIsAlive(internalModel)) {
|
|
153
|
-
if (DEPRECATE_RSVP_PROMISE) {
|
|
154
|
-
deprecate(
|
|
155
|
-
`A Promise while saving ${modelName} did not resolve by the time your model was destroyed. This will error in a future release.`,
|
|
156
|
-
false,
|
|
157
|
-
{
|
|
158
|
-
id: 'ember-data:rsvp-unresolved-async',
|
|
159
|
-
until: '5.0',
|
|
160
|
-
for: '@ember-data/store',
|
|
161
|
-
since: {
|
|
162
|
-
available: '4.5',
|
|
163
|
-
enabled: '4.5',
|
|
164
|
-
},
|
|
165
|
-
}
|
|
166
|
-
);
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
if (adapterPayload) {
|
|
171
|
-
return normalizeResponseHelper(serializer, store, modelClass, adapterPayload, snapshot.id, operation);
|
|
172
|
-
}
|
|
173
|
-
},
|
|
174
|
-
function (error) {
|
|
175
|
-
if (error && error.isAdapterError === true && error.code === 'InvalidError') {
|
|
176
|
-
let parsedErrors = error.errors;
|
|
177
|
-
|
|
178
|
-
// TODO deprecate extractErrors being called and/or make it part of the public interface
|
|
179
|
-
if (serializer && typeof serializer.extractErrors === 'function') {
|
|
180
|
-
parsedErrors = serializer.extractErrors(store, modelClass, error, snapshot.id);
|
|
181
|
-
} else {
|
|
182
|
-
parsedErrors = errorsArrayToHash(error.errors);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
throw { error, parsedErrors };
|
|
186
|
-
} else {
|
|
187
|
-
throw { error };
|
|
188
|
-
}
|
|
189
|
-
},
|
|
190
|
-
label
|
|
191
|
-
);
|
|
192
|
-
resolver.resolve(promise);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
This method is called at the end of the run loop, and
|
|
197
|
-
flushes any records passed into `scheduleSave`
|
|
198
|
-
|
|
199
|
-
@method flushPendingSave
|
|
200
|
-
@internal
|
|
201
|
-
*/
|
|
202
|
-
_flushPendingSaves() {
|
|
203
|
-
let pending = this._pendingSave.slice();
|
|
204
|
-
this._pendingSave = [];
|
|
205
|
-
for (let i = 0, j = pending.length; i < j; i++) {
|
|
206
|
-
let pendingItem = pending[i];
|
|
207
|
-
this._flushPendingSave(pendingItem);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
scheduleFetch(identifier: StableExistingRecordIdentifier, options: FindOptions): Promise<StableRecordIdentifier> {
|
|
212
|
-
// TODO Probably the store should pass in the query object
|
|
213
|
-
let shouldTrace = DEBUG && this._store.generateStackTracesForTrackedRequests;
|
|
214
|
-
|
|
215
|
-
let query: FindRecordQuery = {
|
|
216
|
-
op: 'findRecord',
|
|
217
|
-
recordIdentifier: identifier,
|
|
218
|
-
options,
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
let queryRequest: Request = {
|
|
222
|
-
data: [query],
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
let pendingFetch = this.getPendingFetch(identifier, options);
|
|
226
|
-
if (pendingFetch) {
|
|
227
|
-
return pendingFetch;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
let id = identifier.id;
|
|
231
|
-
let modelName = identifier.type;
|
|
232
|
-
|
|
233
|
-
let resolver = RSVP.defer<SingleResourceDocument>(`Fetching ${modelName}' with id: ${id}`);
|
|
234
|
-
let pendingFetchItem: PendingFetchItem = {
|
|
235
|
-
identifier,
|
|
236
|
-
resolver,
|
|
237
|
-
options,
|
|
238
|
-
queryRequest,
|
|
239
|
-
} as PendingFetchItem;
|
|
240
|
-
|
|
241
|
-
if (DEBUG) {
|
|
242
|
-
if (shouldTrace) {
|
|
243
|
-
let trace;
|
|
244
|
-
|
|
245
|
-
try {
|
|
246
|
-
throw new Error(`Trace Origin for scheduled fetch for ${modelName}:${id}.`);
|
|
247
|
-
} catch (e) {
|
|
248
|
-
trace = e;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// enable folks to discover the origin of this findRecord call when
|
|
252
|
-
// debugging. Ideally we would have a tracked queue for requests with
|
|
253
|
-
// labels or local IDs that could be used to merge this trace with
|
|
254
|
-
// the trace made available when we detect an async leak
|
|
255
|
-
pendingFetchItem.trace = trace;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
let resolverPromise = resolver.promise;
|
|
260
|
-
|
|
261
|
-
// TODO replace with some form of record state cache
|
|
262
|
-
const store = this._store;
|
|
263
|
-
const internalModel = store._instanceCache.getInternalModel(identifier);
|
|
264
|
-
const isLoading = !internalModel.isLoaded; // we don't use isLoading directly because we are the request
|
|
265
|
-
|
|
266
|
-
const promise = resolverPromise.then(
|
|
267
|
-
(payload) => {
|
|
268
|
-
// ensure that regardless of id returned we assign to the correct record
|
|
269
|
-
if (payload.data && !Array.isArray(payload.data)) {
|
|
270
|
-
payload.data.lid = identifier.lid;
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
// additional data received in the payload
|
|
274
|
-
// may result in the merging of identifiers (and thus records)
|
|
275
|
-
let potentiallyNewIm = store._push(payload);
|
|
276
|
-
if (potentiallyNewIm && !Array.isArray(potentiallyNewIm)) {
|
|
277
|
-
return potentiallyNewIm;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return identifier;
|
|
281
|
-
},
|
|
282
|
-
(error) => {
|
|
283
|
-
if (internalModel.isEmpty || isLoading) {
|
|
284
|
-
internalModel.unloadRecord();
|
|
285
|
-
}
|
|
286
|
-
throw error;
|
|
287
|
-
}
|
|
288
|
-
);
|
|
289
|
-
|
|
290
|
-
if (this._pendingFetch.size === 0) {
|
|
291
|
-
emberBackburner.schedule('actions', this, this.flushAllPendingFetches);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
let fetches = this._pendingFetch;
|
|
295
|
-
|
|
296
|
-
if (!fetches.has(modelName)) {
|
|
297
|
-
fetches.set(modelName, []);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
(fetches.get(modelName) as PendingFetchItem[]).push(pendingFetchItem);
|
|
301
|
-
|
|
302
|
-
pendingFetchItem.promise = promise;
|
|
303
|
-
this.requestCache.enqueue(resolverPromise, pendingFetchItem.queryRequest);
|
|
304
|
-
return promise;
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
_fetchRecord(fetchItem: PendingFetchItem) {
|
|
308
|
-
let identifier = fetchItem.identifier;
|
|
309
|
-
let modelName = identifier.type;
|
|
310
|
-
let adapter = this._store.adapterFor(modelName);
|
|
311
|
-
|
|
312
|
-
assert(`You tried to find a record but you have no adapter (for ${modelName})`, adapter);
|
|
313
|
-
assert(
|
|
314
|
-
`You tried to find a record but your adapter (for ${modelName}) does not implement 'findRecord'`,
|
|
315
|
-
typeof adapter.findRecord === 'function'
|
|
316
|
-
);
|
|
317
|
-
|
|
318
|
-
let snapshot = new Snapshot(fetchItem.options, identifier, this._store);
|
|
319
|
-
let klass = this._store.modelFor(identifier.type);
|
|
320
|
-
let id = identifier.id;
|
|
321
|
-
let label = `DS: Handle Adapter#findRecord of '${modelName}' with id: '${id}'`;
|
|
322
|
-
|
|
323
|
-
let promise = guardDestroyedStore(
|
|
324
|
-
resolve().then(() => {
|
|
325
|
-
return adapter.findRecord(this._store, klass, identifier.id, snapshot);
|
|
326
|
-
}),
|
|
327
|
-
this._store,
|
|
328
|
-
label
|
|
329
|
-
).then(
|
|
330
|
-
(adapterPayload) => {
|
|
331
|
-
assert(
|
|
332
|
-
`You made a 'findRecord' request for a '${modelName}' with id '${id}', but the adapter's response did not have any data`,
|
|
333
|
-
!!payloadIsNotBlank(adapterPayload)
|
|
334
|
-
);
|
|
335
|
-
let serializer = this._store.serializerFor(modelName);
|
|
336
|
-
let payload = normalizeResponseHelper(serializer, this._store, klass, adapterPayload, id, 'findRecord');
|
|
337
|
-
assert(
|
|
338
|
-
`Ember Data expected the primary data returned from a 'findRecord' response to be an object but instead it found an array.`,
|
|
339
|
-
!Array.isArray(payload.data)
|
|
340
|
-
);
|
|
341
|
-
assert(
|
|
342
|
-
`The 'findRecord' request for ${modelName}:${id} resolved indicating success but contained no primary data. To indicate a 404 not found you should either reject the promise returned by the adapter's findRecord method or throw a NotFoundError.`,
|
|
343
|
-
'data' in payload && payload.data !== null && typeof payload.data === 'object'
|
|
344
|
-
);
|
|
345
|
-
|
|
346
|
-
warn(
|
|
347
|
-
`You requested a record of type '${modelName}' with id '${id}' but the adapter returned a payload with primary data having an id of '${payload.data.id}'. Use 'store.findRecord()' when the requested id is the same as the one returned by the adapter. In other cases use 'store.queryRecord()' instead.`,
|
|
348
|
-
coerceId(payload.data.id) === coerceId(id),
|
|
349
|
-
{
|
|
350
|
-
id: 'ds.store.findRecord.id-mismatch',
|
|
351
|
-
}
|
|
352
|
-
);
|
|
353
|
-
|
|
354
|
-
return payload;
|
|
355
|
-
},
|
|
356
|
-
(error) => {
|
|
357
|
-
throw error;
|
|
358
|
-
},
|
|
359
|
-
`DS: Extract payload of '${modelName}'`
|
|
360
|
-
);
|
|
361
|
-
|
|
362
|
-
fetchItem.resolver.resolve(promise);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
// TODO should probably refactor expectedSnapshots to be identifiers
|
|
366
|
-
handleFoundRecords(
|
|
367
|
-
seeking: { [id: string]: PendingFetchItem },
|
|
368
|
-
coalescedPayload: CollectionResourceDocument,
|
|
369
|
-
expectedSnapshots: Snapshot[]
|
|
370
|
-
) {
|
|
371
|
-
// resolve found records
|
|
372
|
-
let found = Object.create(null);
|
|
373
|
-
let payloads = coalescedPayload.data;
|
|
374
|
-
let coalescedIncluded = coalescedPayload.included || [];
|
|
375
|
-
for (let i = 0, l = payloads.length; i < l; i++) {
|
|
376
|
-
let payload = payloads[i];
|
|
377
|
-
let pair = seeking[payload.id];
|
|
378
|
-
found[payload.id] = payload;
|
|
379
|
-
let included = coalescedIncluded.concat(payloads);
|
|
380
|
-
|
|
381
|
-
// TODO remove original data from included
|
|
382
|
-
if (pair) {
|
|
383
|
-
let resolver = pair.resolver;
|
|
384
|
-
resolver.resolve({ data: payload, included });
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// reject missing records
|
|
389
|
-
|
|
390
|
-
// TODO NOW clean this up to refer to payloads
|
|
391
|
-
let missingSnapshots: Snapshot[] = [];
|
|
392
|
-
|
|
393
|
-
for (let i = 0, l = expectedSnapshots.length; i < l; i++) {
|
|
394
|
-
let snapshot = expectedSnapshots[i];
|
|
395
|
-
assertIsString(snapshot.id);
|
|
396
|
-
|
|
397
|
-
// We know id is a string because you can't fetch
|
|
398
|
-
// without one.
|
|
399
|
-
if (!found[snapshot.id]) {
|
|
400
|
-
missingSnapshots.push(snapshot);
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
if (missingSnapshots.length) {
|
|
405
|
-
warn(
|
|
406
|
-
'Ember Data expected to find records with the following ids in the adapter response but they were missing: [ "' +
|
|
407
|
-
missingSnapshots.map((r) => r.id).join('", "') +
|
|
408
|
-
'" ]',
|
|
409
|
-
false,
|
|
410
|
-
{
|
|
411
|
-
id: 'ds.store.missing-records-from-adapter',
|
|
412
|
-
}
|
|
413
|
-
);
|
|
414
|
-
this.rejectFetchedItems(seeking, missingSnapshots);
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
rejectFetchedItems(seeking: { [id: string]: PendingFetchItem }, snapshots: Snapshot[], error?) {
|
|
419
|
-
for (let i = 0, l = snapshots.length; i < l; i++) {
|
|
420
|
-
let snapshot = snapshots[i];
|
|
421
|
-
assertIsString(snapshot.id);
|
|
422
|
-
// TODO refactor to identifier.lid to avoid this cast to string
|
|
423
|
-
// we can do this case because you can only fetch an identifier
|
|
424
|
-
// that has an ID
|
|
425
|
-
let pair = seeking[snapshot.id];
|
|
426
|
-
|
|
427
|
-
if (pair) {
|
|
428
|
-
pair.resolver.reject(
|
|
429
|
-
error ||
|
|
430
|
-
new Error(
|
|
431
|
-
`Expected: '<${snapshot.modelName}:${snapshot.id}>' to be present in the adapter provided payload, but it was not found.`
|
|
432
|
-
)
|
|
433
|
-
);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
_findMany(
|
|
439
|
-
adapter: any,
|
|
440
|
-
store: Store,
|
|
441
|
-
modelName: string,
|
|
442
|
-
snapshots: Snapshot[],
|
|
443
|
-
identifiers: RecordIdentifier[],
|
|
444
|
-
optionsMap
|
|
445
|
-
) {
|
|
446
|
-
let modelClass = store.modelFor(modelName); // `adapter.findMany` gets the modelClass still
|
|
447
|
-
let ids = snapshots.map((s) => s.id);
|
|
448
|
-
let promise = adapter.findMany(store, modelClass, ids, snapshots);
|
|
449
|
-
let label = `DS: Handle Adapter#findMany of '${modelName}'`;
|
|
450
|
-
|
|
451
|
-
if (promise === undefined) {
|
|
452
|
-
throw new Error('adapter.findMany returned undefined, this was very likely a mistake');
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
promise = guardDestroyedStore(promise, store, label);
|
|
456
|
-
|
|
457
|
-
return promise.then(
|
|
458
|
-
(adapterPayload) => {
|
|
459
|
-
assert(
|
|
460
|
-
`You made a 'findMany' request for '${modelName}' records with ids '[${ids}]', but the adapter's response did not have any data`,
|
|
461
|
-
!!payloadIsNotBlank(adapterPayload)
|
|
462
|
-
);
|
|
463
|
-
let serializer = store.serializerFor(modelName);
|
|
464
|
-
let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findMany');
|
|
465
|
-
return payload;
|
|
466
|
-
},
|
|
467
|
-
null,
|
|
468
|
-
`DS: Extract payload of ${modelName}`
|
|
469
|
-
);
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
_processCoalescedGroup(
|
|
473
|
-
seeking: { [id: string]: PendingFetchItem },
|
|
474
|
-
group: Snapshot[],
|
|
475
|
-
adapter: any,
|
|
476
|
-
optionsMap,
|
|
477
|
-
modelName: string
|
|
478
|
-
) {
|
|
479
|
-
//TODO check what happened with identifiers here
|
|
480
|
-
let totalInGroup = group.length;
|
|
481
|
-
let ids = new Array(totalInGroup);
|
|
482
|
-
let groupedSnapshots = new Array(totalInGroup);
|
|
483
|
-
|
|
484
|
-
for (let j = 0; j < totalInGroup; j++) {
|
|
485
|
-
groupedSnapshots[j] = group[j];
|
|
486
|
-
ids[j] = groupedSnapshots[j].id;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
let store = this._store;
|
|
490
|
-
if (totalInGroup > 1) {
|
|
491
|
-
this._findMany(adapter, store, modelName, group, groupedSnapshots, optionsMap)
|
|
492
|
-
.then((payloads) => {
|
|
493
|
-
this.handleFoundRecords(seeking, payloads, groupedSnapshots);
|
|
494
|
-
})
|
|
495
|
-
.catch((error) => {
|
|
496
|
-
this.rejectFetchedItems(seeking, groupedSnapshots, error);
|
|
497
|
-
});
|
|
498
|
-
} else if (ids.length === 1) {
|
|
499
|
-
let pair = seeking[groupedSnapshots[0].id];
|
|
500
|
-
this._fetchRecord(pair);
|
|
501
|
-
} else {
|
|
502
|
-
assert("You cannot return an empty array from adapter's method groupRecordsForFindMany", false);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
_flushPendingFetchForType(pendingFetchItems: PendingFetchItem[], modelName: string) {
|
|
507
|
-
let adapter = this._store.adapterFor(modelName);
|
|
508
|
-
let shouldCoalesce = !!adapter.findMany && adapter.coalesceFindRequests;
|
|
509
|
-
let totalItems = pendingFetchItems.length;
|
|
510
|
-
let identifiers = new Array(totalItems);
|
|
511
|
-
let seeking: { [id: string]: PendingFetchItem } = Object.create(null);
|
|
512
|
-
|
|
513
|
-
let optionsMap = new WeakCache<RecordIdentifier, FindOptions>(DEBUG ? 'fetch-options' : '');
|
|
514
|
-
|
|
515
|
-
for (let i = 0; i < totalItems; i++) {
|
|
516
|
-
let pendingItem = pendingFetchItems[i];
|
|
517
|
-
let identifier = pendingItem.identifier;
|
|
518
|
-
identifiers[i] = identifier;
|
|
519
|
-
optionsMap.set(identifier, pendingItem.options);
|
|
520
|
-
seeking[identifier.id] = pendingItem;
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
if (shouldCoalesce) {
|
|
524
|
-
// TODO: Improve records => snapshots => records => snapshots
|
|
525
|
-
//
|
|
526
|
-
// We want to provide records to all store methods and snapshots to all
|
|
527
|
-
// adapter methods. To make sure we're doing that we're providing an array
|
|
528
|
-
// of snapshots to adapter.groupRecordsForFindMany(), which in turn will
|
|
529
|
-
// return grouped snapshots instead of grouped records.
|
|
530
|
-
//
|
|
531
|
-
// But since the _findMany() finder is a store method we need to get the
|
|
532
|
-
// records from the grouped snapshots even though the _findMany() finder
|
|
533
|
-
// will once again convert the records to snapshots for adapter.findMany()
|
|
534
|
-
let snapshots = new Array<Snapshot>(totalItems);
|
|
535
|
-
for (let i = 0; i < totalItems; i++) {
|
|
536
|
-
// we know options is in the map due to having just set it above
|
|
537
|
-
// but TS doesn't know so we cast it
|
|
538
|
-
let options = optionsMap.get(identifiers[i]) as Dict<unknown>;
|
|
539
|
-
snapshots[i] = new Snapshot(options, identifiers[i], this._store);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
let groups: Snapshot[][];
|
|
543
|
-
if (adapter.groupRecordsForFindMany) {
|
|
544
|
-
groups = adapter.groupRecordsForFindMany(this._store, snapshots);
|
|
545
|
-
} else {
|
|
546
|
-
groups = [snapshots];
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
for (let i = 0, l = groups.length; i < l; i++) {
|
|
550
|
-
this._processCoalescedGroup(seeking, groups[i], adapter, optionsMap, modelName);
|
|
551
|
-
}
|
|
552
|
-
} else {
|
|
553
|
-
for (let i = 0; i < totalItems; i++) {
|
|
554
|
-
this._fetchRecord(pendingFetchItems[i]);
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
getPendingFetch(identifier: StableRecordIdentifier, options: FindOptions) {
|
|
560
|
-
let pendingFetches = this._pendingFetch.get(identifier.type);
|
|
561
|
-
|
|
562
|
-
// We already have a pending fetch for this
|
|
563
|
-
if (pendingFetches) {
|
|
564
|
-
let matchingPendingFetch = pendingFetches.find((fetch) => fetch.identifier === identifier);
|
|
565
|
-
if (matchingPendingFetch && isSameRequest(options, matchingPendingFetch.options)) {
|
|
566
|
-
return matchingPendingFetch.promise;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
|
|
571
|
-
flushAllPendingFetches() {
|
|
572
|
-
if (this.isDestroyed) {
|
|
573
|
-
return;
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
this._pendingFetch.forEach(this._flushPendingFetchForType, this);
|
|
577
|
-
this._pendingFetch.clear();
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
destroy() {
|
|
581
|
-
this.isDestroyed = true;
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
function assertIsString(id: string | null): asserts id is string {
|
|
586
|
-
if (DEBUG) {
|
|
587
|
-
if (typeof id !== 'string') {
|
|
588
|
-
throw new Error(`Cannot fetch record without an id`);
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
// this function helps resolve whether we have a pending request that we should use instead
|
|
594
|
-
// TODO @runspired @needsTest removing this did not cause any test failures
|
|
595
|
-
function isSameRequest(options: FindOptions = {}, reqOptions: FindOptions = {}) {
|
|
596
|
-
return options.include === reqOptions.include;
|
|
597
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { ConfidentDict } from '@ember-data/types/q/utils';
|
|
2
|
-
|
|
3
|
-
import InternalModelMap from './internal-model-map';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
@module @ember-data/store
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
`IdentityMap` is a custom storage map for records by modelName
|
|
11
|
-
used by `Store`.
|
|
12
|
-
|
|
13
|
-
@class IdentityMap
|
|
14
|
-
@internal
|
|
15
|
-
*/
|
|
16
|
-
export default class IdentityMap {
|
|
17
|
-
private _map: ConfidentDict<InternalModelMap> = Object.create(null);
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
Retrieves the `InternalModelMap` for a given modelName,
|
|
21
|
-
creating one if one did not already exist. This is
|
|
22
|
-
similar to `getWithDefault` or `get` on a `MapWithDefault`
|
|
23
|
-
|
|
24
|
-
@method retrieve
|
|
25
|
-
@internal
|
|
26
|
-
@param modelName a previously normalized modelName
|
|
27
|
-
@return {InternalModelMap} the InternalModelMap for the given modelName
|
|
28
|
-
*/
|
|
29
|
-
retrieve(modelName: string): InternalModelMap {
|
|
30
|
-
let map = this._map[modelName];
|
|
31
|
-
|
|
32
|
-
if (map === undefined) {
|
|
33
|
-
map = this._map[modelName] = new InternalModelMap(modelName);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return map;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
Clears the contents of all known `RecordMaps`, but does
|
|
41
|
-
not remove the InternalModelMap instances.
|
|
42
|
-
|
|
43
|
-
@internal
|
|
44
|
-
*/
|
|
45
|
-
clear(): void {
|
|
46
|
-
let map = this._map;
|
|
47
|
-
let keys = Object.keys(map);
|
|
48
|
-
|
|
49
|
-
for (let i = 0; i < keys.length; i++) {
|
|
50
|
-
let key = keys[i];
|
|
51
|
-
map[key].clear();
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|