-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument-collection.js
271 lines (271 loc) · 7.82 KB
/
document-collection.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
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
import { CacheableHelper } from './services/cacheable-helper.';
import { Resource } from './resource';
import { Page } from './services/page';
import { Document } from './document';
import { Converter } from './services/converter';
import { isDevMode } from '@angular/core';
// unsupported: template constraints.
/**
* @template R
*/
export class RelatedDocumentCollection extends Document {
constructor() {
super(...arguments);
this.data = [];
this.page = new Page();
this.ttl = 0;
this.content = 'ids';
}
/**
* @param {?} iterated_resource
* @return {?}
*/
trackBy(iterated_resource) {
return iterated_resource.id;
}
/**
* @param {?} id
* @return {?}
*/
find(id) {
if (this.content === 'ids') {
return null;
}
// this is the best way: https://jsperf.com/fast-array-foreach
for (let i = 0; i < this.data.length; i++) {
if (this.data[i].id === id) {
return /** @type {?} */ (this.data[i]);
}
}
return null;
}
/**
* @param {?} data_collection
* @return {?}
*/
fill(data_collection) {
this.data_collection = data_collection;
Converter.buildIncluded(data_collection);
// sometimes get Cannot set property 'number' of undefined (page)
if (this.page && data_collection.meta) {
this.page.number = data_collection.meta["page"] || 1;
this.page.resources_per_page = data_collection.meta["resources_per_page"] || null; // @deprecated (v2.0.2)
this.page.size = data_collection.meta["resources_per_page"] || null;
this.page.total_resources = data_collection.meta["total_resources"] || null;
}
/** @type {?} */
let new_ids = {};
this.data.length = 0;
this.builded = data_collection.data && data_collection.data.length === 0;
for (let dataresource of data_collection.data) {
try {
/** @type {?} */
let res = this.getResourceOrFail(dataresource);
res.fill({ data: dataresource });
new_ids[dataresource.id] = dataresource.id;
(/** @type {?} */ (this.data)).push(/** @type {?} */ (res));
if (Object.keys(res.attributes).length > 0) {
this.builded = true;
}
}
catch (error) {
this.content = 'ids';
this.builded = false;
this.data.push({ id: dataresource.id, type: dataresource.type });
}
}
// remove old members of collection (bug, for example, when request something like orders/10/details and has new ids)
// @todo test with relation.data.filter(resource => resource.id != id );
for (let i; i < this.data.length; i++) {
if (!(this.data[i].id in new_ids)) {
delete this.data[i];
}
}
this.meta = data_collection.meta || {};
if ('cache_last_update' in data_collection) {
this.cache_last_update = data_collection.cache_last_update;
}
}
/**
* @param {?} dataresource
* @return {?}
*/
getResourceOrFail(dataresource) {
/** @type {?} */
let res = this.find(dataresource.id);
if (res !== null) {
return res;
}
/** @type {?} */
let service = Converter.getService(dataresource.type);
// remove when getService return null or catch errors
// this prvent a fill on undefinied service :/
if (!service) {
if (isDevMode()) {
console.warn('The relationship ' +
'relation_alias?' +
' (type ' +
dataresource.type +
') cant be generated because service for this type has not been injected.');
}
throw new Error('Cant create service for ' + dataresource.type);
}
// END remove when getService return null or catch errors
return service.getOrCreateResource(dataresource.id);
}
/**
* @param {?} resource
* @return {?}
*/
replaceOrAdd(resource) {
/** @type {?} */
let res = this.find(resource.id);
if (res === null) {
(/** @type {?} */ (this.data)).push(resource);
}
else {
res = resource;
}
}
/**
* @return {?}
*/
hasMorePages() {
if (!this.page.size || this.page.size < 1) {
return null;
}
/** @type {?} */
let total_resources = this.page.size * (this.page.number - 1) + this.data.length;
return total_resources < this.page.total_resources;
}
/**
* @param {?} value
* @return {?}
*/
setLoaded(value) {
// tslint:disable-next-line:deprecation
this.is_loading = !value;
this.loaded = value;
}
/**
* @param {?} value
* @return {?}
*/
setLoadedAndPropagate(value) {
this.setLoaded(value);
if (this.content === 'ids') {
return;
}
(/** @type {?} */ (this.data)).forEach(resource => {
CacheableHelper.propagateLoaded(resource.relationships, value);
});
}
/**
* @param {?} value
* @return {?}
*/
setBuilded(value) {
this.builded = value;
}
/**
* @param {?} value
* @return {?}
*/
setBuildedAndPropagate(value) {
this.setBuilded(value);
if (this.content === 'ids') {
return;
}
(/** @type {?} */ (this.data)).forEach(resource => {
resource.setLoaded(value);
});
}
/**
* @param {?} value
* @return {?}
*/
setSource(value) {
this.source = value;
}
/**
* @param {?} value
* @return {?}
*/
setSourceAndPropagate(value) {
this.setSource(value);
this.data.forEach(resource => {
if (resource instanceof Resource) {
resource.setSource(value);
}
});
}
/**
* @param {?=} value
* @return {?}
*/
setCacheLastUpdate(value = Date.now()) {
this.cache_last_update = value;
}
/**
* @param {?=} value
* @return {?}
*/
setCacheLastUpdateAndPropagate(value = Date.now()) {
this.setCacheLastUpdate(value);
this.data.forEach(resource => {
if (resource instanceof Resource) {
resource.setCacheLastUpdate(value);
}
});
}
/**
* @param {?=} params
* @return {?}
*/
toObject(params) {
if (!this.builded) {
return { data: this.data };
}
/** @type {?} */
let data = (/** @type {?} */ (this.data)).map(resource => {
return resource.toObject(params).data;
});
return {
data: data
};
}
}
if (false) {
/** @type {?} */
RelatedDocumentCollection.prototype.data;
/** @type {?} */
RelatedDocumentCollection.prototype.page;
/** @type {?} */
RelatedDocumentCollection.prototype.ttl;
/** @type {?} */
RelatedDocumentCollection.prototype.content;
/** @type {?} */
RelatedDocumentCollection.prototype.data_collection;
}
// unsupported: template constraints.
/**
* @template R
*/
export class DocumentCollection extends RelatedDocumentCollection {
constructor() {
super(...arguments);
this.data = [];
this.content = 'collection';
}
}
if (false) {
/** @type {?} */
DocumentCollection.prototype.data;
/** @type {?} */
DocumentCollection.prototype.content;
}
//# sourceMappingURL=document-collection.js.map