ig-types 6.13.2 → 6.13.4
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/Promise.js +59 -34
- package/package.json +1 -1
- package/test.js +17 -0
package/Promise.js
CHANGED
|
@@ -58,6 +58,7 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
58
58
|
//
|
|
59
59
|
// Format:
|
|
60
60
|
// [
|
|
61
|
+
// <non-array-value>,
|
|
61
62
|
// [ <value> ],
|
|
62
63
|
// <promise>,
|
|
63
64
|
// ...
|
|
@@ -66,24 +67,33 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
66
67
|
__list: null,
|
|
67
68
|
|
|
68
69
|
// low-level .__list handlers/helpers...
|
|
70
|
+
//
|
|
69
71
|
// NOTE: these can be useful for debugging and extending...
|
|
70
|
-
|
|
72
|
+
__pack: function(list, handler){
|
|
73
|
+
var that = this
|
|
74
|
+
// handle iterable promise list...
|
|
75
|
+
if(list instanceof IterablePromise){
|
|
76
|
+
return this.__handle(list.__list, handler) }
|
|
71
77
|
// handle promise list...
|
|
72
78
|
if(list instanceof Promise){
|
|
73
|
-
var that = this
|
|
74
79
|
return list.then(function(list){
|
|
75
|
-
return that.
|
|
76
|
-
|
|
80
|
+
return that.__pack(list, handler) }) }
|
|
81
|
+
// do the work...
|
|
82
|
+
// NOTE: packing and handling are mixed here because it's faster
|
|
83
|
+
// to do them both on a single list traverse...
|
|
77
84
|
var handle = !!handler
|
|
78
85
|
handler = handler
|
|
79
86
|
?? function(elem){
|
|
80
87
|
return [elem] }
|
|
81
88
|
return [list].flat()
|
|
82
89
|
.map(function(elem){
|
|
83
|
-
return elem
|
|
84
|
-
|
|
85
|
-
: elem && elem.then ?
|
|
90
|
+
return elem && elem.then ?
|
|
91
|
+
//that.__pack(elem, handler)
|
|
86
92
|
elem.then(handler)
|
|
93
|
+
: elem instanceof Array ?
|
|
94
|
+
handler(elem)
|
|
95
|
+
// NOTE: we keep things that do not need protecting
|
|
96
|
+
// from .flat() as-is...
|
|
87
97
|
: !handle ?
|
|
88
98
|
elem
|
|
89
99
|
: handler(elem) }) },
|
|
@@ -98,25 +108,28 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
98
108
|
if(list instanceof Promise){
|
|
99
109
|
return list.then(function(list){
|
|
100
110
|
return that.__handle(list, handler) }) }
|
|
101
|
-
|
|
111
|
+
// do the work...
|
|
112
|
+
// NOTE: since each section of the packed .__array is the same
|
|
113
|
+
// structure as the input we'll use .__pack(..) to handle
|
|
114
|
+
// them, this also keeps all the handling code in one place.
|
|
102
115
|
return list.map(function(elem){
|
|
103
116
|
return elem instanceof Array ?
|
|
104
|
-
that.
|
|
117
|
+
that.__pack(elem, handler)
|
|
105
118
|
: elem instanceof Promise ?
|
|
106
|
-
that.
|
|
119
|
+
that.__pack(elem, handler)
|
|
107
120
|
.then(function(elem){
|
|
108
121
|
return elem.flat() })
|
|
109
|
-
: handler(elem) })
|
|
122
|
+
: [handler(elem)] })
|
|
110
123
|
.flat() },
|
|
111
|
-
|
|
124
|
+
__unpack: function(list){
|
|
112
125
|
list = list
|
|
113
126
|
?? this.__list
|
|
114
127
|
// handle promise list...
|
|
115
128
|
if(list instanceof Promise){
|
|
116
129
|
var that = this
|
|
117
130
|
return list.then(function(list){
|
|
118
|
-
return that.
|
|
119
|
-
|
|
131
|
+
return that.__unpack(list) }) }
|
|
132
|
+
// do the work...
|
|
120
133
|
return Promise.all(list)
|
|
121
134
|
.then(function(list){
|
|
122
135
|
return list.flat() }) },
|
|
@@ -155,7 +168,6 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
155
168
|
map: function(func){
|
|
156
169
|
return this.constructor(this,
|
|
157
170
|
function(e){
|
|
158
|
-
//return [func(e)] }) },
|
|
159
171
|
var res = func(e)
|
|
160
172
|
return res instanceof Promise ?
|
|
161
173
|
res.then(function(e){
|
|
@@ -164,18 +176,14 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
164
176
|
filter: function(func){
|
|
165
177
|
return this.constructor(this,
|
|
166
178
|
function(e){
|
|
167
|
-
//return func(e) ?
|
|
168
|
-
// [e]
|
|
169
|
-
// : [] }) },
|
|
170
179
|
var res = func(e)
|
|
180
|
+
var _filter = function(elem){
|
|
181
|
+
return res ?
|
|
182
|
+
[elem]
|
|
183
|
+
: [] }
|
|
171
184
|
return res instanceof Promise ?
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
[e]
|
|
175
|
-
: [] })
|
|
176
|
-
: res ?
|
|
177
|
-
[e]
|
|
178
|
-
: [] }) },
|
|
185
|
+
res.then(_filter)
|
|
186
|
+
: _filter(e) }) },
|
|
179
187
|
// NOTE: this does not return an iterable promise as we can't know
|
|
180
188
|
// what the user reduces to...
|
|
181
189
|
// XXX we could look at the initial state though...
|
|
@@ -231,13 +239,29 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
231
239
|
.reverse(),
|
|
232
240
|
'raw') },
|
|
233
241
|
|
|
234
|
-
// NOTE:
|
|
235
|
-
// promise...
|
|
242
|
+
// NOTE: the following methods can create an unresolved promise from
|
|
243
|
+
// a resolved promise...
|
|
236
244
|
concat: function(other){
|
|
245
|
+
var that = this
|
|
246
|
+
var cur = this.__pack(this)
|
|
247
|
+
var other = this.__pack(other)
|
|
237
248
|
return this.constructor(
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
249
|
+
// NOTE: we are not using only the first branch to keep the
|
|
250
|
+
// lists as exposed as possible thus avoiding blocking
|
|
251
|
+
// until the whole ting is resolved...
|
|
252
|
+
(cur instanceof Promise
|
|
253
|
+
&& other instanceof Promise) ?
|
|
254
|
+
Promise.all([cur, other])
|
|
255
|
+
.then(function(list){
|
|
256
|
+
return list[0].concat(list[1]) })
|
|
257
|
+
: cur instanceof Promise ?
|
|
258
|
+
cur.then(function(list){
|
|
259
|
+
return list.concat(other) })
|
|
260
|
+
: other instanceof Promise ?
|
|
261
|
+
other.then(function(list){
|
|
262
|
+
return cur.concat(list) })
|
|
263
|
+
: cur.concat(other),
|
|
264
|
+
'raw') },
|
|
241
265
|
push: function(elem){
|
|
242
266
|
return this.concat([elem]) },
|
|
243
267
|
unshift: function(elem){
|
|
@@ -248,6 +272,7 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
248
272
|
// .pop()
|
|
249
273
|
// .shift()
|
|
250
274
|
// .first() / .last()
|
|
275
|
+
// .at(..)
|
|
251
276
|
// ...would be nice if these could stop everything that's not
|
|
252
277
|
// needed to execute...
|
|
253
278
|
|
|
@@ -359,7 +384,7 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
359
384
|
if(handler != 'raw'){
|
|
360
385
|
list = list instanceof IterablePromise ?
|
|
361
386
|
this.__handle(list.__list, handler)
|
|
362
|
-
: this.
|
|
387
|
+
: this.__pack(list, handler) }
|
|
363
388
|
|
|
364
389
|
Object.defineProperty(obj, '__list', {
|
|
365
390
|
value: list,
|
|
@@ -367,7 +392,7 @@ object.Constructor('IterablePromise', Promise, {
|
|
|
367
392
|
})
|
|
368
393
|
|
|
369
394
|
// handle promise state...
|
|
370
|
-
this.
|
|
395
|
+
this.__unpack(list)
|
|
371
396
|
.then(function(list){
|
|
372
397
|
promise.resolve(list) })
|
|
373
398
|
.catch(promise.reject) }
|
|
@@ -577,8 +602,8 @@ object.Mixin('PromiseProtoMixin', 'soft', {
|
|
|
577
602
|
as: ProxyPromise,
|
|
578
603
|
|
|
579
604
|
// XXX
|
|
580
|
-
iter: function(){
|
|
581
|
-
return IterablePromise(this) },
|
|
605
|
+
iter: function(handler){
|
|
606
|
+
return IterablePromise(this, handler) },
|
|
582
607
|
})
|
|
583
608
|
|
|
584
609
|
PromiseProtoMixin(Promise.prototype)
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -183,6 +183,23 @@ var cases = test.Cases({
|
|
|
183
183
|
.filter(function(e){ return false }),
|
|
184
184
|
} },
|
|
185
185
|
|
|
186
|
+
/* XXX need tuning...
|
|
187
|
+
concat_basic: function(assert, {input, output}){
|
|
188
|
+
return {
|
|
189
|
+
input: [input].flat()
|
|
190
|
+
.concat(['a', 'b', 'c']),
|
|
191
|
+
output: output
|
|
192
|
+
.concat(['a', 'b', 'c']),
|
|
193
|
+
} },
|
|
194
|
+
concat_nested_array: function(assert, {input, output}){
|
|
195
|
+
return {
|
|
196
|
+
input: [input].flat()
|
|
197
|
+
.concat(['a', ['b'], 'c']),
|
|
198
|
+
output: output
|
|
199
|
+
.concat(['a', ['b'], 'c']),
|
|
200
|
+
} },
|
|
201
|
+
//*/
|
|
202
|
+
|
|
186
203
|
})
|
|
187
204
|
this.Test({
|
|
188
205
|
value: async function(assert, {input, output}){
|