ig-types 6.15.0 → 6.15.1

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.
Files changed (3) hide show
  1. package/Promise.js +30 -25
  2. package/generator.js +43 -0
  3. package/package.json +1 -1
package/Promise.js CHANGED
@@ -5,8 +5,7 @@
5
5
  * Promise.iter(seq)
6
6
  * <promise>.iter()
7
7
  * Iterable promise object.
8
- * Similar to Promise.all(..) but adds basic iterator/generator
9
- * API and will resolve the items as they are ready (resolved).
8
+ * Similar to Promise.all(..) but adds basic iterator API.
10
9
  *
11
10
  * Promise.interactive(handler)
12
11
  * Interactive promise object.
@@ -39,16 +38,6 @@ var object = require('ig-object')
39
38
  // Like Promise.all(..) but adds ability to iterate through results
40
39
  // via generators .map(..)/.reduce(..) and friends...
41
40
  //
42
- // NOTE: it would be nice to support throwing STOP from the iterable
43
- // promise but...
44
- // - this is more complicated than simply using .smap(..) instead
45
- // of .map(..) because the list can contain async promises...
46
- // ...would need to wrap each .then(..) call in try-catch and
47
- // manually handle the stop...
48
- // ...then there's a question of derivative iterators etc.
49
- // - another issue here is that the stop would happen in order of
50
- // execution and not order of elements...
51
- // XXX EXPEREMENTAL: STOP...
52
41
  // NOTE: the following can not be implemented here:
53
42
  // .splice(..) - can't both modify and return
54
43
  // a result...
@@ -68,18 +57,18 @@ var object = require('ig-object')
68
57
  // back to the complex IterablePromise...
69
58
  //
70
59
  // XXX how do we handle errors/rejections???
60
+ // ...mostly the current state is OK, but need more testing...
71
61
  //
72
62
 
73
- // XXX should these be exported???
74
63
  var iterPromiseProxy =
75
- //module.iterPromiseProxy =
64
+ module.iterPromiseProxy =
76
65
  function(name){
77
66
  return function(...args){
78
67
  return this.constructor(
79
68
  this.then(function(lst){
80
69
  return lst[name](...args) })) } }
81
70
  var promiseProxy =
82
- //module.promiseProxy =
71
+ module.promiseProxy =
83
72
  function(name){
84
73
  return async function(...args){
85
74
  return (await this)[name](...args) } }
@@ -87,7 +76,6 @@ function(name){
87
76
  var IterablePromise =
88
77
  module.IterablePromise =
89
78
  object.Constructor('IterablePromise', Promise, {
90
- // XXX EXPEREMENTAL: STOP...
91
79
  get STOP(){
92
80
  return Array.STOP },
93
81
 
@@ -210,6 +198,20 @@ object.Constructor('IterablePromise', Promise, {
210
198
  : handler(elem) }) },
211
199
  //*/
212
200
  // transform/handle packed array (sync)...
201
+ //
202
+ // XXX BUG:
203
+ // await Promise.iter(['a', Promise.resolve(222), ['c']])
204
+ // .map(async e => e)
205
+ // -> ['a', <promise>, ['c']]
206
+ // is not the same as:
207
+ // await Promise.iter(['a', Promise.resolve(222), ['c']])
208
+ // .map(e => e)
209
+ // -> ['a', 222, ['c']]
210
+ // ...and these works as expected:
211
+ // await Promise.iter(['a', Promise.resolve(222), ['c']], async e => [e])
212
+ // -> ['a', 222, ['c']]
213
+ // await Promise.iter(['a', Promise.resolve(222), ['c']], e => [e])
214
+ // -> ['a', 222, ['c']]
213
215
  __handle: function(list, handler=undefined){
214
216
  var that = this
215
217
  if(typeof(list) == 'function'){
@@ -237,8 +239,9 @@ object.Constructor('IterablePromise', Promise, {
237
239
  that.__pack(elem, handler)
238
240
  : elem instanceof Promise ?
239
241
  that.__pack(elem, handler)
240
- .then(function(elem){
241
- return elem.flat() })
242
+ //.then(function(elem){
243
+ .then(function([elem]){
244
+ return elem })
242
245
  : [handler(elem)] })
243
246
  .flat() },
244
247
  // unpack array (async)...
@@ -367,6 +370,7 @@ object.Constructor('IterablePromise', Promise, {
367
370
  var list = this.__packed
368
371
  return ((i != 0 && i != -1)
369
372
  || list instanceof Promise
373
+ // XXX not sure if this is correct...
370
374
  || list.at(i) instanceof Promise) ?
371
375
  (await this).at(i)
372
376
  // NOTE: we can only reason about first/last explicit elements,
@@ -384,7 +388,6 @@ object.Constructor('IterablePromise', Promise, {
384
388
  // NOTE: unlike .reduce(..) this needs the parent fully resolved
385
389
  // to be able to iterate from the end.
386
390
  // XXX is it faster to do .reverse().reduce(..) ???
387
- // XXX ???
388
391
  reduceRight: promiseProxy('reduceRight'),
389
392
 
390
393
  // NOTE: there is no way we can do a sync generator returning
@@ -465,6 +468,12 @@ object.Constructor('IterablePromise', Promise, {
465
468
  every: promiseProxy('every'),
466
469
 
467
470
 
471
+ // this is defined globally as Promise.prototype.iter(..)
472
+ //
473
+ // for details see: PromiseMixin(..) below...
474
+ //iter: function(handler=undefined){ ... },
475
+
476
+
468
477
  // promise api...
469
478
  //
470
479
  // Overload .then(..), .catch(..) and .finally(..) to return a plain
@@ -492,12 +501,6 @@ object.Constructor('IterablePromise', Promise, {
492
501
  : p },
493
502
 
494
503
 
495
- // this is defined globally as Promise.prototype.iter(.,)
496
- //
497
- // for details see: PromiseMixin(..) below...
498
- //iter: function(handler=undefined){ ... },
499
-
500
-
501
504
  // constructor...
502
505
  //
503
506
  // Promise.iter([ .. ])
@@ -518,6 +521,7 @@ object.Constructor('IterablePromise', Promise, {
518
521
  // items...
519
522
  // XXX we can make the index a promise, then if the client needs
520
523
  // the value they can wait for it...
524
+ // ...this may be quite an overhead...
521
525
  //
522
526
  //
523
527
  // Special cases useful for extending this constructor...
@@ -654,6 +658,7 @@ object.Constructor('InteractivePromise', Promise, {
654
658
  // Cooperative promise...
655
659
  //
656
660
  // A promise that can be resolved/rejected externally.
661
+ //
657
662
  // NOTE: normally this has no internal resolver logic...
658
663
  //
659
664
 
package/generator.js CHANGED
@@ -66,6 +66,14 @@ module.Generator =
66
66
  (function*(){}).constructor
67
67
 
68
68
 
69
+ var AsyncGeneratorPrototype =
70
+ (async function*(){}).constructor.prototype
71
+
72
+ var AsyncGenerator =
73
+ module.AsyncGenerator =
74
+ (async function*(){}).constructor
75
+
76
+
69
77
  // base iterator prototypes...
70
78
  var ITERATOR_PROTOTYPES = [
71
79
  Array,
@@ -428,6 +436,41 @@ ITERATOR_PROTOTYPES
428
436
  GeneratorProtoMixin(proto) })
429
437
 
430
438
 
439
+ // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
440
+ // XXX EXPERIMENTAL...
441
+
442
+ var makeAsyncGenerator = function(name, pre){
443
+ return function(...args){
444
+ var that = this
445
+ return Object.assign(
446
+ async function*(){
447
+ var a = pre ?
448
+ pre.call(this, args, ...arguments)
449
+ : args
450
+ for await (var e of that(...arguments)[name](...a)){
451
+ yield e } },
452
+ { toString: function(){
453
+ return [
454
+ that.toString(),
455
+ // XXX need to normalize args better...
456
+ `.${ name }(${ args.join(', ') })`,
457
+ ].join('\n ') }, }) } }
458
+
459
+ var AsyncGeneratorMixin =
460
+ module.AsyncGeneratorMixin =
461
+ object.Mixin('AsyncGeneratorMixin', 'soft', {
462
+ map: makeAsyncGenerator('map'),
463
+ })
464
+
465
+ var AsyncGeneratorProtoMixin =
466
+ module.AsyncGeneratorProtoMixin =
467
+ object.Mixin('AsyncGeneratorProtoMixin', 'soft', {
468
+ })
469
+
470
+ //AsyncGeneratorMixin(AsyncGeneratorPrototype)
471
+ //AsyncGeneratorProtoMixin(AsyncGeneratorPrototype.prototype)
472
+
473
+
431
474
 
432
475
  //---------------------------------------------------------------------
433
476
  // Generators...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.15.0",
3
+ "version": "6.15.1",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {