@warp-drive-mirror/json-api 5.8.0-alpha.9 → 5.8.0-beta.1
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/README.md +16 -29
- package/declarations/-private/cache.d.ts +9 -0
- package/dist/index.js +28 -9
- package/dist/unpkg/dev/index.js +3350 -0
- package/dist/unpkg/dev-deprecated/index.js +3311 -0
- package/dist/unpkg/prod/index.js +1884 -0
- package/dist/unpkg/prod-deprecated/index.js +1884 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +12 -4
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -0,0 +1,1884 @@
|
|
|
1
|
+
import { graphFor, peekGraph, isBelongsTo } from '@warp-drive-mirror/core/graph/-private';
|
|
2
|
+
import { assertPrivateCapabilities, isResourceKey, isRequestKey } from '@warp-drive-mirror/core/store/-private';
|
|
3
|
+
import 'fuse.js';
|
|
4
|
+
import 'json-to-ast';
|
|
5
|
+
|
|
6
|
+
function isMetaDocument(doc) {
|
|
7
|
+
return !(doc instanceof Error) && doc.content && !('data' in doc.content) && !('included' in doc.content) && 'meta' in doc.content;
|
|
8
|
+
}
|
|
9
|
+
function isErrorDocument(doc) {
|
|
10
|
+
return doc instanceof Error;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isImplicit(relationship) {
|
|
14
|
+
return relationship.definition.isImplicit;
|
|
15
|
+
}
|
|
16
|
+
const EMPTY_ITERATOR = {
|
|
17
|
+
iterator() {
|
|
18
|
+
return {
|
|
19
|
+
next() {
|
|
20
|
+
return {
|
|
21
|
+
done: true,
|
|
22
|
+
value: undefined
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
function makeCache() {
|
|
29
|
+
return {
|
|
30
|
+
id: null,
|
|
31
|
+
remoteAttrs: null,
|
|
32
|
+
localAttrs: null,
|
|
33
|
+
defaultAttrs: null,
|
|
34
|
+
inflightAttrs: null,
|
|
35
|
+
changes: null,
|
|
36
|
+
errors: null,
|
|
37
|
+
isNew: false,
|
|
38
|
+
isDeleted: false,
|
|
39
|
+
isDeletionCommitted: false
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* ```ts
|
|
45
|
+
* import { JSONAPICache } from '@warp-drive-mirror/json-api';
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* A {@link Cache} implementation tuned for [{json:api}](https://jsonapi.org/)
|
|
49
|
+
*
|
|
50
|
+
* @categoryDescription Cache Management
|
|
51
|
+
* APIs for primary cache management functionality
|
|
52
|
+
* @categoryDescription Cache Forking
|
|
53
|
+
* APIs that support Cache Forking
|
|
54
|
+
* @categoryDescription SSR Support
|
|
55
|
+
* APIs that support SSR functionality
|
|
56
|
+
* @categoryDescription Resource Lifecycle
|
|
57
|
+
* APIs that support management of resource data
|
|
58
|
+
* @categoryDescription Resource Data
|
|
59
|
+
* APIs that support granular field level management of resource data
|
|
60
|
+
* @categoryDescription Resource State
|
|
61
|
+
* APIs that support managing Resource states
|
|
62
|
+
*
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
class JSONAPICache {
|
|
66
|
+
/**
|
|
67
|
+
* The Cache Version that this implementation implements.
|
|
68
|
+
*
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
/** @internal */
|
|
73
|
+
|
|
74
|
+
/** @internal */
|
|
75
|
+
|
|
76
|
+
/** @internal */
|
|
77
|
+
|
|
78
|
+
/** @internal */
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
|
|
82
|
+
constructor(capabilities) {
|
|
83
|
+
this.version = '2';
|
|
84
|
+
this._capabilities = capabilities;
|
|
85
|
+
this.__cache = new Map();
|
|
86
|
+
this.__graph = graphFor(capabilities);
|
|
87
|
+
this.__destroyedCache = new Map();
|
|
88
|
+
this.__documents = new Map();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
////////// ================ //////////
|
|
92
|
+
////////// Cache Management //////////
|
|
93
|
+
////////// ================ //////////
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Cache the response to a request
|
|
97
|
+
*
|
|
98
|
+
* Implements `Cache.put`.
|
|
99
|
+
*
|
|
100
|
+
* Expects a StructuredDocument whose `content` member is a JsonApiDocument.
|
|
101
|
+
*
|
|
102
|
+
* ```js
|
|
103
|
+
* cache.put({
|
|
104
|
+
* request: { url: 'https://api.example.com/v1/user/1' },
|
|
105
|
+
* content: {
|
|
106
|
+
* data: {
|
|
107
|
+
* type: 'user',
|
|
108
|
+
* id: '1',
|
|
109
|
+
* attributes: {
|
|
110
|
+
* name: 'Chris'
|
|
111
|
+
* }
|
|
112
|
+
* }
|
|
113
|
+
* }
|
|
114
|
+
* })
|
|
115
|
+
* ```
|
|
116
|
+
*
|
|
117
|
+
* > **Note**
|
|
118
|
+
* > The nested `content` and `data` members are not a mistake. This is because
|
|
119
|
+
* > there are two separate concepts involved here, the `StructuredDocument` which contains
|
|
120
|
+
* > the context of a given Request that has been issued with the returned contents as its
|
|
121
|
+
* > `content` property, and a `JSON:API Document` which is the json contents returned by
|
|
122
|
+
* > this endpoint and which uses its `data` property to signify which resources are the
|
|
123
|
+
* > primary resources associated with the request.
|
|
124
|
+
*
|
|
125
|
+
* StructuredDocument's with urls will be cached as full documents with
|
|
126
|
+
* associated resource membership order and contents preserved but linked
|
|
127
|
+
* into the cache.
|
|
128
|
+
*
|
|
129
|
+
* @category Cache Management
|
|
130
|
+
* @public
|
|
131
|
+
*/
|
|
132
|
+
|
|
133
|
+
put(doc) {
|
|
134
|
+
if (isErrorDocument(doc)) {
|
|
135
|
+
return this._putDocument(doc, undefined, undefined);
|
|
136
|
+
} else if (isMetaDocument(doc)) {
|
|
137
|
+
return this._putDocument(doc, undefined, undefined);
|
|
138
|
+
}
|
|
139
|
+
const jsonApiDoc = doc.content;
|
|
140
|
+
const included = jsonApiDoc.included;
|
|
141
|
+
let i, length;
|
|
142
|
+
const {
|
|
143
|
+
cacheKeyManager
|
|
144
|
+
} = this._capabilities;
|
|
145
|
+
if (included) {
|
|
146
|
+
for (i = 0, length = included.length; i < length; i++) {
|
|
147
|
+
included[i] = putOne(this, cacheKeyManager, included[i]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (Array.isArray(jsonApiDoc.data)) {
|
|
151
|
+
length = jsonApiDoc.data.length;
|
|
152
|
+
const identifiers = [];
|
|
153
|
+
for (i = 0; i < length; i++) {
|
|
154
|
+
identifiers.push(putOne(this, cacheKeyManager, jsonApiDoc.data[i]));
|
|
155
|
+
}
|
|
156
|
+
return this._putDocument(doc, identifiers, included);
|
|
157
|
+
}
|
|
158
|
+
if (jsonApiDoc.data === null) {
|
|
159
|
+
return this._putDocument(doc, null, included);
|
|
160
|
+
}
|
|
161
|
+
const identifier = putOne(this, cacheKeyManager, jsonApiDoc.data);
|
|
162
|
+
return this._putDocument(doc, identifier, included);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** @internal */
|
|
166
|
+
|
|
167
|
+
/** @internal */
|
|
168
|
+
|
|
169
|
+
/** @internal */
|
|
170
|
+
|
|
171
|
+
/** @internal */
|
|
172
|
+
|
|
173
|
+
/** @internal */
|
|
174
|
+
_putDocument(doc, data, included) {
|
|
175
|
+
// @ts-expect-error narrowing within is just horrible in TS :/
|
|
176
|
+
const resourceDocument = isErrorDocument(doc) ? fromStructuredError(doc) : fromBaseDocument(doc);
|
|
177
|
+
if (data !== undefined) {
|
|
178
|
+
resourceDocument.data = data;
|
|
179
|
+
}
|
|
180
|
+
if (included !== undefined) {
|
|
181
|
+
resourceDocument.included = included;
|
|
182
|
+
}
|
|
183
|
+
const request = doc.request;
|
|
184
|
+
const identifier = request ? this._capabilities.cacheKeyManager.getOrCreateDocumentIdentifier(request) : null;
|
|
185
|
+
if (identifier) {
|
|
186
|
+
resourceDocument.lid = identifier.lid;
|
|
187
|
+
|
|
188
|
+
// @ts-expect-error
|
|
189
|
+
doc.content = resourceDocument;
|
|
190
|
+
const hasExisting = this.__documents.has(identifier.lid);
|
|
191
|
+
this.__documents.set(identifier.lid, doc);
|
|
192
|
+
this._capabilities.notifyChange(identifier, hasExisting ? 'updated' : 'added', null);
|
|
193
|
+
}
|
|
194
|
+
if (doc.request?.op === 'findHasMany') {
|
|
195
|
+
const parentIdentifier = doc.request.options?.identifier;
|
|
196
|
+
const parentField = doc.request.options?.field;
|
|
197
|
+
if (parentField && parentIdentifier) {
|
|
198
|
+
this.__graph.push({
|
|
199
|
+
op: 'updateRelationship',
|
|
200
|
+
record: parentIdentifier,
|
|
201
|
+
field: parentField.name,
|
|
202
|
+
value: resourceDocument
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
return resourceDocument;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Update the "remote" or "canonical" (persisted) state of the Cache
|
|
211
|
+
* by merging new information into the existing state.
|
|
212
|
+
*
|
|
213
|
+
* @category Cache Management
|
|
214
|
+
* @public
|
|
215
|
+
* @param op the operation or list of operations to perform
|
|
216
|
+
*/
|
|
217
|
+
patch(op) {
|
|
218
|
+
if (Array.isArray(op)) {
|
|
219
|
+
assertPrivateCapabilities(this._capabilities);
|
|
220
|
+
this._capabilities._store._join(() => {
|
|
221
|
+
for (const operation of op) {
|
|
222
|
+
patchCache(this, operation);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
} else {
|
|
226
|
+
patchCache(this, op);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Update the "local" or "current" (unpersisted) state of the Cache
|
|
232
|
+
*
|
|
233
|
+
* @category Cache Management
|
|
234
|
+
* @public
|
|
235
|
+
*/
|
|
236
|
+
mutate(mutation) {
|
|
237
|
+
this.__graph.update(mutation, false);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Peek resource data from the Cache.
|
|
242
|
+
*
|
|
243
|
+
* In development, if the return value
|
|
244
|
+
* is JSON the return value
|
|
245
|
+
* will be deep-cloned and deep-frozen
|
|
246
|
+
* to prevent mutation thereby enforcing cache
|
|
247
|
+
* Immutability.
|
|
248
|
+
*
|
|
249
|
+
* This form of peek is useful for implementations
|
|
250
|
+
* that want to feed raw-data from cache to the UI
|
|
251
|
+
* or which want to interact with a blob of data
|
|
252
|
+
* directly from the presentation cache.
|
|
253
|
+
*
|
|
254
|
+
* An implementation might want to do this because
|
|
255
|
+
* de-referencing records which read from their own
|
|
256
|
+
* blob is generally safer because the record does
|
|
257
|
+
* not require retainining connections to the Store
|
|
258
|
+
* and Cache to present data on a per-field basis.
|
|
259
|
+
*
|
|
260
|
+
* This generally takes the place of `getAttr` as
|
|
261
|
+
* an API and may even take the place of `getRelationship`
|
|
262
|
+
* depending on implementation specifics, though this
|
|
263
|
+
* latter usage is less recommended due to the advantages
|
|
264
|
+
* of the Graph handling necessary entanglements and
|
|
265
|
+
* notifications for relational data.
|
|
266
|
+
*
|
|
267
|
+
* :::warning
|
|
268
|
+
* It is not recommended to use the return value as
|
|
269
|
+
* a serialized representation of the resource both
|
|
270
|
+
* due to it containing local mutations and because
|
|
271
|
+
* it may contain additional fields not recognized
|
|
272
|
+
* by the {json:api} API implementation such as `lid` and
|
|
273
|
+
* the various internal WarpDrive bookkeeping fields.
|
|
274
|
+
* :::
|
|
275
|
+
*
|
|
276
|
+
* @category Cache Management
|
|
277
|
+
* @public
|
|
278
|
+
*/
|
|
279
|
+
|
|
280
|
+
peek(identifier) {
|
|
281
|
+
if (isResourceKey(identifier)) {
|
|
282
|
+
const peeked = this.__safePeek(identifier, false);
|
|
283
|
+
if (!peeked) {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
const {
|
|
287
|
+
type,
|
|
288
|
+
id,
|
|
289
|
+
lid
|
|
290
|
+
} = identifier;
|
|
291
|
+
const attributes = structuredClone(Object.assign({}, peeked.remoteAttrs, peeked.inflightAttrs, peeked.localAttrs));
|
|
292
|
+
const relationships = {};
|
|
293
|
+
const rels = this.__graph.identifiers.get(identifier);
|
|
294
|
+
if (rels) {
|
|
295
|
+
Object.keys(rels).forEach(key => {
|
|
296
|
+
const rel = rels[key];
|
|
297
|
+
if (rel.definition.isImplicit) {
|
|
298
|
+
return;
|
|
299
|
+
} else {
|
|
300
|
+
relationships[key] = this.__graph.getData(identifier, key);
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
assertPrivateCapabilities(this._capabilities);
|
|
305
|
+
const store = this._capabilities._store;
|
|
306
|
+
const attrs = getCacheFields(this, identifier);
|
|
307
|
+
attrs.forEach((attr, key) => {
|
|
308
|
+
if (key in attributes && attributes[key] !== undefined) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
const defaultValue = getDefaultValue(attr, identifier, store);
|
|
312
|
+
if (defaultValue !== undefined) {
|
|
313
|
+
attributes[key] = defaultValue;
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
return {
|
|
317
|
+
type,
|
|
318
|
+
id,
|
|
319
|
+
lid,
|
|
320
|
+
attributes,
|
|
321
|
+
relationships
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
const document = this.peekRequest(identifier);
|
|
325
|
+
if (document) {
|
|
326
|
+
if ('content' in document) return document.content;
|
|
327
|
+
}
|
|
328
|
+
return null;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Peek the remote resource data from the Cache.
|
|
333
|
+
*
|
|
334
|
+
* @category Cache Management
|
|
335
|
+
* @public
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
peekRemoteState(identifier) {
|
|
339
|
+
if (isResourceKey(identifier)) {
|
|
340
|
+
const peeked = this.__safePeek(identifier, false);
|
|
341
|
+
if (!peeked) {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
const {
|
|
345
|
+
type,
|
|
346
|
+
id,
|
|
347
|
+
lid
|
|
348
|
+
} = identifier;
|
|
349
|
+
const attributes = structuredClone(peeked.remoteAttrs);
|
|
350
|
+
const relationships = {};
|
|
351
|
+
const rels = this.__graph.identifiers.get(identifier);
|
|
352
|
+
if (rels) {
|
|
353
|
+
Object.keys(rels).forEach(key => {
|
|
354
|
+
const rel = rels[key];
|
|
355
|
+
if (rel.definition.isImplicit) {
|
|
356
|
+
return;
|
|
357
|
+
} else {
|
|
358
|
+
relationships[key] = structuredClone(this.__graph.getData(identifier, key));
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
assertPrivateCapabilities(this._capabilities);
|
|
363
|
+
const store = this._capabilities._store;
|
|
364
|
+
const attrs = getCacheFields(this, identifier);
|
|
365
|
+
attrs.forEach((attr, key) => {
|
|
366
|
+
if (key in attributes && attributes[key] !== undefined) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const defaultValue = getDefaultValue(attr, identifier, store);
|
|
370
|
+
if (defaultValue !== undefined) {
|
|
371
|
+
attributes[key] = defaultValue;
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
return {
|
|
375
|
+
type,
|
|
376
|
+
id,
|
|
377
|
+
lid,
|
|
378
|
+
attributes,
|
|
379
|
+
relationships
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
const document = this.peekRequest(identifier);
|
|
383
|
+
if (document) {
|
|
384
|
+
if ('content' in document) return document.content;
|
|
385
|
+
}
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Peek the Cache for the existing request data associated with
|
|
391
|
+
* a cacheable request.
|
|
392
|
+
*
|
|
393
|
+
* This is effectively the reverse of `put` for a request in
|
|
394
|
+
* that it will return the the request, response, and content
|
|
395
|
+
* whereas `peek` will return just the `content`.
|
|
396
|
+
*
|
|
397
|
+
* @category Cache Management
|
|
398
|
+
* @public
|
|
399
|
+
*/
|
|
400
|
+
peekRequest(identifier) {
|
|
401
|
+
return this.__documents.get(identifier.lid) || null;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Push resource data from a remote source into the cache for this identifier
|
|
406
|
+
*
|
|
407
|
+
* @category Cache Management
|
|
408
|
+
* @public
|
|
409
|
+
* @return if `calculateChanges` is true then calculated key changes should be returned
|
|
410
|
+
*/
|
|
411
|
+
upsert(identifier, data, calculateChanges) {
|
|
412
|
+
assertPrivateCapabilities(this._capabilities);
|
|
413
|
+
const store = this._capabilities._store;
|
|
414
|
+
if (!store._cbs) {
|
|
415
|
+
let result = undefined;
|
|
416
|
+
store._run(() => {
|
|
417
|
+
result = cacheUpsert(this, identifier, data, calculateChanges);
|
|
418
|
+
});
|
|
419
|
+
return result;
|
|
420
|
+
}
|
|
421
|
+
return cacheUpsert(this, identifier, data, calculateChanges);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
////////// ============= //////////
|
|
425
|
+
////////// Cache Forking //////////
|
|
426
|
+
////////// ============= //////////
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Create a fork of the cache from the current state.
|
|
430
|
+
*
|
|
431
|
+
* Applications should typically not call this method themselves,
|
|
432
|
+
* preferring instead to fork at the Store level, which will
|
|
433
|
+
* utilize this method to fork the cache.
|
|
434
|
+
*
|
|
435
|
+
* @category Cache Forking
|
|
436
|
+
* @private
|
|
437
|
+
*/
|
|
438
|
+
fork() {
|
|
439
|
+
throw new Error(`Not Implemented`);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Merge a fork back into a parent Cache.
|
|
444
|
+
*
|
|
445
|
+
* Applications should typically not call this method themselves,
|
|
446
|
+
* preferring instead to merge at the Store level, which will
|
|
447
|
+
* utilize this method to merge the caches.
|
|
448
|
+
*
|
|
449
|
+
* @category Cache Forking
|
|
450
|
+
* @private
|
|
451
|
+
*/
|
|
452
|
+
merge(_cache) {
|
|
453
|
+
throw new Error(`Not Implemented`);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Generate the list of changes applied to all
|
|
458
|
+
* record in the store.
|
|
459
|
+
*
|
|
460
|
+
* Each individual resource or document that has
|
|
461
|
+
* been mutated should be described as an individual
|
|
462
|
+
* `Change` entry in the returned array.
|
|
463
|
+
*
|
|
464
|
+
* A `Change` is described by an object containing up to
|
|
465
|
+
* three properties: (1) the `identifier` of the entity that
|
|
466
|
+
* changed; (2) the `op` code of that change being one of
|
|
467
|
+
* `upsert` or `remove`, and if the op is `upsert` a `patch`
|
|
468
|
+
* containing the data to merge into the cache for the given
|
|
469
|
+
* entity.
|
|
470
|
+
*
|
|
471
|
+
* This `patch` is opaque to the Store but should be understood
|
|
472
|
+
* by the Cache and may expect to be utilized by an Adapter
|
|
473
|
+
* when generating data during a `save` operation.
|
|
474
|
+
*
|
|
475
|
+
* It is generally recommended that the `patch` contain only
|
|
476
|
+
* the updated state, ignoring fields that are unchanged
|
|
477
|
+
*
|
|
478
|
+
* ```ts
|
|
479
|
+
* interface Change {
|
|
480
|
+
* identifier: ResourceKey | RequestKey;
|
|
481
|
+
* op: 'upsert' | 'remove';
|
|
482
|
+
* patch?: unknown;
|
|
483
|
+
* }
|
|
484
|
+
* ```
|
|
485
|
+
*
|
|
486
|
+
* @category Cache Forking
|
|
487
|
+
* @private
|
|
488
|
+
*/
|
|
489
|
+
diff() {
|
|
490
|
+
throw new Error(`Not Implemented`);
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
////////// =========== //////////
|
|
494
|
+
////////// SSR Support //////////
|
|
495
|
+
////////// =========== //////////
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* Serialize the entire contents of the Cache into a Stream
|
|
499
|
+
* which may be fed back into a new instance of the same Cache
|
|
500
|
+
* via `cache.hydrate`.
|
|
501
|
+
*
|
|
502
|
+
* @category SSR Support
|
|
503
|
+
* @private
|
|
504
|
+
*/
|
|
505
|
+
dump() {
|
|
506
|
+
throw new Error(`Not Implemented`);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* hydrate a Cache from a Stream with content previously serialized
|
|
511
|
+
* from another instance of the same Cache, resolving when hydration
|
|
512
|
+
* is complete.
|
|
513
|
+
*
|
|
514
|
+
* This method should expect to be called both in the context of restoring
|
|
515
|
+
* the Cache during application rehydration after SSR **AND** at unknown
|
|
516
|
+
* times during the lifetime of an already booted application when it is
|
|
517
|
+
* desired to bulk-load additional information into the cache. This latter
|
|
518
|
+
* behavior supports optimizing pre/fetching of data for route transitions
|
|
519
|
+
* via data-only SSR modes.
|
|
520
|
+
*
|
|
521
|
+
* @category SSR Support
|
|
522
|
+
* @private
|
|
523
|
+
*/
|
|
524
|
+
hydrate(stream) {
|
|
525
|
+
throw new Error('Not Implemented');
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
////////// ================== //////////
|
|
529
|
+
////////// Resource Lifecycle //////////
|
|
530
|
+
////////// ================== //////////
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* [LIFECYCLE] Signal to the cache that a new record has been instantiated on the client
|
|
534
|
+
*
|
|
535
|
+
* It returns properties from options that should be set on the record during the create
|
|
536
|
+
* process. This return value behavior is deprecated.
|
|
537
|
+
*
|
|
538
|
+
* @category Resource Lifecycle
|
|
539
|
+
* @public
|
|
540
|
+
*/
|
|
541
|
+
clientDidCreate(identifier, options) {
|
|
542
|
+
const cached = this._createCache(identifier);
|
|
543
|
+
cached.isNew = true;
|
|
544
|
+
const createOptions = {};
|
|
545
|
+
if (options !== undefined) {
|
|
546
|
+
const fields = getCacheFields(this, identifier);
|
|
547
|
+
const graph = this.__graph;
|
|
548
|
+
const propertyNames = Object.keys(options);
|
|
549
|
+
for (let i = 0; i < propertyNames.length; i++) {
|
|
550
|
+
const name = propertyNames[i];
|
|
551
|
+
const propertyValue = options[name];
|
|
552
|
+
if (name === 'id') {
|
|
553
|
+
continue;
|
|
554
|
+
}
|
|
555
|
+
const fieldType = fields.get(name);
|
|
556
|
+
const kind = fieldType !== undefined ? 'kind' in fieldType ? fieldType.kind : 'attribute' : null;
|
|
557
|
+
let relationship;
|
|
558
|
+
switch (kind) {
|
|
559
|
+
case 'attribute':
|
|
560
|
+
this.setAttr(identifier, name, propertyValue);
|
|
561
|
+
createOptions[name] = propertyValue;
|
|
562
|
+
break;
|
|
563
|
+
case 'belongsTo':
|
|
564
|
+
this.mutate({
|
|
565
|
+
op: 'replaceRelatedRecord',
|
|
566
|
+
field: name,
|
|
567
|
+
record: identifier,
|
|
568
|
+
value: propertyValue
|
|
569
|
+
});
|
|
570
|
+
relationship = graph.get(identifier, name);
|
|
571
|
+
relationship.state.hasReceivedData = true;
|
|
572
|
+
relationship.state.isEmpty = false;
|
|
573
|
+
break;
|
|
574
|
+
case 'hasMany':
|
|
575
|
+
this.mutate({
|
|
576
|
+
op: 'replaceRelatedRecords',
|
|
577
|
+
field: name,
|
|
578
|
+
record: identifier,
|
|
579
|
+
value: propertyValue
|
|
580
|
+
});
|
|
581
|
+
relationship = graph.get(identifier, name);
|
|
582
|
+
relationship.state.hasReceivedData = true;
|
|
583
|
+
relationship.state.isEmpty = false;
|
|
584
|
+
break;
|
|
585
|
+
default:
|
|
586
|
+
// reflect back (pass-thru) unknown properties
|
|
587
|
+
createOptions[name] = propertyValue;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
this._capabilities.notifyChange(identifier, 'added', null);
|
|
592
|
+
return createOptions;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
/**
|
|
596
|
+
* [LIFECYCLE] Signals to the cache that a resource
|
|
597
|
+
* will be part of a save transaction.
|
|
598
|
+
*
|
|
599
|
+
* @category Resource Lifecycle
|
|
600
|
+
* @public
|
|
601
|
+
*/
|
|
602
|
+
willCommit(identifier, _context) {
|
|
603
|
+
if (Array.isArray(identifier)) {
|
|
604
|
+
for (const key of identifier) {
|
|
605
|
+
willCommit(this, key);
|
|
606
|
+
}
|
|
607
|
+
} else {
|
|
608
|
+
willCommit(this, identifier);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* [LIFECYCLE] Signals to the cache that a resource
|
|
614
|
+
* was successfully updated as part of a save transaction.
|
|
615
|
+
*
|
|
616
|
+
* @category Resource Lifecycle
|
|
617
|
+
* @public
|
|
618
|
+
*/
|
|
619
|
+
|
|
620
|
+
didCommit(committedIdentifier, result) {
|
|
621
|
+
const payload = result ? result.content : null;
|
|
622
|
+
const operation = result?.request?.op ?? null;
|
|
623
|
+
const data = payload && payload.data;
|
|
624
|
+
const responseIsCollection = Array.isArray(data);
|
|
625
|
+
const hasMultipleIdentifiers = Array.isArray(committedIdentifier) && committedIdentifier.length > 1;
|
|
626
|
+
if (Array.isArray(committedIdentifier)) {
|
|
627
|
+
if (responseIsCollection) {
|
|
628
|
+
for (let i = 0; i < committedIdentifier.length; i++) {
|
|
629
|
+
const identifier = committedIdentifier[i];
|
|
630
|
+
didCommit(this, identifier, data[i] ?? null, operation);
|
|
631
|
+
}
|
|
632
|
+
// but if we get back no data or a single entry, we apply
|
|
633
|
+
// the change back to the original identifier
|
|
634
|
+
} else {
|
|
635
|
+
for (let i = 0; i < committedIdentifier.length; i++) {
|
|
636
|
+
const identifier = committedIdentifier[i];
|
|
637
|
+
didCommit(this, identifier, i === 0 ? data : null, operation);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
} else {
|
|
641
|
+
didCommit(this, committedIdentifier, data, operation);
|
|
642
|
+
}
|
|
643
|
+
const included = payload && payload.included;
|
|
644
|
+
const {
|
|
645
|
+
cacheKeyManager
|
|
646
|
+
} = this._capabilities;
|
|
647
|
+
if (included) {
|
|
648
|
+
for (let i = 0, length = included.length; i < length; i++) {
|
|
649
|
+
putOne(this, cacheKeyManager, included[i]);
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
return hasMultipleIdentifiers && responseIsCollection ? {
|
|
653
|
+
data: committedIdentifier
|
|
654
|
+
} : {
|
|
655
|
+
data: Array.isArray(committedIdentifier) ? committedIdentifier[0] : committedIdentifier
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* [LIFECYCLE] Signals to the cache that a resource
|
|
661
|
+
* was update via a save transaction failed.
|
|
662
|
+
*
|
|
663
|
+
* @category Resource Lifecycle
|
|
664
|
+
* @public
|
|
665
|
+
*/
|
|
666
|
+
commitWasRejected(identifier, errors) {
|
|
667
|
+
if (Array.isArray(identifier)) {
|
|
668
|
+
for (let i = 0; i < identifier.length; i++) {
|
|
669
|
+
commitDidError(this, identifier[i], errors && i === 0 ? errors : null);
|
|
670
|
+
}
|
|
671
|
+
return;
|
|
672
|
+
}
|
|
673
|
+
return commitDidError(this, identifier, errors || null);
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/**
|
|
677
|
+
* [LIFECYCLE] Signals to the cache that all data for a resource
|
|
678
|
+
* should be cleared.
|
|
679
|
+
*
|
|
680
|
+
* This method is a candidate to become a mutation
|
|
681
|
+
*
|
|
682
|
+
* @category Resource Lifecycle
|
|
683
|
+
* @public
|
|
684
|
+
*/
|
|
685
|
+
unloadRecord(identifier) {
|
|
686
|
+
const storeWrapper = this._capabilities;
|
|
687
|
+
// TODO this is necessary because
|
|
688
|
+
// we maintain memebership inside InstanceCache
|
|
689
|
+
// for peekAll, so even though we haven't created
|
|
690
|
+
// any data we think this exists.
|
|
691
|
+
// TODO can we eliminate that membership now?
|
|
692
|
+
if (!this.__cache.has(identifier)) {
|
|
693
|
+
// the graph may still need to unload identity
|
|
694
|
+
peekGraph(storeWrapper)?.unload(identifier);
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
const removeFromRecordArray = !this.isDeletionCommitted(identifier);
|
|
698
|
+
let removed = false;
|
|
699
|
+
const cached = this.__peek(identifier, false);
|
|
700
|
+
if (cached.isNew || cached.isDeletionCommitted) {
|
|
701
|
+
peekGraph(storeWrapper)?.push({
|
|
702
|
+
op: 'deleteRecord',
|
|
703
|
+
record: identifier,
|
|
704
|
+
isNew: cached.isNew
|
|
705
|
+
});
|
|
706
|
+
} else {
|
|
707
|
+
peekGraph(storeWrapper)?.unload(identifier);
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
// effectively clearing these is ensuring that
|
|
711
|
+
// we report as `isEmpty` during teardown.
|
|
712
|
+
cached.localAttrs = null;
|
|
713
|
+
cached.remoteAttrs = null;
|
|
714
|
+
cached.defaultAttrs = null;
|
|
715
|
+
cached.inflightAttrs = null;
|
|
716
|
+
const relatedIdentifiers = _allRelatedIdentifiers(storeWrapper, identifier);
|
|
717
|
+
if (areAllModelsUnloaded(storeWrapper, relatedIdentifiers)) {
|
|
718
|
+
for (let i = 0; i < relatedIdentifiers.length; ++i) {
|
|
719
|
+
const relatedIdentifier = relatedIdentifiers[i];
|
|
720
|
+
storeWrapper.notifyChange(relatedIdentifier, 'removed', null);
|
|
721
|
+
removed = true;
|
|
722
|
+
storeWrapper.disconnectRecord(relatedIdentifier);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
this.__cache.delete(identifier);
|
|
726
|
+
this.__destroyedCache.set(identifier, cached);
|
|
727
|
+
|
|
728
|
+
/*
|
|
729
|
+
* The destroy cache is a hack to prevent applications
|
|
730
|
+
* from blowing up during teardown. Accessing state
|
|
731
|
+
* on a destroyed record is not safe, but historically
|
|
732
|
+
* was possible due to a combination of teardown timing
|
|
733
|
+
* and retention of cached state directly on the
|
|
734
|
+
* record itself.
|
|
735
|
+
*
|
|
736
|
+
* Once we have deprecated accessing state on a destroyed
|
|
737
|
+
* instance we may remove this. The timing isn't a huge deal
|
|
738
|
+
* as momentarily retaining the objects outside the bounds
|
|
739
|
+
* of a test won't cause issues.
|
|
740
|
+
*/
|
|
741
|
+
if (this.__destroyedCache.size === 1) {
|
|
742
|
+
// TODO do we still need this?
|
|
743
|
+
setTimeout(() => {
|
|
744
|
+
this.__destroyedCache.clear();
|
|
745
|
+
}, 100);
|
|
746
|
+
}
|
|
747
|
+
if (!removed && removeFromRecordArray) {
|
|
748
|
+
storeWrapper.notifyChange(identifier, 'removed', null);
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
////////// ============= //////////
|
|
753
|
+
////////// Resource Data //////////
|
|
754
|
+
////////// ============= //////////
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* Retrieve the data for an attribute from the cache
|
|
758
|
+
* with local mutations applied.
|
|
759
|
+
*
|
|
760
|
+
* @category Resource Data
|
|
761
|
+
* @public
|
|
762
|
+
*/
|
|
763
|
+
getAttr(identifier, attr) {
|
|
764
|
+
const isSimplePath = !Array.isArray(attr) || attr.length === 1;
|
|
765
|
+
if (Array.isArray(attr) && attr.length === 1) {
|
|
766
|
+
attr = attr[0];
|
|
767
|
+
}
|
|
768
|
+
if (isSimplePath) {
|
|
769
|
+
const attribute = attr;
|
|
770
|
+
const cached = this.__peek(identifier, true);
|
|
771
|
+
|
|
772
|
+
// in Prod we try to recover when accessing something that
|
|
773
|
+
// doesn't exist
|
|
774
|
+
if (!cached) {
|
|
775
|
+
return undefined;
|
|
776
|
+
}
|
|
777
|
+
if (cached.localAttrs && attribute in cached.localAttrs) {
|
|
778
|
+
return cached.localAttrs[attribute];
|
|
779
|
+
} else if (cached.inflightAttrs && attribute in cached.inflightAttrs) {
|
|
780
|
+
return cached.inflightAttrs[attribute];
|
|
781
|
+
} else if (cached.remoteAttrs && attribute in cached.remoteAttrs) {
|
|
782
|
+
return cached.remoteAttrs[attribute];
|
|
783
|
+
} else if (cached.defaultAttrs && attribute in cached.defaultAttrs) {
|
|
784
|
+
return cached.defaultAttrs[attribute];
|
|
785
|
+
} else {
|
|
786
|
+
const attrSchema = getCacheFields(this, identifier).get(attribute);
|
|
787
|
+
assertPrivateCapabilities(this._capabilities);
|
|
788
|
+
const defaultValue = getDefaultValue(attrSchema, identifier, this._capabilities._store);
|
|
789
|
+
if (schemaHasLegacyDefaultValueFn(attrSchema)) {
|
|
790
|
+
cached.defaultAttrs = cached.defaultAttrs || Object.create(null);
|
|
791
|
+
cached.defaultAttrs[attribute] = defaultValue;
|
|
792
|
+
}
|
|
793
|
+
return defaultValue;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
// TODO @runspired consider whether we need a defaultValue cache in ReactiveResource
|
|
798
|
+
// like we do for the simple case above.
|
|
799
|
+
const path = attr;
|
|
800
|
+
const cached = this.__peek(identifier, true);
|
|
801
|
+
const basePath = path[0];
|
|
802
|
+
let current = cached.localAttrs && basePath in cached.localAttrs ? cached.localAttrs[basePath] : undefined;
|
|
803
|
+
if (current === undefined) {
|
|
804
|
+
current = cached.inflightAttrs && basePath in cached.inflightAttrs ? cached.inflightAttrs[basePath] : undefined;
|
|
805
|
+
}
|
|
806
|
+
if (current === undefined) {
|
|
807
|
+
current = cached.remoteAttrs && basePath in cached.remoteAttrs ? cached.remoteAttrs[basePath] : undefined;
|
|
808
|
+
}
|
|
809
|
+
if (current === undefined) {
|
|
810
|
+
return undefined;
|
|
811
|
+
}
|
|
812
|
+
for (let i = 1; i < path.length; i++) {
|
|
813
|
+
current = current[path[i]];
|
|
814
|
+
if (current === undefined) {
|
|
815
|
+
return undefined;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
return current;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* Retrieve the remote data for an attribute from the cache
|
|
823
|
+
*
|
|
824
|
+
* @category Resource Data
|
|
825
|
+
* @public
|
|
826
|
+
*/
|
|
827
|
+
getRemoteAttr(identifier, attr) {
|
|
828
|
+
const isSimplePath = !Array.isArray(attr) || attr.length === 1;
|
|
829
|
+
if (Array.isArray(attr) && attr.length === 1) {
|
|
830
|
+
attr = attr[0];
|
|
831
|
+
}
|
|
832
|
+
if (isSimplePath) {
|
|
833
|
+
const attribute = attr;
|
|
834
|
+
const cached = this.__peek(identifier, true);
|
|
835
|
+
|
|
836
|
+
// in Prod we try to recover when accessing something that
|
|
837
|
+
// doesn't exist
|
|
838
|
+
if (!cached) {
|
|
839
|
+
return undefined;
|
|
840
|
+
}
|
|
841
|
+
if (cached.remoteAttrs && attribute in cached.remoteAttrs) {
|
|
842
|
+
return cached.remoteAttrs[attribute];
|
|
843
|
+
|
|
844
|
+
// we still show defaultValues in the case of a remoteAttr access
|
|
845
|
+
} else if (cached.defaultAttrs && attribute in cached.defaultAttrs) {
|
|
846
|
+
return cached.defaultAttrs[attribute];
|
|
847
|
+
} else {
|
|
848
|
+
const attrSchema = getCacheFields(this, identifier).get(attribute);
|
|
849
|
+
assertPrivateCapabilities(this._capabilities);
|
|
850
|
+
const defaultValue = getDefaultValue(attrSchema, identifier, this._capabilities._store);
|
|
851
|
+
if (schemaHasLegacyDefaultValueFn(attrSchema)) {
|
|
852
|
+
cached.defaultAttrs = cached.defaultAttrs || Object.create(null);
|
|
853
|
+
cached.defaultAttrs[attribute] = defaultValue;
|
|
854
|
+
}
|
|
855
|
+
return defaultValue;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
// TODO @runspired consider whether we need a defaultValue cache in ReactiveResource
|
|
860
|
+
// like we do for the simple case above.
|
|
861
|
+
const path = attr;
|
|
862
|
+
const cached = this.__peek(identifier, true);
|
|
863
|
+
const basePath = path[0];
|
|
864
|
+
let current = cached.remoteAttrs && basePath in cached.remoteAttrs ? cached.remoteAttrs[basePath] : undefined;
|
|
865
|
+
if (current === undefined) {
|
|
866
|
+
return undefined;
|
|
867
|
+
}
|
|
868
|
+
for (let i = 1; i < path.length; i++) {
|
|
869
|
+
current = current[path[i]];
|
|
870
|
+
if (current === undefined) {
|
|
871
|
+
return undefined;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return current;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
/**
|
|
878
|
+
* Mutate the data for an attribute in the cache
|
|
879
|
+
*
|
|
880
|
+
* This method is a candidate to become a mutation
|
|
881
|
+
*
|
|
882
|
+
* @category Resource Data
|
|
883
|
+
* @public
|
|
884
|
+
*/
|
|
885
|
+
setAttr(identifier, attr, value) {
|
|
886
|
+
const isSimplePath = !Array.isArray(attr) || attr.length === 1;
|
|
887
|
+
if (Array.isArray(attr) && attr.length === 1) {
|
|
888
|
+
attr = attr[0];
|
|
889
|
+
}
|
|
890
|
+
if (isSimplePath) {
|
|
891
|
+
const cached = this.__peek(identifier, false);
|
|
892
|
+
const currentAttr = attr;
|
|
893
|
+
const existing = cached.inflightAttrs && currentAttr in cached.inflightAttrs ? cached.inflightAttrs[currentAttr] : cached.remoteAttrs && currentAttr in cached.remoteAttrs ? cached.remoteAttrs[currentAttr] : undefined;
|
|
894
|
+
if (existing !== value) {
|
|
895
|
+
cached.localAttrs = cached.localAttrs || Object.create(null);
|
|
896
|
+
cached.localAttrs[currentAttr] = value;
|
|
897
|
+
cached.changes = cached.changes || Object.create(null);
|
|
898
|
+
cached.changes[currentAttr] = [existing, value];
|
|
899
|
+
} else if (cached.localAttrs) {
|
|
900
|
+
delete cached.localAttrs[currentAttr];
|
|
901
|
+
delete cached.changes[currentAttr];
|
|
902
|
+
}
|
|
903
|
+
if (cached.defaultAttrs && currentAttr in cached.defaultAttrs) {
|
|
904
|
+
delete cached.defaultAttrs[currentAttr];
|
|
905
|
+
}
|
|
906
|
+
this._capabilities.notifyChange(identifier, 'attributes', currentAttr);
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
// get current value from local else inflight else remote
|
|
911
|
+
// structuredClone current if not local (or always?)
|
|
912
|
+
// traverse path, update value at path
|
|
913
|
+
// notify change at first link in path.
|
|
914
|
+
// second pass optimization is change notifyChange signature to take an array path
|
|
915
|
+
|
|
916
|
+
// guaranteed that we have path of at least 2 in length
|
|
917
|
+
const path = attr;
|
|
918
|
+
const cached = this.__peek(identifier, false);
|
|
919
|
+
|
|
920
|
+
// get existing cache record for base path
|
|
921
|
+
const basePath = path[0];
|
|
922
|
+
const existing = cached.inflightAttrs && basePath in cached.inflightAttrs ? cached.inflightAttrs[basePath] : cached.remoteAttrs && basePath in cached.remoteAttrs ? cached.remoteAttrs[basePath] : undefined;
|
|
923
|
+
let existingAttr;
|
|
924
|
+
if (existing) {
|
|
925
|
+
existingAttr = existing[path[1]];
|
|
926
|
+
for (let i = 2; i < path.length; i++) {
|
|
927
|
+
// the specific change we're making is at path[length - 1]
|
|
928
|
+
existingAttr = existingAttr[path[i]];
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
if (existingAttr !== value) {
|
|
932
|
+
cached.localAttrs = cached.localAttrs || Object.create(null);
|
|
933
|
+
cached.localAttrs[basePath] = cached.localAttrs[basePath] || structuredClone(existing);
|
|
934
|
+
cached.changes = cached.changes || Object.create(null);
|
|
935
|
+
let currentLocal = cached.localAttrs[basePath];
|
|
936
|
+
let nextLink = 1;
|
|
937
|
+
while (nextLink < path.length - 1) {
|
|
938
|
+
currentLocal = currentLocal[path[nextLink++]];
|
|
939
|
+
}
|
|
940
|
+
currentLocal[path[nextLink]] = value;
|
|
941
|
+
cached.changes[basePath] = [existing, cached.localAttrs[basePath]];
|
|
942
|
+
|
|
943
|
+
// since we initiaize the value as basePath as a clone of the value at the remote basePath
|
|
944
|
+
// then in theory we can use JSON.stringify to compare the two values as key insertion order
|
|
945
|
+
// ought to be consistent.
|
|
946
|
+
// we try/catch this because users have a habit of doing "Bad Things"TM wherein the cache contains
|
|
947
|
+
// stateful values that are not JSON serializable correctly such as Dates.
|
|
948
|
+
// in the case that we error, we fallback to not removing the local value
|
|
949
|
+
// so that any changes we don't understand are preserved. Thse objects would then sometimes
|
|
950
|
+
// appear to be dirty unnecessarily, and for folks that open an issue we can guide them
|
|
951
|
+
// to make their cache data less stateful.
|
|
952
|
+
} else if (cached.localAttrs) {
|
|
953
|
+
try {
|
|
954
|
+
if (!existing) {
|
|
955
|
+
return;
|
|
956
|
+
}
|
|
957
|
+
const existingStr = JSON.stringify(existing);
|
|
958
|
+
const newStr = JSON.stringify(cached.localAttrs[basePath]);
|
|
959
|
+
if (existingStr !== newStr) {
|
|
960
|
+
delete cached.localAttrs[basePath];
|
|
961
|
+
delete cached.changes[basePath];
|
|
962
|
+
}
|
|
963
|
+
} catch {
|
|
964
|
+
// noop
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
this._capabilities.notifyChange(identifier, 'attributes', basePath);
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
/**
|
|
971
|
+
* Query the cache for the changed attributes of a resource.
|
|
972
|
+
*
|
|
973
|
+
* @category Resource Data
|
|
974
|
+
* @public
|
|
975
|
+
* @return `{ '<field>': ['<old>', '<new>'] }`
|
|
976
|
+
*/
|
|
977
|
+
changedAttrs(identifier) {
|
|
978
|
+
const cached = this.__peek(identifier, false);
|
|
979
|
+
|
|
980
|
+
// in Prod we try to recover when accessing something that
|
|
981
|
+
// doesn't exist
|
|
982
|
+
if (!cached) {
|
|
983
|
+
return Object.create(null);
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
// TODO freeze in dev
|
|
987
|
+
return cached.changes || Object.create(null);
|
|
988
|
+
}
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Query the cache for whether any mutated attributes exist
|
|
992
|
+
*
|
|
993
|
+
* @category Resource Data
|
|
994
|
+
* @public
|
|
995
|
+
*/
|
|
996
|
+
hasChangedAttrs(identifier) {
|
|
997
|
+
const cached = this.__peek(identifier, true);
|
|
998
|
+
|
|
999
|
+
// in Prod we try to recover when accessing something that
|
|
1000
|
+
// doesn't exist
|
|
1001
|
+
if (!cached) {
|
|
1002
|
+
return false;
|
|
1003
|
+
}
|
|
1004
|
+
return cached.inflightAttrs !== null && Object.keys(cached.inflightAttrs).length > 0 || cached.localAttrs !== null && Object.keys(cached.localAttrs).length > 0;
|
|
1005
|
+
}
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Tell the cache to discard any uncommitted mutations to attributes
|
|
1009
|
+
*
|
|
1010
|
+
* This method is a candidate to become a mutation
|
|
1011
|
+
*
|
|
1012
|
+
* @category Resource Data
|
|
1013
|
+
* @public
|
|
1014
|
+
* @return the names of fields that were restored
|
|
1015
|
+
*/
|
|
1016
|
+
rollbackAttrs(identifier) {
|
|
1017
|
+
const cached = this.__peek(identifier, false);
|
|
1018
|
+
let dirtyKeys;
|
|
1019
|
+
cached.isDeleted = false;
|
|
1020
|
+
if (cached.localAttrs !== null) {
|
|
1021
|
+
dirtyKeys = Object.keys(cached.localAttrs);
|
|
1022
|
+
cached.localAttrs = null;
|
|
1023
|
+
cached.changes = null;
|
|
1024
|
+
}
|
|
1025
|
+
if (cached.isNew) {
|
|
1026
|
+
// > Note: Graph removal handled by unloadRecord
|
|
1027
|
+
cached.isDeletionCommitted = true;
|
|
1028
|
+
cached.isDeleted = true;
|
|
1029
|
+
cached.isNew = false;
|
|
1030
|
+
}
|
|
1031
|
+
cached.inflightAttrs = null;
|
|
1032
|
+
cached.defaultAttrs = null;
|
|
1033
|
+
if (cached.errors) {
|
|
1034
|
+
cached.errors = null;
|
|
1035
|
+
this._capabilities.notifyChange(identifier, 'errors', null);
|
|
1036
|
+
}
|
|
1037
|
+
this._capabilities.notifyChange(identifier, 'state', null);
|
|
1038
|
+
if (dirtyKeys && dirtyKeys.length) {
|
|
1039
|
+
notifyAttributes(this._capabilities, identifier, new Set(dirtyKeys));
|
|
1040
|
+
}
|
|
1041
|
+
return dirtyKeys || [];
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
/**
|
|
1045
|
+
* Query the cache for the changes to relationships of a resource.
|
|
1046
|
+
*
|
|
1047
|
+
* Returns a map of relationship names to RelationshipDiff objects.
|
|
1048
|
+
*
|
|
1049
|
+
* ```ts
|
|
1050
|
+
* type RelationshipDiff =
|
|
1051
|
+
| {
|
|
1052
|
+
kind: 'collection';
|
|
1053
|
+
remoteState: ResourceKey[];
|
|
1054
|
+
additions: Set<ResourceKey>;
|
|
1055
|
+
removals: Set<ResourceKey>;
|
|
1056
|
+
localState: ResourceKey[];
|
|
1057
|
+
reordered: boolean;
|
|
1058
|
+
}
|
|
1059
|
+
| {
|
|
1060
|
+
kind: 'resource';
|
|
1061
|
+
remoteState: ResourceKey | null;
|
|
1062
|
+
localState: ResourceKey | null;
|
|
1063
|
+
};
|
|
1064
|
+
```
|
|
1065
|
+
*
|
|
1066
|
+
* @category Resource Data
|
|
1067
|
+
* @public
|
|
1068
|
+
*/
|
|
1069
|
+
changedRelationships(identifier) {
|
|
1070
|
+
return this.__graph.getChanged(identifier);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
/**
|
|
1074
|
+
* Query the cache for whether any mutated relationships exist
|
|
1075
|
+
*
|
|
1076
|
+
* @category Resource Data
|
|
1077
|
+
* @public
|
|
1078
|
+
*/
|
|
1079
|
+
hasChangedRelationships(identifier) {
|
|
1080
|
+
return this.__graph.hasChanged(identifier);
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
/**
|
|
1084
|
+
* Tell the cache to discard any uncommitted mutations to relationships.
|
|
1085
|
+
*
|
|
1086
|
+
* This will also discard the change on any appropriate inverses.
|
|
1087
|
+
*
|
|
1088
|
+
* This method is a candidate to become a mutation
|
|
1089
|
+
*
|
|
1090
|
+
* @category Resource Data
|
|
1091
|
+
* @public
|
|
1092
|
+
* @return the names of relationships that were restored
|
|
1093
|
+
*/
|
|
1094
|
+
rollbackRelationships(identifier) {
|
|
1095
|
+
assertPrivateCapabilities(this._capabilities);
|
|
1096
|
+
let result;
|
|
1097
|
+
this._capabilities._store._join(() => {
|
|
1098
|
+
result = this.__graph.rollback(identifier);
|
|
1099
|
+
});
|
|
1100
|
+
return result;
|
|
1101
|
+
}
|
|
1102
|
+
|
|
1103
|
+
/**
|
|
1104
|
+
* Query the cache for the current state of a relationship property
|
|
1105
|
+
*
|
|
1106
|
+
* @category Resource Data
|
|
1107
|
+
* @public
|
|
1108
|
+
* @return resource relationship object
|
|
1109
|
+
*/
|
|
1110
|
+
getRelationship(identifier, field) {
|
|
1111
|
+
return this.__graph.getData(identifier, field);
|
|
1112
|
+
}
|
|
1113
|
+
|
|
1114
|
+
/**
|
|
1115
|
+
* Query the cache for the remote state of a relationship property
|
|
1116
|
+
*
|
|
1117
|
+
* @category Resource Data
|
|
1118
|
+
* @public
|
|
1119
|
+
* @return resource relationship object
|
|
1120
|
+
*/
|
|
1121
|
+
getRemoteRelationship(identifier, field) {
|
|
1122
|
+
return this.__graph.getRemoteData(identifier, field);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
////////// ============== //////////
|
|
1126
|
+
////////// Resource State //////////
|
|
1127
|
+
////////// ============== //////////
|
|
1128
|
+
|
|
1129
|
+
/**
|
|
1130
|
+
* Update the cache state for the given resource to be marked
|
|
1131
|
+
* as locally deleted, or remove such a mark.
|
|
1132
|
+
*
|
|
1133
|
+
* This method is a candidate to become a mutation
|
|
1134
|
+
*
|
|
1135
|
+
* @category Resource State
|
|
1136
|
+
* @public
|
|
1137
|
+
*/
|
|
1138
|
+
setIsDeleted(identifier, isDeleted) {
|
|
1139
|
+
const cached = this.__peek(identifier, false);
|
|
1140
|
+
cached.isDeleted = isDeleted;
|
|
1141
|
+
// > Note: Graph removal for isNew handled by unloadRecord
|
|
1142
|
+
this._capabilities.notifyChange(identifier, 'state', null);
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* Query the cache for any validation errors applicable to the given resource.
|
|
1147
|
+
*
|
|
1148
|
+
* @category Resource State
|
|
1149
|
+
* @public
|
|
1150
|
+
*/
|
|
1151
|
+
getErrors(identifier) {
|
|
1152
|
+
return this.__peek(identifier, true).errors || [];
|
|
1153
|
+
}
|
|
1154
|
+
|
|
1155
|
+
/**
|
|
1156
|
+
* Query the cache for whether a given resource has any available data
|
|
1157
|
+
*
|
|
1158
|
+
* @category Resource State
|
|
1159
|
+
* @public
|
|
1160
|
+
*/
|
|
1161
|
+
isEmpty(identifier) {
|
|
1162
|
+
const cached = this.__safePeek(identifier, true);
|
|
1163
|
+
return cached ? cached.remoteAttrs === null && cached.inflightAttrs === null && cached.localAttrs === null : true;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
/**
|
|
1167
|
+
* Query the cache for whether a given resource was created locally and not
|
|
1168
|
+
* yet persisted.
|
|
1169
|
+
*
|
|
1170
|
+
* @category Resource State
|
|
1171
|
+
* @public
|
|
1172
|
+
*/
|
|
1173
|
+
isNew(identifier) {
|
|
1174
|
+
// TODO can we assert here?
|
|
1175
|
+
return this.__safePeek(identifier, true)?.isNew || false;
|
|
1176
|
+
}
|
|
1177
|
+
|
|
1178
|
+
/**
|
|
1179
|
+
* Query the cache for whether a given resource is marked as deleted (but not
|
|
1180
|
+
* necessarily persisted yet).
|
|
1181
|
+
*
|
|
1182
|
+
* @category Resource State
|
|
1183
|
+
* @public
|
|
1184
|
+
*/
|
|
1185
|
+
isDeleted(identifier) {
|
|
1186
|
+
// TODO can we assert here?
|
|
1187
|
+
return this.__safePeek(identifier, true)?.isDeleted || false;
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
/**
|
|
1191
|
+
* Query the cache for whether a given resource has been deleted and that deletion
|
|
1192
|
+
* has also been persisted.
|
|
1193
|
+
*
|
|
1194
|
+
* @category Resource State
|
|
1195
|
+
* @public
|
|
1196
|
+
*/
|
|
1197
|
+
isDeletionCommitted(identifier) {
|
|
1198
|
+
// TODO can we assert here?
|
|
1199
|
+
return this.__safePeek(identifier, true)?.isDeletionCommitted || false;
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
/**
|
|
1203
|
+
* Private method used to populate an entry for the identifier
|
|
1204
|
+
*
|
|
1205
|
+
* @internal
|
|
1206
|
+
*/
|
|
1207
|
+
_createCache(identifier) {
|
|
1208
|
+
const cache = makeCache();
|
|
1209
|
+
this.__cache.set(identifier, cache);
|
|
1210
|
+
return cache;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
/**
|
|
1214
|
+
* Peek whether we have cached resource data matching the identifier
|
|
1215
|
+
* without asserting if the resource data is missing.
|
|
1216
|
+
*
|
|
1217
|
+
* @internal
|
|
1218
|
+
*/
|
|
1219
|
+
__safePeek(identifier, allowDestroyed) {
|
|
1220
|
+
let resource = this.__cache.get(identifier);
|
|
1221
|
+
if (!resource && allowDestroyed) {
|
|
1222
|
+
resource = this.__destroyedCache.get(identifier);
|
|
1223
|
+
}
|
|
1224
|
+
return resource;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
/**
|
|
1228
|
+
* Peek whether we have cached resource data matching the identifier
|
|
1229
|
+
* Asserts if the resource data is missing.
|
|
1230
|
+
*
|
|
1231
|
+
* @internal
|
|
1232
|
+
*/
|
|
1233
|
+
__peek(identifier, allowDestroyed) {
|
|
1234
|
+
const resource = this.__safePeek(identifier, allowDestroyed);
|
|
1235
|
+
return resource;
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
function addResourceToDocument(cache, op) {
|
|
1239
|
+
const doc = cache.__documents.get(op.record.lid);
|
|
1240
|
+
const {
|
|
1241
|
+
content
|
|
1242
|
+
} = doc;
|
|
1243
|
+
if (op.field === 'data') {
|
|
1244
|
+
let shouldNotify = false;
|
|
1245
|
+
|
|
1246
|
+
// if data is not an array, we set the data property directly
|
|
1247
|
+
if (!Array.isArray(content.data)) {
|
|
1248
|
+
shouldNotify = content.data !== op.value;
|
|
1249
|
+
if (shouldNotify) content.data = op.value;
|
|
1250
|
+
} else {
|
|
1251
|
+
if (Array.isArray(op.value)) {
|
|
1252
|
+
if (op.index !== undefined) {
|
|
1253
|
+
// for collections, because we allow duplicates we are always changed.
|
|
1254
|
+
shouldNotify = true;
|
|
1255
|
+
content.data.splice(op.index, 0, ...op.value);
|
|
1256
|
+
} else {
|
|
1257
|
+
// for collections, because we allow duplicates we are always changed.
|
|
1258
|
+
shouldNotify = true;
|
|
1259
|
+
content.data.push(...op.value);
|
|
1260
|
+
}
|
|
1261
|
+
} else {
|
|
1262
|
+
if (op.index !== undefined) {
|
|
1263
|
+
// for collections, because we allow duplicates we are always changed.
|
|
1264
|
+
shouldNotify = true;
|
|
1265
|
+
content.data.splice(op.index, 0, op.value);
|
|
1266
|
+
} else {
|
|
1267
|
+
// for collections, because we allow duplicates we are always changed.
|
|
1268
|
+
shouldNotify = true;
|
|
1269
|
+
content.data.push(op.value);
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
|
|
1274
|
+
// notify
|
|
1275
|
+
if (shouldNotify) cache._capabilities.notifyChange(op.record, 'updated', null);
|
|
1276
|
+
return;
|
|
1277
|
+
}
|
|
1278
|
+
content.included = content.included || [];
|
|
1279
|
+
if (Array.isArray(op.value)) {
|
|
1280
|
+
content.included = content.included.concat(op.value);
|
|
1281
|
+
} else {
|
|
1282
|
+
content.included.push(op.value);
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
// we don't notify in the included case because this is not reactively
|
|
1286
|
+
// exposed. We should possibly consider doing so though for subscribers
|
|
1287
|
+
}
|
|
1288
|
+
function removeResourceFromDocument(cache, op) {
|
|
1289
|
+
const doc = cache.__documents.get(op.record.lid);
|
|
1290
|
+
const {
|
|
1291
|
+
content
|
|
1292
|
+
} = doc;
|
|
1293
|
+
if (op.field === 'data') {
|
|
1294
|
+
let shouldNotify = false;
|
|
1295
|
+
|
|
1296
|
+
// if data is not an array, we set the data property directly
|
|
1297
|
+
if (!Array.isArray(content.data)) {
|
|
1298
|
+
shouldNotify = content.data === op.value;
|
|
1299
|
+
// we only remove the value if it was our existing value
|
|
1300
|
+
if (shouldNotify) content.data = null;
|
|
1301
|
+
} else {
|
|
1302
|
+
const toRemove = Array.isArray(op.value) ? op.value : [op.value];
|
|
1303
|
+
for (let i = 0; i < toRemove.length; i++) {
|
|
1304
|
+
const value = toRemove[i];
|
|
1305
|
+
if (op.index !== undefined) {
|
|
1306
|
+
// in production we want to recover gracefully
|
|
1307
|
+
// so we fallback to first-index-of
|
|
1308
|
+
const index = op.index < content.data.length && content.data[op.index] === value ? op.index : content.data.indexOf(value);
|
|
1309
|
+
if (index !== -1) {
|
|
1310
|
+
// we remove the first occurrence of the value
|
|
1311
|
+
shouldNotify = true;
|
|
1312
|
+
content.data.splice(index, 1);
|
|
1313
|
+
}
|
|
1314
|
+
} else {
|
|
1315
|
+
// we remove the first occurrence of the value
|
|
1316
|
+
const index = content.data.indexOf(value);
|
|
1317
|
+
if (index !== -1) {
|
|
1318
|
+
shouldNotify = true;
|
|
1319
|
+
content.data.splice(index, 1);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
// notify
|
|
1326
|
+
if (shouldNotify) cache._capabilities.notifyChange(op.record, 'updated', null);
|
|
1327
|
+
} else {
|
|
1328
|
+
content.included = content.included || [];
|
|
1329
|
+
const toRemove = Array.isArray(op.value) ? op.value : [op.value];
|
|
1330
|
+
for (const identifier of toRemove) {
|
|
1331
|
+
const index = content.included.indexOf(identifier);
|
|
1332
|
+
if (index !== -1) {
|
|
1333
|
+
content.included.splice(index, 1);
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1337
|
+
// we don't notify in the included case because this is not reactively
|
|
1338
|
+
// exposed. We should possibly consider doing so though for subscribers
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
function areAllModelsUnloaded(wrapper, identifiers) {
|
|
1342
|
+
for (let i = 0; i < identifiers.length; ++i) {
|
|
1343
|
+
const identifier = identifiers[i];
|
|
1344
|
+
if (wrapper.hasRecord(identifier)) {
|
|
1345
|
+
return false;
|
|
1346
|
+
}
|
|
1347
|
+
}
|
|
1348
|
+
return true;
|
|
1349
|
+
}
|
|
1350
|
+
function getLocalState(rel) {
|
|
1351
|
+
if (isBelongsTo(rel)) {
|
|
1352
|
+
return rel.localState ? [rel.localState] : [];
|
|
1353
|
+
}
|
|
1354
|
+
return rel.additions ? [...rel.additions] : [];
|
|
1355
|
+
}
|
|
1356
|
+
function getRemoteState(rel) {
|
|
1357
|
+
if (isBelongsTo(rel)) {
|
|
1358
|
+
return rel.remoteState ? [rel.remoteState] : [];
|
|
1359
|
+
}
|
|
1360
|
+
return rel.remoteState;
|
|
1361
|
+
}
|
|
1362
|
+
function schemaHasLegacyDefaultValueFn(schema) {
|
|
1363
|
+
if (!schema) return false;
|
|
1364
|
+
return hasLegacyDefaultValueFn(schema.options);
|
|
1365
|
+
}
|
|
1366
|
+
function hasLegacyDefaultValueFn(options) {
|
|
1367
|
+
return !!options && typeof options.defaultValue === 'function';
|
|
1368
|
+
}
|
|
1369
|
+
function getDefaultValue(schema, identifier, store) {
|
|
1370
|
+
const options = schema?.options;
|
|
1371
|
+
if (!schema || !options && !schema.type) {
|
|
1372
|
+
return;
|
|
1373
|
+
}
|
|
1374
|
+
if (schema.kind !== 'attribute' && schema.kind !== 'field') {
|
|
1375
|
+
return;
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
// legacy support for defaultValues that are functions
|
|
1379
|
+
if (hasLegacyDefaultValueFn(options)) {
|
|
1380
|
+
// If anyone opens an issue for args not working right, we'll restore + deprecate it via a Proxy
|
|
1381
|
+
// that lazily instantiates the record. We don't want to provide any args here
|
|
1382
|
+
// because in a non @ember-data-mirror/model world they don't make sense.
|
|
1383
|
+
return options.defaultValue();
|
|
1384
|
+
// legacy support for defaultValues that are primitives
|
|
1385
|
+
} else if (options && 'defaultValue' in options) {
|
|
1386
|
+
const defaultValue = options.defaultValue;
|
|
1387
|
+
return defaultValue;
|
|
1388
|
+
|
|
1389
|
+
// new style transforms
|
|
1390
|
+
} else if (schema.kind !== 'attribute' && schema.type) {
|
|
1391
|
+
const transform = store.schema.transformation(schema);
|
|
1392
|
+
if (transform?.defaultValue) {
|
|
1393
|
+
return transform.defaultValue(options || null, identifier);
|
|
1394
|
+
}
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
function notifyAttributes(storeWrapper, identifier, keys) {
|
|
1398
|
+
if (!keys) {
|
|
1399
|
+
storeWrapper.notifyChange(identifier, 'attributes', null);
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
for (const key of keys) {
|
|
1403
|
+
storeWrapper.notifyChange(identifier, 'attributes', key);
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
/*
|
|
1408
|
+
TODO @deprecate IGOR DAVID
|
|
1409
|
+
There seems to be a potential bug here, where we will return keys that are not
|
|
1410
|
+
in the schema
|
|
1411
|
+
*/
|
|
1412
|
+
function calculateChangedKeys(cached, updates, fields) {
|
|
1413
|
+
const changedKeys = new Set();
|
|
1414
|
+
const keys = Object.keys(updates);
|
|
1415
|
+
const length = keys.length;
|
|
1416
|
+
const localAttrs = cached.localAttrs;
|
|
1417
|
+
const original = Object.assign(Object.create(null), cached.remoteAttrs, cached.inflightAttrs);
|
|
1418
|
+
for (let i = 0; i < length; i++) {
|
|
1419
|
+
const key = keys[i];
|
|
1420
|
+
if (!fields.has(key)) {
|
|
1421
|
+
continue;
|
|
1422
|
+
}
|
|
1423
|
+
const value = updates[key];
|
|
1424
|
+
|
|
1425
|
+
// A value in localAttrs means the user has a local change to
|
|
1426
|
+
// this attribute. We never override this value when merging
|
|
1427
|
+
// updates from the backend so we should not sent a change
|
|
1428
|
+
// notification if the server value differs from the original.
|
|
1429
|
+
if (localAttrs && localAttrs[key] !== undefined) {
|
|
1430
|
+
continue;
|
|
1431
|
+
}
|
|
1432
|
+
if (original[key] !== value) {
|
|
1433
|
+
changedKeys.add(key);
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
return changedKeys;
|
|
1437
|
+
}
|
|
1438
|
+
function cacheIsEmpty(cached) {
|
|
1439
|
+
return !cached || cached.remoteAttrs === null && cached.inflightAttrs === null && cached.localAttrs === null;
|
|
1440
|
+
}
|
|
1441
|
+
function _isEmpty(peeked) {
|
|
1442
|
+
if (!peeked) {
|
|
1443
|
+
return true;
|
|
1444
|
+
}
|
|
1445
|
+
const isNew = peeked.isNew;
|
|
1446
|
+
const isDeleted = peeked.isDeleted;
|
|
1447
|
+
const isEmpty = cacheIsEmpty(peeked);
|
|
1448
|
+
return (!isNew || isDeleted) && isEmpty;
|
|
1449
|
+
}
|
|
1450
|
+
function recordIsLoaded(cached, filterDeleted = false) {
|
|
1451
|
+
if (!cached) {
|
|
1452
|
+
return false;
|
|
1453
|
+
}
|
|
1454
|
+
const isNew = cached.isNew;
|
|
1455
|
+
const isEmpty = cacheIsEmpty(cached);
|
|
1456
|
+
|
|
1457
|
+
// if we are new we must consider ourselves loaded
|
|
1458
|
+
if (isNew) {
|
|
1459
|
+
return !cached.isDeleted;
|
|
1460
|
+
}
|
|
1461
|
+
// even if we have a past request, if we are now empty we are not loaded
|
|
1462
|
+
// typically this is true after an unloadRecord call
|
|
1463
|
+
|
|
1464
|
+
// if we are not empty, not new && we have a fulfilled request then we are loaded
|
|
1465
|
+
// we should consider allowing for something to be loaded that is simply "not empty".
|
|
1466
|
+
// which is how RecordState currently handles this case; however, RecordState is buggy
|
|
1467
|
+
// in that it does not account for unloading.
|
|
1468
|
+
return filterDeleted && cached.isDeletionCommitted ? false : !isEmpty;
|
|
1469
|
+
}
|
|
1470
|
+
function _isLoading(peeked, capabilities, identifier) {
|
|
1471
|
+
assertPrivateCapabilities(capabilities);
|
|
1472
|
+
// TODO refactor things such that the cache is not required to know
|
|
1473
|
+
// about isLoading
|
|
1474
|
+
const req = capabilities._store.getRequestStateService();
|
|
1475
|
+
// const fulfilled = req.getLastRequestForRecord(identifier);
|
|
1476
|
+
const isLoaded = recordIsLoaded(peeked);
|
|
1477
|
+
return !isLoaded &&
|
|
1478
|
+
// fulfilled === null &&
|
|
1479
|
+
req.getPendingRequestsForRecord(identifier).some(r => r.type === 'query');
|
|
1480
|
+
}
|
|
1481
|
+
function setupRelationships(graph, fields, identifier, data) {
|
|
1482
|
+
for (const name in data.relationships) {
|
|
1483
|
+
const relationshipData = data.relationships[name];
|
|
1484
|
+
const field = fields.get(name);
|
|
1485
|
+
// TODO consider asserting if the relationship is not in the schema
|
|
1486
|
+
// we intentionally ignore relationships that are not in the schema
|
|
1487
|
+
if (!relationshipData || !field || !isRelationship(field)) continue;
|
|
1488
|
+
graph.push({
|
|
1489
|
+
op: 'updateRelationship',
|
|
1490
|
+
record: identifier,
|
|
1491
|
+
field: name,
|
|
1492
|
+
value: relationshipData
|
|
1493
|
+
});
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
function isRelationship(field) {
|
|
1497
|
+
const {
|
|
1498
|
+
kind
|
|
1499
|
+
} = field;
|
|
1500
|
+
return kind === 'hasMany' || kind === 'belongsTo' || kind === 'resource' || kind === 'collection';
|
|
1501
|
+
}
|
|
1502
|
+
function patchLocalAttributes(cached, changedRemoteKeys) {
|
|
1503
|
+
const {
|
|
1504
|
+
localAttrs,
|
|
1505
|
+
remoteAttrs,
|
|
1506
|
+
inflightAttrs,
|
|
1507
|
+
defaultAttrs,
|
|
1508
|
+
changes
|
|
1509
|
+
} = cached;
|
|
1510
|
+
if (!localAttrs) {
|
|
1511
|
+
cached.changes = null;
|
|
1512
|
+
return false;
|
|
1513
|
+
}
|
|
1514
|
+
let hasAppliedPatch = false;
|
|
1515
|
+
const mutatedKeys = Object.keys(localAttrs);
|
|
1516
|
+
for (let i = 0, length = mutatedKeys.length; i < length; i++) {
|
|
1517
|
+
const attr = mutatedKeys[i];
|
|
1518
|
+
const existing = inflightAttrs && attr in inflightAttrs ? inflightAttrs[attr] : remoteAttrs && attr in remoteAttrs ? remoteAttrs[attr] : undefined;
|
|
1519
|
+
if (existing === localAttrs[attr]) {
|
|
1520
|
+
hasAppliedPatch = true;
|
|
1521
|
+
|
|
1522
|
+
// if the local change is committed, then
|
|
1523
|
+
// the remoteKeyChange is no longer relevant
|
|
1524
|
+
changedRemoteKeys?.delete(attr);
|
|
1525
|
+
delete localAttrs[attr];
|
|
1526
|
+
delete changes[attr];
|
|
1527
|
+
}
|
|
1528
|
+
if (defaultAttrs && attr in defaultAttrs) {
|
|
1529
|
+
delete defaultAttrs[attr];
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
return hasAppliedPatch;
|
|
1533
|
+
}
|
|
1534
|
+
function putOne(cache, identifiers, resource) {
|
|
1535
|
+
let identifier = identifiers.peekResourceKey(resource);
|
|
1536
|
+
if (identifier) {
|
|
1537
|
+
identifier = identifiers.updateRecordIdentifier(identifier, resource);
|
|
1538
|
+
} else {
|
|
1539
|
+
identifier = identifiers.getOrCreateRecordIdentifier(resource);
|
|
1540
|
+
}
|
|
1541
|
+
cache.upsert(identifier, resource, cache._capabilities.hasRecord(identifier));
|
|
1542
|
+
// even if the identifier was not "existing" before, it is now
|
|
1543
|
+
return identifier;
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
/*
|
|
1547
|
+
Iterates over the set of internal models reachable from `this` across exactly one
|
|
1548
|
+
relationship.
|
|
1549
|
+
*/
|
|
1550
|
+
function _directlyRelatedIdentifiersIterable(storeWrapper, originating) {
|
|
1551
|
+
const graph = peekGraph(storeWrapper);
|
|
1552
|
+
const initializedRelationships = graph?.identifiers.get(originating);
|
|
1553
|
+
if (!initializedRelationships) {
|
|
1554
|
+
return EMPTY_ITERATOR;
|
|
1555
|
+
}
|
|
1556
|
+
const initializedRelationshipsArr = [];
|
|
1557
|
+
Object.keys(initializedRelationships).forEach(key => {
|
|
1558
|
+
const rel = initializedRelationships[key];
|
|
1559
|
+
if (rel && !isImplicit(rel)) {
|
|
1560
|
+
initializedRelationshipsArr.push(rel);
|
|
1561
|
+
}
|
|
1562
|
+
});
|
|
1563
|
+
let i = 0;
|
|
1564
|
+
let j = 0;
|
|
1565
|
+
let k = 0;
|
|
1566
|
+
const findNext = () => {
|
|
1567
|
+
while (i < initializedRelationshipsArr.length) {
|
|
1568
|
+
while (j < 2) {
|
|
1569
|
+
const relatedIdentifiers = j === 0 ? getLocalState(initializedRelationshipsArr[i]) : getRemoteState(initializedRelationshipsArr[i]);
|
|
1570
|
+
while (k < relatedIdentifiers.length) {
|
|
1571
|
+
const relatedIdentifier = relatedIdentifiers[k++];
|
|
1572
|
+
if (relatedIdentifier !== null) {
|
|
1573
|
+
return relatedIdentifier;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
k = 0;
|
|
1577
|
+
j++;
|
|
1578
|
+
}
|
|
1579
|
+
j = 0;
|
|
1580
|
+
i++;
|
|
1581
|
+
}
|
|
1582
|
+
return undefined;
|
|
1583
|
+
};
|
|
1584
|
+
return {
|
|
1585
|
+
iterator() {
|
|
1586
|
+
return {
|
|
1587
|
+
next: () => {
|
|
1588
|
+
const value = findNext();
|
|
1589
|
+
return {
|
|
1590
|
+
value,
|
|
1591
|
+
done: value === undefined
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
};
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
/*
|
|
1600
|
+
Computes the set of Identifiers reachable from this Identifier.
|
|
1601
|
+
|
|
1602
|
+
Reachability is determined over the relationship graph (ie a graph where
|
|
1603
|
+
nodes are identifiers and edges are belongs to or has many
|
|
1604
|
+
relationships).
|
|
1605
|
+
|
|
1606
|
+
Returns an array including `this` and all identifiers reachable
|
|
1607
|
+
from `this.identifier`.
|
|
1608
|
+
*/
|
|
1609
|
+
function _allRelatedIdentifiers(storeWrapper, originating) {
|
|
1610
|
+
const array = [];
|
|
1611
|
+
const queue = [];
|
|
1612
|
+
const seen = new Set();
|
|
1613
|
+
queue.push(originating);
|
|
1614
|
+
while (queue.length > 0) {
|
|
1615
|
+
const identifier = queue.shift();
|
|
1616
|
+
array.push(identifier);
|
|
1617
|
+
seen.add(identifier);
|
|
1618
|
+
const iterator = _directlyRelatedIdentifiersIterable(storeWrapper, originating).iterator();
|
|
1619
|
+
for (let obj = iterator.next(); !obj.done; obj = iterator.next()) {
|
|
1620
|
+
const relatedIdentifier = obj.value;
|
|
1621
|
+
if (relatedIdentifier && !seen.has(relatedIdentifier)) {
|
|
1622
|
+
seen.add(relatedIdentifier);
|
|
1623
|
+
queue.push(relatedIdentifier);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
}
|
|
1627
|
+
return array;
|
|
1628
|
+
}
|
|
1629
|
+
function fromBaseDocument(doc) {
|
|
1630
|
+
const resourceDocument = {};
|
|
1631
|
+
const jsonApiDoc = doc.content;
|
|
1632
|
+
if (jsonApiDoc) {
|
|
1633
|
+
copyLinksAndMeta(resourceDocument, jsonApiDoc);
|
|
1634
|
+
}
|
|
1635
|
+
return resourceDocument;
|
|
1636
|
+
}
|
|
1637
|
+
function fromStructuredError(doc) {
|
|
1638
|
+
const errorDoc = {};
|
|
1639
|
+
if (doc.content) {
|
|
1640
|
+
copyLinksAndMeta(errorDoc, doc.content);
|
|
1641
|
+
if ('errors' in doc.content) {
|
|
1642
|
+
errorDoc.errors = doc.content.errors;
|
|
1643
|
+
} else if (typeof doc.error === 'object' && 'errors' in doc.error) {
|
|
1644
|
+
errorDoc.errors = doc.error.errors;
|
|
1645
|
+
} else {
|
|
1646
|
+
errorDoc.errors = [{
|
|
1647
|
+
title: doc.message
|
|
1648
|
+
}];
|
|
1649
|
+
}
|
|
1650
|
+
}
|
|
1651
|
+
return errorDoc;
|
|
1652
|
+
}
|
|
1653
|
+
function copyLinksAndMeta(target, source) {
|
|
1654
|
+
if ('links' in source) {
|
|
1655
|
+
target.links = source.links;
|
|
1656
|
+
}
|
|
1657
|
+
if ('meta' in source) {
|
|
1658
|
+
target.meta = source.meta;
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
function cacheUpsert(cache, identifier, data, calculateChanges) {
|
|
1662
|
+
let changedKeys;
|
|
1663
|
+
const peeked = cache.__safePeek(identifier, false);
|
|
1664
|
+
const existed = !!peeked;
|
|
1665
|
+
const cached = peeked || cache._createCache(identifier);
|
|
1666
|
+
const isLoading = /*#__NOINLINE__*/_isLoading(peeked, cache._capabilities, identifier) || !recordIsLoaded(peeked);
|
|
1667
|
+
const isUpdate = /*#__NOINLINE__*/!_isEmpty(peeked) && !isLoading;
|
|
1668
|
+
if (cached.isNew) {
|
|
1669
|
+
cached.isNew = false;
|
|
1670
|
+
cache._capabilities.notifyChange(identifier, 'identity', null);
|
|
1671
|
+
cache._capabilities.notifyChange(identifier, 'state', null);
|
|
1672
|
+
}
|
|
1673
|
+
const fields = getCacheFields(cache, identifier);
|
|
1674
|
+
|
|
1675
|
+
// if no cache entry existed, no record exists / property has been accessed
|
|
1676
|
+
// and thus we do not need to notify changes to any properties.
|
|
1677
|
+
if (calculateChanges && existed && data.attributes) {
|
|
1678
|
+
changedKeys = calculateChangedKeys(cached, data.attributes, fields);
|
|
1679
|
+
}
|
|
1680
|
+
cached.remoteAttrs = Object.assign(cached.remoteAttrs || Object.create(null), data.attributes);
|
|
1681
|
+
if (cached.localAttrs) {
|
|
1682
|
+
if (patchLocalAttributes(cached, changedKeys)) {
|
|
1683
|
+
cache._capabilities.notifyChange(identifier, 'state', null);
|
|
1684
|
+
}
|
|
1685
|
+
}
|
|
1686
|
+
if (!isUpdate) {
|
|
1687
|
+
cache._capabilities.notifyChange(identifier, 'added', null);
|
|
1688
|
+
}
|
|
1689
|
+
if (data.id) {
|
|
1690
|
+
cached.id = data.id;
|
|
1691
|
+
}
|
|
1692
|
+
if (data.relationships) {
|
|
1693
|
+
setupRelationships(cache.__graph, fields, identifier, data);
|
|
1694
|
+
}
|
|
1695
|
+
if (changedKeys?.size) {
|
|
1696
|
+
notifyAttributes(cache._capabilities, identifier, changedKeys);
|
|
1697
|
+
}
|
|
1698
|
+
return changedKeys?.size ? Array.from(changedKeys) : undefined;
|
|
1699
|
+
}
|
|
1700
|
+
function patchCache(Cache, op) {
|
|
1701
|
+
const isRecord = isResourceKey(op.record);
|
|
1702
|
+
!isRecord && isRequestKey(op.record);
|
|
1703
|
+
switch (op.op) {
|
|
1704
|
+
case 'mergeIdentifiers':
|
|
1705
|
+
{
|
|
1706
|
+
const cache = Cache.__cache.get(op.record);
|
|
1707
|
+
if (cache) {
|
|
1708
|
+
Cache.__cache.set(op.value, cache);
|
|
1709
|
+
Cache.__cache.delete(op.record);
|
|
1710
|
+
}
|
|
1711
|
+
Cache.__graph.update(op, true);
|
|
1712
|
+
break;
|
|
1713
|
+
}
|
|
1714
|
+
case 'update':
|
|
1715
|
+
{
|
|
1716
|
+
if (isRecord) {
|
|
1717
|
+
if ('field' in op) {
|
|
1718
|
+
const field = getCacheFields(Cache, op.record).get(op.field);
|
|
1719
|
+
if (isRelationship(field)) {
|
|
1720
|
+
Cache.__graph.push(op);
|
|
1721
|
+
} else {
|
|
1722
|
+
Cache.upsert(op.record, {
|
|
1723
|
+
type: op.record.type,
|
|
1724
|
+
id: op.record.id,
|
|
1725
|
+
attributes: {
|
|
1726
|
+
[op.field]: op.value
|
|
1727
|
+
}
|
|
1728
|
+
}, Cache._capabilities.hasRecord(op.record));
|
|
1729
|
+
}
|
|
1730
|
+
} else {
|
|
1731
|
+
Cache.upsert(op.record, op.value, Cache._capabilities.hasRecord(op.record));
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
break;
|
|
1735
|
+
}
|
|
1736
|
+
case 'add':
|
|
1737
|
+
{
|
|
1738
|
+
if (isRecord) {
|
|
1739
|
+
if ('field' in op) {
|
|
1740
|
+
Cache.__graph.push(op);
|
|
1741
|
+
} else {
|
|
1742
|
+
Cache.upsert(op.record, op.value, Cache._capabilities.hasRecord(op.record));
|
|
1743
|
+
}
|
|
1744
|
+
} else {
|
|
1745
|
+
addResourceToDocument(Cache, op);
|
|
1746
|
+
}
|
|
1747
|
+
break;
|
|
1748
|
+
}
|
|
1749
|
+
case 'remove':
|
|
1750
|
+
{
|
|
1751
|
+
if (isRecord) {
|
|
1752
|
+
if ('field' in op) {
|
|
1753
|
+
Cache.__graph.push(op);
|
|
1754
|
+
} else {
|
|
1755
|
+
const cached = Cache.__safePeek(op.record, false);
|
|
1756
|
+
if (cached) {
|
|
1757
|
+
cached.isDeleted = true;
|
|
1758
|
+
cached.isDeletionCommitted = true;
|
|
1759
|
+
Cache.unloadRecord(op.record);
|
|
1760
|
+
} else {
|
|
1761
|
+
peekGraph(Cache._capabilities)?.push({
|
|
1762
|
+
op: 'deleteRecord',
|
|
1763
|
+
record: op.record,
|
|
1764
|
+
isNew: false
|
|
1765
|
+
});
|
|
1766
|
+
}
|
|
1767
|
+
}
|
|
1768
|
+
} else {
|
|
1769
|
+
if ('field' in op) {
|
|
1770
|
+
removeResourceFromDocument(Cache, op);
|
|
1771
|
+
}
|
|
1772
|
+
}
|
|
1773
|
+
break;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
function getCacheFields(cache, identifier) {
|
|
1778
|
+
if (cache._capabilities.schema.cacheFields) {
|
|
1779
|
+
const result = cache._capabilities.schema.cacheFields(identifier);
|
|
1780
|
+
if (result) {
|
|
1781
|
+
return result;
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
// the model schema service cannot process fields that are not cache fields
|
|
1786
|
+
return cache._capabilities.schema.fields(identifier);
|
|
1787
|
+
}
|
|
1788
|
+
function commitDidError(cache, identifier, errors) {
|
|
1789
|
+
const cached = cache.__peek(identifier, false);
|
|
1790
|
+
if (cached.inflightAttrs) {
|
|
1791
|
+
const keys = Object.keys(cached.inflightAttrs);
|
|
1792
|
+
if (keys.length > 0) {
|
|
1793
|
+
const attrs = cached.localAttrs = cached.localAttrs || Object.create(null);
|
|
1794
|
+
for (let i = 0; i < keys.length; i++) {
|
|
1795
|
+
if (attrs[keys[i]] === undefined) {
|
|
1796
|
+
attrs[keys[i]] = cached.inflightAttrs[keys[i]];
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
cached.inflightAttrs = null;
|
|
1801
|
+
}
|
|
1802
|
+
if (errors) {
|
|
1803
|
+
cached.errors = errors;
|
|
1804
|
+
}
|
|
1805
|
+
cache._capabilities.notifyChange(identifier, 'errors', null);
|
|
1806
|
+
}
|
|
1807
|
+
function didCommit(cache, committedIdentifier, data, op) {
|
|
1808
|
+
const {
|
|
1809
|
+
cacheKeyManager
|
|
1810
|
+
} = cache._capabilities;
|
|
1811
|
+
const existingId = committedIdentifier.id;
|
|
1812
|
+
const identifier = op !== 'deleteRecord' && data ? cacheKeyManager.updateRecordIdentifier(committedIdentifier, data) : committedIdentifier;
|
|
1813
|
+
const cached = cache.__peek(identifier, false);
|
|
1814
|
+
if (cached.isDeleted || op === 'deleteRecord') {
|
|
1815
|
+
cache.__graph.push({
|
|
1816
|
+
op: 'deleteRecord',
|
|
1817
|
+
record: identifier,
|
|
1818
|
+
isNew: false
|
|
1819
|
+
});
|
|
1820
|
+
cached.isDeleted = true;
|
|
1821
|
+
cached.isDeletionCommitted = true;
|
|
1822
|
+
cache._capabilities.notifyChange(identifier, 'removed', null);
|
|
1823
|
+
// TODO @runspired should we early exit here?
|
|
1824
|
+
}
|
|
1825
|
+
const fields = getCacheFields(cache, identifier);
|
|
1826
|
+
cached.isNew = false;
|
|
1827
|
+
let newCanonicalAttributes;
|
|
1828
|
+
if (data) {
|
|
1829
|
+
if (data.id && !cached.id) {
|
|
1830
|
+
cached.id = data.id;
|
|
1831
|
+
}
|
|
1832
|
+
if (identifier === committedIdentifier && identifier.id !== existingId) {
|
|
1833
|
+
cache._capabilities.notifyChange(identifier, 'identity', null);
|
|
1834
|
+
}
|
|
1835
|
+
if (data.relationships) {
|
|
1836
|
+
setupRelationships(cache.__graph, fields, identifier, data);
|
|
1837
|
+
}
|
|
1838
|
+
newCanonicalAttributes = data.attributes;
|
|
1839
|
+
}
|
|
1840
|
+
const changedKeys = newCanonicalAttributes && calculateChangedKeys(cached, newCanonicalAttributes, fields);
|
|
1841
|
+
cached.remoteAttrs = Object.assign(cached.remoteAttrs || Object.create(null), cached.inflightAttrs, newCanonicalAttributes);
|
|
1842
|
+
cached.inflightAttrs = null;
|
|
1843
|
+
patchLocalAttributes(cached, changedKeys);
|
|
1844
|
+
if (cached.errors) {
|
|
1845
|
+
cached.errors = null;
|
|
1846
|
+
cache._capabilities.notifyChange(identifier, 'errors', null);
|
|
1847
|
+
}
|
|
1848
|
+
if (changedKeys?.size) notifyAttributes(cache._capabilities, identifier, changedKeys);
|
|
1849
|
+
cache._capabilities.notifyChange(identifier, 'state', null);
|
|
1850
|
+
}
|
|
1851
|
+
function willCommit(cache, identifier) {
|
|
1852
|
+
const cached = cache.__peek(identifier, false);
|
|
1853
|
+
|
|
1854
|
+
/*
|
|
1855
|
+
if we have multiple saves in flight at once then
|
|
1856
|
+
we have information loss no matter what. This
|
|
1857
|
+
attempts to lose the least information.
|
|
1858
|
+
If we were to clear inflightAttrs, previous requests
|
|
1859
|
+
would not be able to use it during their didCommit.
|
|
1860
|
+
If we upsert inflightattrs, previous requests incorrectly
|
|
1861
|
+
see more recent inflight changes as part of their own and
|
|
1862
|
+
will incorrectly mark the new state as the correct remote state.
|
|
1863
|
+
We choose this latter behavior to avoid accidentally removing
|
|
1864
|
+
earlier changes.
|
|
1865
|
+
If apps do not want this behavior they can either
|
|
1866
|
+
- chain save requests serially vs allowing concurrent saves
|
|
1867
|
+
- move to using a request handler that caches the inflight state
|
|
1868
|
+
on a per-request basis
|
|
1869
|
+
- change their save requests to only send a "PATCH" instead of a "PUT"
|
|
1870
|
+
so that only latest changes are involved in each request, and then also
|
|
1871
|
+
ensure that the API or their handler reflects only those changes back
|
|
1872
|
+
for upsert into the cache.
|
|
1873
|
+
*/
|
|
1874
|
+
if (cached.inflightAttrs) {
|
|
1875
|
+
if (cached.localAttrs) {
|
|
1876
|
+
Object.assign(cached.inflightAttrs, cached.localAttrs);
|
|
1877
|
+
}
|
|
1878
|
+
} else {
|
|
1879
|
+
cached.inflightAttrs = cached.localAttrs;
|
|
1880
|
+
}
|
|
1881
|
+
cached.localAttrs = null;
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
export { JSONAPICache };
|