ig-types 6.24.14 → 6.24.16

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 +31 -6
  2. package/package.json +1 -1
  3. package/test.js +34 -26
package/Promise.js CHANGED
@@ -817,11 +817,31 @@ object.Constructor('IterablePromise', Promise, {
817
817
  //---------------------------------------------------------------------
818
818
  // XXX EXPEREMENTAL/HACK...
819
819
 
820
+ // This like IterablePromise but guarantees handler execution in order
821
+ // element occurrence.
822
+ //
823
+ // For comparison:
824
+ // Promise.all([ .. ]).then(func)
825
+ // - func is called on element list
826
+ // - func is called when all the elements are resolved
827
+ // Promise.iter([ .. ]).iter(func)
828
+ // - func per element
829
+ // - func is called when an element is resolved/ready
830
+ // in any order
831
+ // Promise.seqiter([ .. ]).iter(func)
832
+ // - func per element
833
+ // - func is called when an element is resolved/ready
834
+ // and when all elements before it are handled
835
+ //
836
+ // NOTE: that here a promise will block handling of later promises even
837
+ // if they are resolved before it.
838
+ //
839
+ // XXX check if this behaves correctly (call order) on concatenation and
840
+ // other methods...
820
841
  // XXX not sure if this is a viable strategy....
821
842
  var IterableSequentialPromise =
822
843
  module.IterableSequentialPromise =
823
844
  object.Constructor('IterableSequentialPromise', IterablePromise, {
824
- // XXX needs more work...
825
845
  __pack: function(list, handler=undefined, onerror=undefined){
826
846
  var seqiter = this.constructor
827
847
 
@@ -829,28 +849,33 @@ object.Constructor('IterableSequentialPromise', IterablePromise, {
829
849
  var res = []
830
850
  for(var [i, e] of list.entries()){
831
851
  // XXX check for .then(..) instead???
832
- if(e instanceof Promise
852
+ //if(e instanceof Promise
853
+ if(e.then
833
854
  // skip last promise -- nothing to wrap...
834
855
  && i < list.length-1){
835
856
  res.push(e
836
857
  .then(function(e){
837
- return seqiter([...e, ...list.slice(i+1)]) }))
858
+ return seqiter(
859
+ [e, ...list.slice(i+1)])
860
+ .flat() }))
838
861
  break }
839
862
  res.push(e) }
840
863
  return res }
841
864
 
842
865
  // NOTE: we are not handling the list here...
843
866
  list = object.parentCall(IterableSequentialPromise.prototype.__pack, this, list)
844
-
867
+ list = list instanceof SyncPromise ?
868
+ list.sync()
869
+ : list
845
870
  list = list instanceof Array ?
846
871
  repack(list)
847
872
  // XXX check for .then(..) instead???
848
- : list instanceof Promise ?
873
+ //: list instanceof Promise ?
874
+ : list.then ?
849
875
  list.then(repack)
850
876
  : list
851
877
 
852
878
  return handler ?
853
- // XXX this seems to be wrong...
854
879
  this.__handle(list, handler, onerror)
855
880
  : list },
856
881
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.24.14",
3
+ "version": "6.24.16",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -266,32 +266,40 @@ var cases = test.Cases({
266
266
  [1, 2, 3],
267
267
  'promises as elements')
268
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)
269
+ // XXX split this into separate cases...
270
+ for(var meth of ['iter', 'seqiter']){
271
+ // XXX need a recursive assert...
272
+ var should_be = [ [1], [2], [3], [4], [5], [6] ]
273
+ var got = await Promise[meth]([
274
+ [1,1,1],
275
+ Promise.sync.resolve([2,2,2]),
276
+ Promise.resolve([3,3,3]),
277
+ Promise.iter([4,4,4]),
278
+ Promise.iter.resolve([5,5,5]),
279
+ Promise.all([6,6,6]),
280
+ ],
281
+ function(e){
282
+ return e instanceof Array ?
283
+ [[ e[0] ]]
284
+ // XXX
285
+ : e })
286
+ assert(
287
+ should_be
288
+ .reduce(function(res, e, i){
289
+ //console.log('---', e, got[i])
290
+ if(res === false){
291
+ return false }
292
+ return e instanceof Array
293
+ && got[i] instanceof Array
294
+ && e.length == got[i].length
295
+ && e[0] == got[i][0] }, true),
296
+ 'pack/unpack', meth, got)
297
+
298
+ assert.array(
299
+ await Promise[meth]([1, Promise.resolve(2), Promise.resolve(3)]),
300
+ [1,2,3],
301
+ 'flat unpack', meth)
302
+ }
295
303
  },
296
304
 
297
305
  // Date.js