ig-types 6.24.18 → 6.24.21
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 +61 -22
- package/package.json +1 -1
- package/runner.js +32 -12
- package/test.js +30 -2
package/Promise.js
CHANGED
|
@@ -35,10 +35,53 @@ var generator = require('./generator')
|
|
|
35
35
|
|
|
36
36
|
|
|
37
37
|
//---------------------------------------------------------------------
|
|
38
|
-
// XXX EXPERIMENTING...
|
|
39
38
|
|
|
40
39
|
// packed format API...
|
|
41
40
|
//
|
|
41
|
+
// This is separate from IterablePromise to enable low-level testing and
|
|
42
|
+
// experimentation.
|
|
43
|
+
//
|
|
44
|
+
// Packed format
|
|
45
|
+
// [
|
|
46
|
+
// <value>,
|
|
47
|
+
//
|
|
48
|
+
// [
|
|
49
|
+
// <value>,
|
|
50
|
+
//
|
|
51
|
+
// <promise( <value> )>,
|
|
52
|
+
//
|
|
53
|
+
// ..
|
|
54
|
+
// ],
|
|
55
|
+
//
|
|
56
|
+
// <promise( <value> )>,
|
|
57
|
+
//
|
|
58
|
+
// <promise( [ <value>, .. ] )>,
|
|
59
|
+
//
|
|
60
|
+
// ..
|
|
61
|
+
// ]
|
|
62
|
+
//
|
|
63
|
+
// <packed> ::=
|
|
64
|
+
// <value>
|
|
65
|
+
// | <packed-array>
|
|
66
|
+
// | <promise( <value> )>
|
|
67
|
+
// | <promise( <packed-array> )>
|
|
68
|
+
//
|
|
69
|
+
// <packed-array> ::=
|
|
70
|
+
// [ <packed-item>, .. ]
|
|
71
|
+
//
|
|
72
|
+
// <packed-item> ::=
|
|
73
|
+
// <value>
|
|
74
|
+
// | [ <nested-value>, .. ]
|
|
75
|
+
// | <promise( <value> )>
|
|
76
|
+
// | <promise( [ <value>, .. ] )>
|
|
77
|
+
//
|
|
78
|
+
// <nested-value> ::=
|
|
79
|
+
// <value>
|
|
80
|
+
// | <promise( <value> )>
|
|
81
|
+
//
|
|
82
|
+
//
|
|
83
|
+
//
|
|
84
|
+
// XXX revise bnf...
|
|
42
85
|
// XXX should this be a container or just a set of function???
|
|
43
86
|
// ...for simplicity I'll keep it a stateless set of functions for
|
|
44
87
|
// now, to avoid yet another layer of indirection -- IterablePromise...
|
|
@@ -50,19 +93,6 @@ module.packed =
|
|
|
50
93
|
// pack(<promise>)
|
|
51
94
|
// -> <packed>
|
|
52
95
|
//
|
|
53
|
-
// <packed> ::=
|
|
54
|
-
// <packed-array>
|
|
55
|
-
// | <packed-promise>
|
|
56
|
-
//
|
|
57
|
-
// <packed-array> ::=
|
|
58
|
-
// [
|
|
59
|
-
// <item>
|
|
60
|
-
// | <items> - array
|
|
61
|
-
// | <promise-item>
|
|
62
|
-
// | <promise-items>,
|
|
63
|
-
// ...
|
|
64
|
-
// ]
|
|
65
|
-
//
|
|
66
96
|
// NOTE: when all promises are expanded the packed array can be unpacked
|
|
67
97
|
// by simply calling .flat()
|
|
68
98
|
// NOTE: if 'types/Array' is imported this will support throwing STOP
|
|
@@ -72,14 +102,14 @@ module.packed =
|
|
|
72
102
|
// of order, as the nested promises resolve and thus throwing
|
|
73
103
|
// stop will stop the handlers not yet run and not the next
|
|
74
104
|
// handlers in sequence.
|
|
75
|
-
//
|
|
76
|
-
// XXX see if we should self-expand promises...
|
|
77
|
-
// XXX migrate these back into InteractivePromise...
|
|
78
105
|
// XXX does this need onerror(..) ???
|
|
106
|
+
// ...essentially this can make sense only in generator expansion,
|
|
107
|
+
// but not sure if this should be done here as it is sync...
|
|
79
108
|
pack: function(list){
|
|
80
109
|
var that = this
|
|
81
110
|
// list: promise...
|
|
82
111
|
// XXX should we just test for typeof(list.then) == 'function'???
|
|
112
|
+
// XXX should we expand async generators here???
|
|
83
113
|
if(list instanceof Promise
|
|
84
114
|
// list: async promise...
|
|
85
115
|
|| (typeof(list) == 'object'
|
|
@@ -92,18 +122,28 @@ module.packed =
|
|
|
92
122
|
&& !list.map
|
|
93
123
|
&& Symbol.iterator in list){
|
|
94
124
|
list = [...list] }
|
|
125
|
+
/* XXX on one hand this should be here and on the other I'm not
|
|
126
|
+
// sure how are we going to thread handler and onerror to
|
|
127
|
+
// here...
|
|
128
|
+
// list: promise / async-iterator...
|
|
129
|
+
if(typeof(list) == 'object'
|
|
130
|
+
&& Symbol.asyncIterator in list){
|
|
131
|
+
return list
|
|
132
|
+
// XXX
|
|
133
|
+
.iter(handler, onerror)
|
|
134
|
+
.then(function(list){
|
|
135
|
+
return that.pack(list) }) }
|
|
136
|
+
//*/
|
|
95
137
|
// list: non-array / non-iterable...
|
|
96
138
|
if(typeof(list.map) != 'function'){
|
|
97
139
|
list = [list].flat() }
|
|
98
140
|
// list: array...
|
|
99
|
-
// XXX should we self-expand this???
|
|
100
141
|
return list
|
|
101
142
|
.map(function(elem){
|
|
102
143
|
// XXX should we expand generators here???
|
|
103
144
|
return elem instanceof Array ?
|
|
104
145
|
[elem]
|
|
105
146
|
// XXX should we just test for .then(..)???
|
|
106
|
-
// XXX should we self-expand this???
|
|
107
147
|
: elem instanceof Promise ?
|
|
108
148
|
elem.then(function(elem){
|
|
109
149
|
return elem instanceof Array ?
|
|
@@ -114,9 +154,6 @@ module.packed =
|
|
|
114
154
|
// handle(<packed>, <handler>[, <onerror>])
|
|
115
155
|
// -> <packed>
|
|
116
156
|
//
|
|
117
|
-
// XXX revise nested promise handling...
|
|
118
|
-
// ...something like simplify(<packed>) -> <packed> ???
|
|
119
|
-
// XXX STOP_PROMISED_HANDLERS -- TEST
|
|
120
157
|
handle: function(packed, handler, onerror){
|
|
121
158
|
var that = this
|
|
122
159
|
var handlers = [...arguments].slice(1)
|
|
@@ -230,6 +267,8 @@ module.packed =
|
|
|
230
267
|
}
|
|
231
268
|
|
|
232
269
|
|
|
270
|
+
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
|
271
|
+
|
|
233
272
|
//
|
|
234
273
|
// itemResolved(<list>, <onupdate>[, <ondone>])
|
|
235
274
|
// itemResolved(<list>, <options>)
|
package/package.json
CHANGED
package/runner.js
CHANGED
|
@@ -12,6 +12,8 @@
|
|
|
12
12
|
* - ...
|
|
13
13
|
* would be nice to make the task just a slightly extended or better
|
|
14
14
|
* defined function/generator, ideally to make them interchangable...
|
|
15
|
+
* XXX BUG: there seems to be an odd bug somplace here where each item
|
|
16
|
+
* gets processed once per "thread"... (ImageGrid.Viewer export)
|
|
15
17
|
*
|
|
16
18
|
**********************************************/ /* c8 ignore next 2 */
|
|
17
19
|
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
|
|
@@ -360,6 +362,7 @@ object.Constructor('Queue', Array, {
|
|
|
360
362
|
// report...
|
|
361
363
|
that.trigger('taskCompleted', task, res) }
|
|
362
364
|
var fail = {doc: 'fail runningDone(..)'}
|
|
365
|
+
// XXX should this also remove .__running when empty???
|
|
363
366
|
var runningDone = function(mode){
|
|
364
367
|
running.splice(0, running.length,
|
|
365
368
|
// NOTE: there can be multiple occurrences of res...
|
|
@@ -409,7 +412,7 @@ object.Constructor('Queue', Array, {
|
|
|
409
412
|
// collect results...
|
|
410
413
|
this.collect_results
|
|
411
414
|
&& res !== module.SKIP
|
|
412
|
-
&& (this.__results = this.__results
|
|
415
|
+
&& (this.__results = this.__results ?? []).push(res)
|
|
413
416
|
|
|
414
417
|
// handle task results...
|
|
415
418
|
//
|
|
@@ -516,6 +519,9 @@ object.Constructor('Queue', Array, {
|
|
|
516
519
|
//
|
|
517
520
|
// NOTE: this helps get around the argument number limitation in JS...
|
|
518
521
|
add: function(tasks){
|
|
522
|
+
tasks = tasks instanceof Array ?
|
|
523
|
+
tasks
|
|
524
|
+
: [tasks]
|
|
519
525
|
// handle too large a number of args...
|
|
520
526
|
var MAX_ARGS = 10000
|
|
521
527
|
if(tasks.length > MAX_ARGS){
|
|
@@ -568,37 +574,51 @@ module.FinalizableQueue =
|
|
|
568
574
|
object.Constructor('FinalizableQueue', Queue, {
|
|
569
575
|
auto_stop: true,
|
|
570
576
|
|
|
577
|
+
// not for direct use...
|
|
578
|
+
// NOTE: we are intentionally not freezing .__results to allow them
|
|
579
|
+
// to be consumed by the client...
|
|
580
|
+
// XXX is this correct???
|
|
581
|
+
__freeze: function(){
|
|
582
|
+
Object.freeze(this)
|
|
583
|
+
return this },
|
|
584
|
+
|
|
571
585
|
__onempty__: function(){
|
|
572
586
|
return this.trigger('done') },
|
|
573
587
|
|
|
574
588
|
done: events.Event('done', function(handle){
|
|
575
589
|
// abort only once...
|
|
576
|
-
if(this.state == 'aborted'
|
|
590
|
+
if(this.state == 'aborted'
|
|
591
|
+
|| this.state == 'done'){
|
|
577
592
|
return handle(false) }
|
|
578
593
|
this.__state = 'done'
|
|
579
|
-
|
|
594
|
+
this.__freeze() }),
|
|
580
595
|
abort: events.Event('abort', function(handle){
|
|
581
596
|
// abort only once...
|
|
582
|
-
if(this.state == 'aborted'
|
|
597
|
+
if(this.state == 'aborted'
|
|
598
|
+
|| this.state == 'done'){
|
|
583
599
|
return handle(false) }
|
|
584
600
|
this.__state = 'aborted'
|
|
585
|
-
|
|
601
|
+
this.__freeze() }),
|
|
586
602
|
|
|
587
603
|
// NOTE: each handler will get called once when the next time the
|
|
588
604
|
// queue is emptied...
|
|
589
605
|
promise: function(){
|
|
606
|
+
return this.__promise },
|
|
607
|
+
then: function(onresolve, onreject){
|
|
608
|
+
return this.promise().then(...arguments) },
|
|
609
|
+
catch: function(onreject){
|
|
610
|
+
return this.promise().catch(...arguments) },
|
|
611
|
+
|
|
612
|
+
__init__: function(options){
|
|
590
613
|
var that = this
|
|
591
|
-
|
|
614
|
+
this.__promise = new Promise(function(resolve, reject){
|
|
592
615
|
that
|
|
593
616
|
.one('done', function(){
|
|
594
617
|
resolve(...(that.collect_results ?
|
|
595
|
-
[that.__results
|
|
618
|
+
[that.__results ?? []]
|
|
596
619
|
: [])) })
|
|
597
|
-
.one('abort', reject) })
|
|
598
|
-
|
|
599
|
-
return this.promise().then(...arguments) },
|
|
600
|
-
catch: function(onreject){
|
|
601
|
-
return this.promise().catch(...arguments) },
|
|
620
|
+
.one('abort', reject) })
|
|
621
|
+
return object.parentCall(FinalizableQueue.prototype.__init__, this, ...arguments) },
|
|
602
622
|
})
|
|
603
623
|
|
|
604
624
|
|
package/test.js
CHANGED
|
@@ -145,16 +145,44 @@ var cases = test.Cases({
|
|
|
145
145
|
[1, 2, 3]) },
|
|
146
146
|
promise_nested_array_mixed: function(assert){
|
|
147
147
|
return create(assert,
|
|
148
|
-
Promise.resolve([
|
|
148
|
+
Promise.resolve([
|
|
149
|
+
1,
|
|
150
|
+
Promise.resolve(2),
|
|
151
|
+
[3],
|
|
152
|
+
Promise.resolve([4]),
|
|
153
|
+
]),
|
|
149
154
|
[1, 2, [3], [4]]) },
|
|
150
155
|
})
|
|
151
156
|
this.Modifier({
|
|
152
157
|
nest: function(assert, setup){
|
|
153
158
|
setup.output = Promise.iter(setup.output)
|
|
154
159
|
return setup },
|
|
155
|
-
|
|
160
|
+
iter_noargs: function(assert, setup){
|
|
156
161
|
setup.output = setup.output.iter()
|
|
157
162
|
return setup },
|
|
163
|
+
iter_asis: function(assert, setup){
|
|
164
|
+
setup.output = setup.output
|
|
165
|
+
.iter(function(e){
|
|
166
|
+
return [e] })
|
|
167
|
+
return setup },
|
|
168
|
+
iter_clear: function(assert, setup){
|
|
169
|
+
return {
|
|
170
|
+
input: [],
|
|
171
|
+
output: setup.output
|
|
172
|
+
.iter(function(e){
|
|
173
|
+
return [] }),
|
|
174
|
+
} },
|
|
175
|
+
/* XXX does not account for promises as input...
|
|
176
|
+
iter_flat: async function(assert, setup){
|
|
177
|
+
return {
|
|
178
|
+
input: setup.input instanceof Array ?
|
|
179
|
+
(await Promise.all(setup.input)).flat()
|
|
180
|
+
: [await setup.input],
|
|
181
|
+
output: setup.output
|
|
182
|
+
.iter(function(e){
|
|
183
|
+
return e }),
|
|
184
|
+
} },
|
|
185
|
+
//*/
|
|
158
186
|
|
|
159
187
|
map_asis: function(assert, setup){
|
|
160
188
|
setup.output = setup.output
|