-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathrelationship.js
247 lines (218 loc) · 7.86 KB
/
relationship.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
import OrderedSet from "ember-data/system/ordered-set";
export default function Relationship(store, record, inverseKey, relationshipMeta) {
var async = relationshipMeta.options.async;
this.members = new OrderedSet();
this.canonicalMembers = new OrderedSet();
this.store = store;
this.key = relationshipMeta.key;
this.inverseKey = inverseKey;
this.record = record;
this.isAsync = typeof async === 'undefined' ? true : async;
this.relationshipMeta = relationshipMeta;
//This probably breaks for polymorphic relationship in complex scenarios, due to
//multiple possible modelNames
this.inverseKeyForImplicit = this.record.constructor.modelName + this.key;
this.linkPromise = null;
this.meta = null;
this.hasData = false;
this.hasLoaded = false;
}
Relationship.prototype = {
constructor: Relationship,
destroy: Ember.K,
updateMeta: function(meta) {
this.meta = meta;
},
clear: function() {
var members = this.members.list;
var member;
while (members.length > 0) {
member = members[0];
this.removeRecord(member);
}
},
removeRecords: function(records) {
records.forEach((record) => this.removeRecord(record));
},
addRecords: function(records, idx) {
records.forEach((record) => {
this.addRecord(record, idx);
if (idx !== undefined) {
idx++;
}
});
},
addCanonicalRecords: function(records, idx) {
for (var i=0; i<records.length; i++) {
if (idx !== undefined) {
this.addCanonicalRecord(records[i], i+idx);
} else {
this.addCanonicalRecord(records[i]);
}
}
},
addCanonicalRecord: function(record, idx) {
if (!this.canonicalMembers.has(record)) {
this.canonicalMembers.add(record);
if (this.inverseKey) {
record._relationships.get(this.inverseKey).addCanonicalRecord(this.record);
} else {
if (!record._implicitRelationships[this.inverseKeyForImplicit]) {
record._implicitRelationships[this.inverseKeyForImplicit] = new Relationship(this.store, record, this.key, { options: {} });
}
record._implicitRelationships[this.inverseKeyForImplicit].addCanonicalRecord(this.record);
}
}
this.flushCanonicalLater();
this.setHasData(true);
},
removeCanonicalRecords: function(records, idx) {
for (var i=0; i<records.length; i++) {
if (idx !== undefined) {
this.removeCanonicalRecord(records[i], i+idx);
} else {
this.removeCanonicalRecord(records[i]);
}
}
},
removeCanonicalRecord: function(record, idx) {
if (this.canonicalMembers.has(record)) {
this.removeCanonicalRecordFromOwn(record);
if (this.inverseKey) {
this.removeCanonicalRecordFromInverse(record);
} else {
if (record._implicitRelationships[this.inverseKeyForImplicit]) {
record._implicitRelationships[this.inverseKeyForImplicit].removeCanonicalRecord(this.record);
}
}
}
this.flushCanonicalLater();
},
addRecord: function(record, idx) {
if (!this.members.has(record)) {
this.members.addWithIndex(record, idx);
this.notifyRecordRelationshipAdded(record, idx);
if (this.inverseKey) {
record._relationships.get(this.inverseKey).addRecord(this.record);
} else {
if (!record._implicitRelationships[this.inverseKeyForImplicit]) {
record._implicitRelationships[this.inverseKeyForImplicit] = new Relationship(this.store, record, this.key, { options: {} });
}
record._implicitRelationships[this.inverseKeyForImplicit].addRecord(this.record);
}
this.record.updateRecordArraysLater();
}
this.setHasData(true);
},
removeRecord: function(record) {
if (this.members.has(record)) {
this.removeRecordFromOwn(record);
if (this.inverseKey) {
this.removeRecordFromInverse(record);
} else {
if (record._implicitRelationships[this.inverseKeyForImplicit]) {
record._implicitRelationships[this.inverseKeyForImplicit].removeRecord(this.record);
}
}
}
},
removeRecordFromInverse: function(record) {
var inverseRelationship = record._relationships.get(this.inverseKey);
//Need to check for existence, as the record might unloading at the moment
if (inverseRelationship) {
inverseRelationship.removeRecordFromOwn(this.record);
}
},
removeRecordFromOwn: function(record) {
this.members.delete(record);
this.notifyRecordRelationshipRemoved(record);
this.record.updateRecordArrays();
},
removeCanonicalRecordFromInverse: function(record) {
var inverseRelationship = record._relationships.get(this.inverseKey);
//Need to check for existence, as the record might unloading at the moment
if (inverseRelationship) {
inverseRelationship.removeCanonicalRecordFromOwn(this.record);
}
},
removeCanonicalRecordFromOwn: function(record) {
this.canonicalMembers.delete(record);
this.flushCanonicalLater();
},
flushCanonical: function() {
this.willSync = false;
//a hack for not removing new records
//TODO remove once we have proper diffing
var newRecords = [];
for (var i=0; i<this.members.list.length; i++) {
if (this.members.list[i].isNew()) {
newRecords.push(this.members.list[i]);
}
}
//TODO(Igor) make this less abysmally slow
this.members = this.canonicalMembers.copy();
for (i=0; i<newRecords.length; i++) {
this.members.add(newRecords[i]);
}
},
flushCanonicalLater: function() {
if (this.willSync) {
return;
}
this.willSync = true;
this.store._backburner.join(() => this.store._backburner.schedule('syncRelationships', this, this.flushCanonical));
},
updateLink: function(link) {
Ember.warn(`You have pushed a record of type '${this.record.type.modelName}' with '${this.key}' as a link, but the association is not an async relationship.`, this.isAsync, {
id: 'ds.store.push-link-for-sync-relationship'
});
Ember.assert("You have pushed a record of type '" + this.record.type.modelName + "' with '" + this.key + "' as a link, but the value of that link is not a string.", typeof link === 'string' || link === null);
if (link !== this.link) {
this.link = link;
this.linkPromise = null;
this.setHasLoaded(false);
this.record.notifyPropertyChange(this.key);
}
},
findLink: function() {
if (this.linkPromise) {
return this.linkPromise;
} else {
var promise = this.fetchLink();
this.linkPromise = promise;
return promise.then((result) => result);
}
},
updateRecordsFromAdapter: function(records) {
//TODO(Igor) move this to a proper place
//TODO Once we have adapter support, we need to handle updated and canonical changes
this.computeChanges(records);
this.setHasData(true);
this.setHasLoaded(true);
},
notifyRecordRelationshipAdded: Ember.K,
notifyRecordRelationshipRemoved: Ember.K,
/*
`hasData` for a relationship is a flag to indicate if we consider the
content of this relationship "known". Snapshots uses this to tell the
difference between unknown (`undefined`) or empty (`null`). The reason for
this is that we wouldn't want to serialize unknown relationships as `null`
as that might overwrite remote state.
All relationships for a newly created (`store.createRecord()`) are
considered known (`hasData === true`).
*/
setHasData: function(value) {
this.hasData = value;
},
/*
`hasLoaded` is a flag to indicate if we have gotten data from the adapter or
not when the relationship has a link.
This is used to be able to tell when to fetch the link and when to return
the local data in scenarios where the local state is considered known
(`hasData === true`).
Updating the link will automatically set `hasLoaded` to `false`.
*/
setHasLoaded: function(value) {
this.hasLoaded = value;
}
};