-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathrecord-data.js
785 lines (666 loc) · 23 KB
/
record-data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
import { DEBUG } from '@glimmer/env';
import { assign } from '@ember/polyfills';
import { isEqual } from '@ember/utils';
import { assert, warn, inspect } from '@ember/debug';
import { run } from '@ember/runloop';
import isEnabled from '../../features';
import Relationships from '../relationships/state/create';
import coerceId from '../coerce-id';
let nextBfsId = 1;
export default class RecordData {
constructor(modelName, id, clientId, storeWrapper, store) {
this.store = store;
this.modelName = modelName;
this.__relationships = null;
this.__implicitRelationships = null;
this.clientId = clientId;
this.id = id;
this.storeWrapper = storeWrapper;
this.isDestroyed = false;
this._isNew = false;
// Used during the mark phase of unloading to avoid checking the same internal
// model twice in the same scan
this._bfsId = 0;
this.reset();
}
// PUBLIC API
getResourceIdentifier() {
return {
id: this.id,
type: this.modelName,
clientId: this.clientId,
};
}
pushData(data, calculateChange) {
let changedKeys;
if (calculateChange) {
changedKeys = this._changedKeys(data.attributes);
}
assign(this._data, data.attributes);
if (this.__attributes) {
// only do if we have attribute changes
this._updateChangedAttributes();
}
if (data.relationships) {
this._setupRelationships(data);
}
if (data.id) {
this.id = coerceId(data.id);
}
return changedKeys;
}
willCommit() {
this._inFlightAttributes = this._attributes;
this._attributes = null;
}
hasChangedAttributes() {
return this.__attributes !== null && Object.keys(this.__attributes).length > 0;
}
// this is a hack bc we don't have access to the state machine
// and relationships need this info and @runspired didn't see
// how to get it just yet from storeWrapper.
isEmpty() {
return this.__attributes === null && this.__inFlightAttributes === null && this.__data === null;
}
reset() {
this.__attributes = null;
this.__inFlightAttributes = null;
this.__data = null;
}
_setupRelationships(data) {
let relationships = this.storeWrapper.relationshipsDefinitionFor(this.modelName);
let keys = Object.keys(relationships);
for (let i = 0; i < keys.length; i++) {
let relationshipName = keys[i];
if (!data.relationships[relationshipName]) {
continue;
}
// in debug, assert payload validity eagerly
let relationshipData = data.relationships[relationshipName];
if (DEBUG) {
let store = this.store;
let recordData = this;
let relationshipMeta = relationships[relationshipName];
if (!relationshipData || !relationshipMeta) {
continue;
}
if (relationshipData.links) {
let isAsync = relationshipMeta.options && relationshipMeta.options.async !== false;
warn(
`You pushed a record of type '${
this.modelName
}' with a relationship '${relationshipName}' configured as 'async: false'. You've included a link but no primary data, this may be an error in your payload. EmberData will treat this relationship as known-to-be-empty.`,
isAsync || relationshipData.data,
{
id: 'ds.store.push-link-for-sync-relationship',
}
);
} else if (relationshipData.data) {
if (relationshipMeta.kind === 'belongsTo') {
assert(
`A ${
this.modelName
} record was pushed into the store with the value of ${relationshipName} being ${inspect(
relationshipData.data
)}, but ${relationshipName} is a belongsTo relationship so the value must not be an array. You should probably check your data payload or serializer.`,
!Array.isArray(relationshipData.data)
);
assertRelationshipData(store, recordData, relationshipData.data, relationshipMeta);
} else if (relationshipMeta.kind === 'hasMany') {
assert(
`A ${
this.modelName
} record was pushed into the store with the value of ${relationshipName} being '${inspect(
relationshipData.data
)}', but ${relationshipName} is a hasMany relationship so the value must be an array. You should probably check your data payload or serializer.`,
Array.isArray(relationshipData.data)
);
if (Array.isArray(relationshipData.data)) {
for (let i = 0; i < relationshipData.data.length; i++) {
assertRelationshipData(
store,
recordData,
relationshipData.data[i],
relationshipMeta
);
}
}
}
}
}
let relationship = this._relationships.get(relationshipName);
relationship.push(relationshipData);
}
}
/*
Checks if the attributes which are considered as changed are still
different to the state which is acknowledged by the server.
This method is needed when data for the internal model is pushed and the
pushed data might acknowledge dirty attributes as confirmed.
@method updateChangedAttributes
@private
*/
_updateChangedAttributes() {
let changedAttributes = this.changedAttributes();
let changedAttributeNames = Object.keys(changedAttributes);
let attrs = this._attributes;
for (let i = 0, length = changedAttributeNames.length; i < length; i++) {
let attribute = changedAttributeNames[i];
let data = changedAttributes[attribute];
let oldData = data[0];
let newData = data[1];
if (oldData === newData) {
delete attrs[attribute];
}
}
}
/*
Returns an object, whose keys are changed properties, and value is an
[oldProp, newProp] array.
@method changedAttributes
@private
*/
changedAttributes() {
let oldData = this._data;
let currentData = this._attributes;
let inFlightData = this._inFlightAttributes;
let newData = assign({}, inFlightData, currentData);
let diffData = Object.create(null);
let newDataKeys = Object.keys(newData);
for (let i = 0, length = newDataKeys.length; i < length; i++) {
let key = newDataKeys[i];
diffData[key] = [oldData[key], newData[key]];
}
return diffData;
}
isNew() {
return this._isNew;
}
rollbackAttributes() {
let dirtyKeys;
if (this.hasChangedAttributes()) {
dirtyKeys = Object.keys(this._attributes);
this._attributes = null;
}
if (this.isNew()) {
this.removeFromInverseRelationships(true);
}
this._inFlightAttributes = null;
return dirtyKeys;
}
didCommit(data) {
this._isNew = false;
if (data) {
// this.store._internalModelDidReceiveRelationshipData(this.modelName, this.id, data.relationships);
if (data.relationships) {
this._setupRelationships(data);
}
if (data.id) {
// didCommit provided an ID, notify the store of it
this.storeWrapper.setRecordId(this.modelName, data.id, this.clientId);
this.id = coerceId(data.id);
}
data = data.attributes;
}
let changedKeys = this._changedKeys(data);
assign(this._data, this.__inFlightAttributes, data);
this._inFlightAttributes = null;
this._updateChangedAttributes();
return changedKeys;
}
// get ResourceIdentifiers for "current state"
getHasMany(key) {
return this._relationships.get(key).getData();
}
// set a new "current state" via ResourceIdentifiers
setDirtyHasMany(key, recordDatas) {
let relationship = this._relationships.get(key);
relationship.clear();
relationship.addRecordDatas(recordDatas);
}
// append to "current state" via RecordDatas
addToHasMany(key, recordDatas, idx) {
this._relationships.get(key).addRecordDatas(recordDatas, idx);
}
// remove from "current state" via RecordDatas
removeFromHasMany(key, recordDatas) {
this._relationships.get(key).removeRecordDatas(recordDatas);
}
commitWasRejected() {
let keys = Object.keys(this._inFlightAttributes);
if (keys.length > 0) {
let attrs = this._attributes;
for (let i = 0; i < keys.length; i++) {
if (attrs[keys[i]] === undefined) {
attrs[keys[i]] = this._inFlightAttributes[keys[i]];
}
}
}
this._inFlightAttributes = null;
}
getBelongsTo(key) {
return this._relationships.get(key).getData();
}
setDirtyBelongsTo(key, recordDataOrPromise) {
if (recordDataOrPromise === undefined) {
recordDataOrPromise = null;
}
if (recordDataOrPromise && recordDataOrPromise.then) {
this._relationships.get(key).setRecordPromise(recordDataOrPromise);
} else {
this._relationships.get(key).setRecordData(recordDataOrPromise);
}
}
setDirtyAttribute(key, value) {
let originalValue;
// Add the new value to the changed attributes hash
this._attributes[key] = value;
if (key in this._inFlightAttributes) {
originalValue = this._inFlightAttributes[key];
} else {
originalValue = this._data[key];
}
// If we went back to our original value, we shouldn't keep the attribute around anymore
if (value === originalValue) {
delete this._attributes[key];
}
}
getAttr(key) {
if (key in this._attributes) {
return this._attributes[key];
} else if (key in this._inFlightAttributes) {
return this._inFlightAttributes[key];
} else {
return this._data[key];
}
}
hasAttr(key) {
return key in this._attributes || key in this._inFlightAttributes || key in this._data;
}
unloadRecord() {
if (this.isDestroyed) {
return;
}
this._destroyRelationships();
this.reset();
if (!this._scheduledDestroy) {
this._scheduledDestroy = run.backburner.schedule(
'destroy',
this,
'_cleanupOrphanedRecordDatas'
);
}
}
_cleanupOrphanedRecordDatas() {
let relatedRecordDatas = this._allRelatedRecordDatas();
if (areAllModelsUnloaded(relatedRecordDatas)) {
for (let i = 0; i < relatedRecordDatas.length; ++i) {
let recordData = relatedRecordDatas[i];
if (!recordData.isDestroyed) {
recordData.destroy();
}
}
}
this._scheduledDestroy = null;
}
destroy() {
this._relationships.forEach((name, rel) => rel.destroy());
this.isDestroyed = true;
this.storeWrapper.disconnectRecord(this.modelName, this.id, this.clientId);
}
isRecordInUse() {
return this.storeWrapper.isRecordInUse(this.modelName, this.id, this.clientId);
}
/**
Computes the set of internal models reachable from `this` across exactly one
relationship.
@return {Array} An array containing the internal models that `this` belongs
to or has many.
*/
_directlyRelatedRecordDatas() {
let array = [];
this._relationships.forEach((name, rel) => {
let members = rel.members.list;
let canonicalMembers = rel.canonicalMembers.list;
array = array.concat(members, canonicalMembers);
});
return array;
}
/**
Computes the set of internal models reachable from this internal model.
Reachability is determined over the relationship graph (ie a graph where
nodes are internal models and edges are belongs to or has many
relationships).
@return {Array} An array including `this` and all internal models reachable
from `this`.
*/
_allRelatedRecordDatas() {
let array = [];
let queue = [];
let bfsId = nextBfsId++;
queue.push(this);
this._bfsId = bfsId;
while (queue.length > 0) {
let node = queue.shift();
array.push(node);
let related = node._directlyRelatedRecordDatas();
for (let i = 0; i < related.length; ++i) {
let recordData = related[i];
assert('Internal Error: seen a future bfs iteration', recordData._bfsId <= bfsId);
if (recordData._bfsId < bfsId) {
queue.push(recordData);
recordData._bfsId = bfsId;
}
}
}
return array;
}
isAttrDirty(key) {
if (this._attributes[key] === undefined) {
return false;
}
let originalValue;
if (this._inFlightAttributes[key] !== undefined) {
originalValue = this._inFlightAttributes[key];
} else {
originalValue = this._data[key];
}
return originalValue !== this._attributes[key];
}
get _attributes() {
if (this.__attributes === null) {
this.__attributes = Object.create(null);
}
return this.__attributes;
}
set _attributes(v) {
this.__attributes = v;
}
get _relationships() {
if (this.__relationships === null) {
this.__relationships = new Relationships(this);
}
return this.__relationships;
}
get _data() {
if (this.__data === null) {
this.__data = Object.create(null);
}
return this.__data;
}
set _data(v) {
this.__data = v;
}
/*
implicit relationships are relationship which have not been declared but the inverse side exists on
another record somewhere
For example if there was
```app/models/comment.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr()
})
```
but there is also
```app/models/post.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
comments: DS.hasMany('comment')
})
```
would have a implicit post relationship in order to be do things like remove ourselves from the post
when we are deleted
*/
get _implicitRelationships() {
if (this.__implicitRelationships === null) {
this.__implicitRelationships = Object.create(null);
}
return this.__implicitRelationships;
}
get _inFlightAttributes() {
if (this.__inFlightAttributes === null) {
this.__inFlightAttributes = Object.create(null);
}
return this.__inFlightAttributes;
}
set _inFlightAttributes(v) {
this.__inFlightAttributes = v;
}
/**
* Receives options passed to `store.createRecord` and is given the opportunity
* to handle them.
*
* The return value is an object of options to pass to `Record.create()`
*
* @param options
* @private
*/
_initRecordCreateOptions(options) {
let createOptions = {};
if (options !== undefined) {
let { modelName, storeWrapper } = this;
let attributeDefs = storeWrapper.attributesDefinitionFor(modelName);
let relationshipDefs = storeWrapper.relationshipsDefinitionFor(modelName);
let relationships = this._relationships;
let propertyNames = Object.keys(options);
for (let i = 0; i < propertyNames.length; i++) {
let name = propertyNames[i];
let propertyValue = options[name];
if (name === 'id') {
this.id = propertyValue;
continue;
}
let fieldType = relationshipDefs[name] || attributeDefs[name];
let kind = fieldType !== undefined ? fieldType.kind : null;
let relationship;
switch (kind) {
case 'attribute':
this.setDirtyAttribute(name, propertyValue);
break;
case 'belongsTo':
this.setDirtyBelongsTo(name, propertyValue);
relationship = relationships.get(name);
relationship.setHasAnyRelationshipData(true);
relationship.setRelationshipIsEmpty(false);
break;
case 'hasMany':
this.setDirtyHasMany(name, propertyValue);
relationship = relationships.get(name);
relationship.setHasAnyRelationshipData(true);
relationship.setRelationshipIsEmpty(false);
break;
default:
// reflect back (pass-thru) unknown properties
createOptions[name] = propertyValue;
}
}
}
return createOptions;
}
/*
TODO IGOR AND DAVID this shouldn't be public
This method should only be called by records in the `isNew()` state OR once the record
has been deleted and that deletion has been persisted.
It will remove this record from any associated relationships.
If `isNew` is true (default false), it will also completely reset all
relationships to an empty state as well.
@method removeFromInverseRelationships
@param {Boolean} isNew whether to unload from the `isNew` perspective
@private
*/
removeFromInverseRelationships(isNew = false) {
this._relationships.forEach((name, rel) => {
rel.removeCompletelyFromInverse();
if (isNew === true) {
rel.clear();
}
});
let implicitRelationships = this._implicitRelationships;
this.__implicitRelationships = null;
Object.keys(implicitRelationships).forEach(key => {
let rel = implicitRelationships[key];
rel.removeCompletelyFromInverse();
if (isNew === true) {
rel.clear();
}
});
}
_destroyRelationships() {
let relationships = this._relationships;
relationships.forEach((name, rel) => destroyRelationship(rel));
let implicitRelationships = this._implicitRelationships;
this.__implicitRelationships = null;
Object.keys(implicitRelationships).forEach(key => {
let rel = implicitRelationships[key];
destroyRelationship(rel);
});
}
clientDidCreate() {
this._isNew = true;
}
/*
Ember Data has 3 buckets for storing the value of an attribute on an internalModel.
`_data` holds all of the attributes that have been acknowledged by
a backend via the adapter. When rollbackAttributes is called on a model all
attributes will revert to the record's state in `_data`.
`_attributes` holds any change the user has made to an attribute
that has not been acknowledged by the adapter. Any values in
`_attributes` are have priority over values in `_data`.
`_inFlightAttributes`. When a record is being synced with the
backend the values in `_attributes` are copied to
`_inFlightAttributes`. This way if the backend acknowledges the
save but does not return the new state Ember Data can copy the
values from `_inFlightAttributes` to `_data`. Without having to
worry about changes made to `_attributes` while the save was
happenign.
Changed keys builds a list of all of the values that may have been
changed by the backend after a successful save.
It does this by iterating over each key, value pair in the payload
returned from the server after a save. If the `key` is found in
`_attributes` then the user has a local changed to the attribute
that has not been synced with the server and the key is not
included in the list of changed keys.
If the value, for a key differs from the value in what Ember Data
believes to be the truth about the backend state (A merger of the
`_data` and `_inFlightAttributes` objects where
`_inFlightAttributes` has priority) then that means the backend
has updated the value and the key is added to the list of changed
keys.
@method _changedKeys
@private
*/
/*
TODO IGOR DAVID
There seems to be a potential bug here, where we will return keys that are not
in the schema
*/
_changedKeys(updates) {
let changedKeys = [];
if (updates) {
let original, i, value, key;
let keys = Object.keys(updates);
let length = keys.length;
let hasAttrs = this.hasChangedAttributes();
let attrs;
if (hasAttrs) {
attrs = this._attributes;
}
original = assign(Object.create(null), this._data, this.__inFlightAttributes);
for (i = 0; i < length; i++) {
key = keys[i];
value = updates[key];
// A value in _attributes means the user has a local change to
// this attributes. We never override this value when merging
// updates from the backend so we should not sent a change
// notification if the server value differs from the original.
if (hasAttrs === true && attrs[key] !== undefined) {
continue;
}
if (!isEqual(original[key], value)) {
changedKeys.push(key);
}
}
}
return changedKeys;
}
toString() {
return `<${this.modelName}:${this.id}>`;
}
}
if (isEnabled('ds-rollback-attribute')) {
/*
Returns the latest truth for an attribute - the canonical value, or the
in-flight value.
@method lastAcknowledgedValue
@private
*/
RecordData.prototype.lastAcknowledgedValue = function lastAcknowledgedValue(key) {
if (key in this._inFlightAttributes) {
return this._inFlightAttributes[key];
} else {
return this._data[key];
}
};
}
function assertRelationshipData(store, recordData, data, meta) {
assert(
`A ${recordData.modelName} record was pushed into the store with the value of ${
meta.key
} being '${JSON.stringify(data)}', but ${
meta.key
} is a belongsTo relationship so the value must not be an array. You should probably check your data payload or serializer.`,
!Array.isArray(data)
);
assert(
`Encountered a relationship identifier without a type for the ${meta.kind} relationship '${
meta.key
}' on ${recordData}, expected a json-api identifier with type '${
meta.type
}' but found '${JSON.stringify(
data
)}'. Please check your serializer and make sure it is serializing the relationship payload into a JSON API format.`,
data === null || (typeof data.type === 'string' && data.type.length)
);
assert(
`Encountered a relationship identifier without an id for the ${meta.kind} relationship '${
meta.key
}' on ${recordData}, expected a json-api identifier but found '${JSON.stringify(
data
)}'. Please check your serializer and make sure it is serializing the relationship payload into a JSON API format.`,
data === null || coerceId(data.id)
);
assert(
`Encountered a relationship identifier with type '${data.type}' for the ${
meta.kind
} relationship '${meta.key}' on ${recordData}, Expected a json-api identifier with type '${
meta.type
}'. No model was found for '${data.type}'.`,
data === null || !data.type || store._hasModelFor(data.type)
);
}
// Handle dematerialization for relationship `rel`. In all cases, notify the
// relatinoship of the dematerialization: this is done so the relationship can
// notify its inverse which needs to update state
//
// If the inverse is sync, unloading this record is treated as a client-side
// delete, so we remove the inverse records from this relationship to
// disconnect the graph. Because it's not async, we don't need to keep around
// the internalModel as an id-wrapper for references and because the graph is
// disconnected we can actually destroy the internalModel when checking for
// orphaned models.
function destroyRelationship(rel) {
rel.recordDataDidDematerialize();
if (rel._inverseIsSync()) {
rel.removeAllRecordDatasFromOwn();
rel.removeAllCanonicalRecordDatasFromOwn();
}
}
function areAllModelsUnloaded(recordDatas) {
for (let i = 0; i < recordDatas.length; ++i) {
if (recordDatas[i].isRecordInUse()) {
return false;
}
}
return true;
}