-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathpromise-many-array.ts
344 lines (305 loc) · 7.48 KB
/
promise-many-array.ts
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
import ArrayMixin from '@ember/array';
import { assert } from '@ember/debug';
import { dependentKeyCompat } from '@ember/object/compat';
import { tracked } from '@glimmer/tracking';
import Ember from 'ember';
import { resolve } from 'rsvp';
import { DEPRECATE_EVENTED_API_USAGE } from '@ember-data/private-build-infra/deprecations';
/**
@module @ember-data/model
*/
/**
This class is returned as the result of accessing an async hasMany relationship
on an instance of a Model extending from `@ember-data/model`.
A PromiseManyArray is an array-like proxy that also proxies certain method calls
to the underlying ManyArray in addition to being "promisified".
Right now we proxy:
* `reload()`
* `createRecord()`
This promise-proxy behavior is primarily to ensure that async relationship interact
nicely with templates. In your JS code you should resolve the promise first.
```js
const comments = await post.comments;
```
@class PromiseManyArray
@public
*/
export default class PromiseManyArray {
declare promise: Promise<any> | null;
declare isDestroyed: boolean;
declare isDestroying: boolean;
constructor(promise, content) {
this._update(promise, content);
this.isDestroyed = false;
this.isDestroying = false;
const meta = Ember.meta(this);
meta.hasMixin = (mixin: Object) => {
if (mixin === ArrayMixin) {
return true;
}
return false;
};
}
//---- Methods/Properties on ArrayProxy that we will keep as our API
@tracked content: any | null = null;
/**
* Retrieve the length of the content
* @property length
* @public
*/
@dependentKeyCompat
get length(): number {
// shouldn't be needed, but ends up being needed
// for computed chains even in 4.x
this['[]'];
return this.content ? this.content.length : 0;
}
// ember-source < 3.23 (e.g. 3.20 lts)
// requires that the tag `'[]'` be notified
// on the ArrayProxy in order for `{{#each}}`
// to recompute. We entangle the '[]' tag from
@dependentKeyCompat
get '[]'() {
return this.content ? this.content['[]'] : this.content;
}
/**
* Iterate the proxied content. Called by the glimmer iterator in #each
*
* @method forEach
* @param cb
* @returns
* @private
*/
forEach(cb) {
this['[]']; // needed for < 3.23 support e.g. 3.20 lts
if (this.content && this.length) {
this.content.forEach(cb);
}
}
//---- Properties/Methods from the PromiseProxyMixin that we will keep as our API
/**
* Whether the loading promise is still pending
*
* @property {boolean} isPending
* @public
*/
@tracked isPending: boolean = false;
/**
* Whether the loading promise rejected
*
* @property {boolean} isRejected
* @public
*/
@tracked isRejected: boolean = false;
/**
* Whether the loading promise succeeded
*
* @property {boolean} isFulfilled
* @public
*/
@tracked isFulfilled: boolean = false;
/**
* Whether the loading promise completed (resolved or rejected)
*
* @property {boolean} isSettled
* @public
*/
@tracked isSettled: boolean = false;
/**
* chain this promise
*
* @method then
* @public
* @param success
* @param fail
* @returns Promise
*/
then(s, f) {
return this.promise!.then(s, f);
}
/**
* catch errors thrown by this promise
* @method catch
* @public
* @param callback
* @returns Promise
*/
catch(cb) {
return this.promise!.catch(cb);
}
/**
* run cleanup after this promise completes
*
* @method finally
* @public
* @param callback
* @returns Promise
*/
finally(cb) {
return this.promise!.finally(cb);
}
//---- Methods on EmberObject that we should keep
destroy() {
this.isDestroying = true;
this.isDestroyed = true;
this.content = null;
this.promise = null;
}
//---- Methods/Properties on ManyArray that we own and proxy to
/**
* Retrieve the links for this relationship
* @property links
* @public
*/
@dependentKeyCompat
get links() {
return this.content ? this.content.links : undefined;
}
/**
* Retrieve the meta for this relationship
* @property meta
* @public
*/
@dependentKeyCompat
get meta() {
return this.content ? this.content.meta : undefined;
}
/**
* Reload the relationship
* @method reload
* @public
* @param options
* @returns
*/
reload(options) {
assert('You are trying to reload an async manyArray before it has been created', this.content);
this.content.reload(options);
return this;
}
//---- Our own stuff
_update(promise, content) {
if (content !== undefined) {
this.content = content;
}
this.promise = tapPromise(this, promise);
}
static create({ promise, content }) {
return new this(promise, content);
}
// Methods on ManyArray which people should resolve the relationship first before calling
createRecord(...args) {
assert('You are trying to createRecord on an async manyArray before it has been created', this.content);
return this.content.createRecord(...args);
}
// Properties/Methods on ArrayProxy we should deprecate
get firstObject() {
return this.content ? this.content.firstObject : undefined;
}
get lastObject() {
return this.content ? this.content.lastObject : undefined;
}
}
function tapPromise(proxy, promise) {
proxy.isPending = true;
proxy.isSettled = false;
proxy.isFulfilled = false;
proxy.isRejected = false;
return resolve(promise).then(
(content) => {
proxy.isPending = false;
proxy.isFulfilled = true;
proxy.isSettled = true;
proxy.content = content;
return content;
},
(error) => {
proxy.isPending = false;
proxy.isFulfilled = false;
proxy.isRejected = true;
proxy.isSettled = true;
throw error;
}
);
}
const EmberObjectMethods = [
'addObserver',
'cacheFor',
'decrementProperty',
'get',
'getProperties',
'incrementProperty',
'notifyPropertyChange',
'removeObserver',
'set',
'setProperties',
'toggleProperty',
];
EmberObjectMethods.forEach((method) => {
PromiseManyArray.prototype[method] = function delegatedMethod(...args) {
return Ember[method](this, ...args);
};
});
const InheritedProxyMethods = [
'addArrayObserver',
'addObject',
'addObjects',
'any',
'arrayContentDidChange',
'arrayContentWillChange',
'clear',
'compact',
'every',
'filter',
'filterBy',
'find',
'findBy',
'getEach',
'includes',
'indexOf',
'insertAt',
'invoke',
'isAny',
'isEvery',
'lastIndexOf',
'map',
'mapBy',
'objectAt',
'objectsAt',
'popObject',
'pushObject',
'pushObjects',
'reduce',
'reject',
'rejectBy',
'removeArrayObserver',
'removeAt',
'removeObject',
'removeObjects',
'replace',
'reverseObjects',
'setEach',
'setObjects',
'shiftObject',
'slice',
'sortBy',
'toArray',
'uniq',
'uniqBy',
'unshiftObject',
'unshiftObjects',
'without',
];
InheritedProxyMethods.forEach((method) => {
PromiseManyArray.prototype[method] = function proxiedMethod(...args) {
assert(`Cannot call ${method} before content is assigned.`, this.content);
return this.content[method](...args);
};
});
if (DEPRECATE_EVENTED_API_USAGE) {
['on', 'has', 'trigger', 'off', 'one'].forEach((method) => {
PromiseManyArray.prototype[method] = function proxiedMethod(...args) {
assert(`Cannot call ${method} before content is assigned.`, this.content);
return this.content[method](...args);
};
});
}