ig-types 6.24.12 → 6.24.14

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 +59 -64
  2. package/package.json +1 -1
  3. package/test.js +27 -0
package/Promise.js CHANGED
@@ -325,38 +325,42 @@ object.Constructor('IterablePromise', Promise, {
325
325
  'smap'
326
326
  : 'map'
327
327
  var stop = false
328
+ // XXX do we handle generators here???
329
+ var each = function(elem){
330
+ return elem instanceof Array ?
331
+ elem
332
+ .map(handler)
333
+ .flat()
334
+ : handler(elem) }
328
335
  return list
329
336
  [map](
330
337
  function(elem){
338
+ // NOTE: we are calling .flat() on the result so we
339
+ // need to keep a handled array as a single
340
+ // element by wrapping the return of handled(..)...
331
341
  return elem instanceof IterablePromise ?
332
- // XXX should this be expanded??? (like Array below)
333
342
  (elem.isSync() ?
334
- handler(elem.sync())
335
- // XXX need to handle this but keep it IterablePromise...
336
- : elem.iterthen(handler))
343
+ each(elem.sync())
344
+ : elem.iterthen(each))
345
+ // sync sync promise...
337
346
  : (elem instanceof SyncPromise
338
347
  && !(elem.sync() instanceof Promise)) ?
339
- // XXX should this be expanded??? (like Array below)
340
- handler(elem.sync())
348
+ [each(elem.sync())]
341
349
  // promise / promise-like...
342
350
  : elem && elem.then ?
343
351
  // NOTE: when this is explicitly stopped we
344
- // do not call any more handlers...
345
- // XXX TEST!!!
352
+ // do not call any more handlers after
353
+ // STOP is thrown/returned...
354
+ // NOTE: the promise protects this from .flat()
346
355
  elem.then(function(elem){
347
356
  return !stop ?
348
- handler(
349
- elem.length == 1 ?
350
- elem[0]
351
- :elem)
357
+ each(elem)
352
358
  : [] })
353
359
  : elem instanceof Array ?
354
- [handler(
355
- elem.length == 1 ?
356
- elem[0]
357
- : elem)]
358
- : handler(elem) },
359
- function(res){
360
+ [each(elem)]
361
+ : each(elem) },
362
+ // handle STOP...
363
+ function(){
360
364
  stop = true })
361
365
  .flat() },
362
366
  //*/
@@ -756,7 +760,8 @@ object.Constructor('IterablePromise', Promise, {
756
760
  // instance...
757
761
  var promise
758
762
  var obj = Reflect.construct(
759
- IterablePromise.__proto__,
763
+ Promise,
764
+ //this.constructor.__proto__,
760
765
  [function(resolve, reject){
761
766
  // NOTE: this is here for Promise compatibility...
762
767
  if(typeof(list) == 'function'){
@@ -765,13 +770,14 @@ object.Constructor('IterablePromise', Promise, {
765
770
  if(list === false){
766
771
  return reject() }
767
772
  promise = {resolve, reject} }],
768
- IterablePromise)
773
+ this.constructor)
769
774
 
770
775
  // populate new instance...
771
776
  if(promise){
772
777
  // handle/pack input data...
773
778
  if(handler != 'raw'){
774
- list = list instanceof IterablePromise ?
779
+ //list = list instanceof IterablePromise ?
780
+ list = list instanceof this.constructor ?
775
781
  obj.__handle(list.__packed, handler, onerror)
776
782
  : obj.__pack(list, handler, onerror) }
777
783
  Object.defineProperty(obj, '__packed', {
@@ -811,53 +817,42 @@ object.Constructor('IterablePromise', Promise, {
811
817
  //---------------------------------------------------------------------
812
818
  // XXX EXPEREMENTAL/HACK...
813
819
 
814
- // XXX we are getting a list with promises triggered outside, we can't
815
- // control order of execution of this but we can control in what
816
- // order the handler is called...
817
- // XXX this potentially shows a bug with IterablePromise:
818
- // Promise.seqiter(
819
- // [1, Promise.resolve(2), 3, Promise.resolve(4)],
820
- // e => (console.log('---', e), e))
821
- // ...this will not resolve/replace the 4's promise...
822
- // for context:
823
- // await Promise.resolve(Promise.resolve(Promise.resolve(123)))
824
- // -> 123
825
- // ...is this just a nesting issue here???
826
- // Q: what IterablePromise does/should do if a promise resolves to
827
- // a promise multiple times...
828
820
  // XXX not sure if this is a viable strategy....
829
821
  var IterableSequentialPromise =
830
822
  module.IterableSequentialPromise =
831
823
  object.Constructor('IterableSequentialPromise', IterablePromise, {
832
- __new__: function(_, list, handler=undefined, onerror=undefined){
833
- var [_, list, ...rest] = arguments
834
- var res = list
835
- // format the list...
836
- if(list instanceof Array
837
- || list instanceof Promise){
838
-
839
- var pre_process = function(list, start=0){
840
- if(list instanceof Promise){
841
- return list.then(pre_process) }
842
- res = []
843
- for(let [i, e] of list.entries().slice(start)){
844
- if(e instanceof Promise){
845
- res.push(e.then(function(e){
846
- return [e,
847
- // XXX need to flatten this...
848
- ...pre_process(list, i+1)] }))
849
- break }
850
- res.push(e) }
851
- return res }
852
-
853
- res = pre_process(list)
854
-
855
- var obj = IterablePromise.prototype.__new__.call(this, _, res, 'raw')
856
-
857
- return obj
858
- }
859
- // XXX use .parentCall(..)...
860
- return IterablePromise.prototype.__new__.call(this, _, res, ...rest) },
824
+ // XXX needs more work...
825
+ __pack: function(list, handler=undefined, onerror=undefined){
826
+ var seqiter = this.constructor
827
+
828
+ var repack = function(list){
829
+ var res = []
830
+ for(var [i, e] of list.entries()){
831
+ // XXX check for .then(..) instead???
832
+ if(e instanceof Promise
833
+ // skip last promise -- nothing to wrap...
834
+ && i < list.length-1){
835
+ res.push(e
836
+ .then(function(e){
837
+ return seqiter([...e, ...list.slice(i+1)]) }))
838
+ break }
839
+ res.push(e) }
840
+ return res }
841
+
842
+ // NOTE: we are not handling the list here...
843
+ list = object.parentCall(IterableSequentialPromise.prototype.__pack, this, list)
844
+
845
+ list = list instanceof Array ?
846
+ repack(list)
847
+ // XXX check for .then(..) instead???
848
+ : list instanceof Promise ?
849
+ list.then(repack)
850
+ : list
851
+
852
+ return handler ?
853
+ // XXX this seems to be wrong...
854
+ this.__handle(list, handler, onerror)
855
+ : list },
861
856
  })
862
857
 
863
858
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.24.12",
3
+ "version": "6.24.14",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -265,6 +265,33 @@ var cases = test.Cases({
265
265
  await Promise.iter([1, Promise.resolve(2), 3]),
266
266
  [1, 2, 3],
267
267
  'promises as elements')
268
+
269
+ // XXX need a recursive assert...
270
+ var should_be = [ [1], [2], [3], [4], [5], [6] ]
271
+ var got = await Promise.iter([
272
+ [1,1,1],
273
+ Promise.sync.resolve([2,2,2]),
274
+ Promise.resolve([3,3,3]),
275
+ Promise.iter([4,4,4]),
276
+ Promise.iter.resolve([5,5,5]),
277
+ Promise.all([6,6,6]),
278
+ ],
279
+ function(e){
280
+ return e instanceof Array ?
281
+ [[ e[0] ]]
282
+ // XXX
283
+ : e })
284
+ assert(
285
+ should_be
286
+ .reduce(function(res, e, i){
287
+ //console.log('---', e, got[i])
288
+ if(res === false){
289
+ return false }
290
+ return e instanceof Array
291
+ && got[i] instanceof Array
292
+ && e.length == got[i].length
293
+ && e[0] == got[i][0] }, true),
294
+ 'pack/unpack:', got)
268
295
  },
269
296
 
270
297
  // Date.js