ig-types 6.24.5 → 6.24.8

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 CHANGED
@@ -30,7 +30,7 @@
30
30
 
31
31
  var object = require('ig-object')
32
32
 
33
- //var generator = require('./generator')
33
+ var generator = require('./generator')
34
34
 
35
35
 
36
36
 
@@ -123,20 +123,50 @@ object.Constructor('IterablePromise', Promise, {
123
123
  // stop will stop the handlers not yet run and not the next
124
124
  // handlers in sequence.
125
125
  // XXX EXPEREMENTAL: STOP...
126
- // XXX add support for async generators...
127
- // ...an async generator is not "parallel", i.e. intil one
128
- // returned promise is resolved the generator blocks (will not
129
- // advance)...
130
- // ...can we feed out a results one by one???
131
- __pack: function(list, handler=undefined){
126
+ // XXX ITER can we unwind (sync/async) generators one by one???
127
+ __pack: function(list, handler=undefined, onerror=undefined){
132
128
  var that = this
133
- // handle iterable promise list...
129
+ // handle iterator...
130
+ // XXX ITER do we unwind the iterator here or wait to unwind later???
131
+ if(typeof(list) == 'object'
132
+ && Symbol.iterator in list){
133
+ if(!onerror){
134
+ list = [...list]
135
+ // handle errors in input generator...
136
+ } else {
137
+ var res = []
138
+ try{
139
+ for(var e of list){
140
+ res.push(e) }
141
+ list = res
142
+ }catch(err){
143
+ var r = onerror(err)
144
+ if(r === this.constructor.STOP
145
+ || r instanceof this.constructor.STOP){
146
+ r instanceof this.constructor.STOP
147
+ && res.push(r.value)
148
+ list = res
149
+ } else {
150
+ list = r instanceof Array ?
151
+ r
152
+ : r ?
153
+ [r]
154
+ : [] } } } }
155
+ // handle iterable promise...
134
156
  if(list instanceof IterablePromise){
135
- return this.__handle(list.__packed, handler) }
136
- // handle promise list...
137
- if(list instanceof Promise){
138
- return list.then(function(list){
139
- return that.__pack(list, handler) }) }
157
+ return this.__handle(list.__packed, handler, onerror) }
158
+ // handle promise / async-iterator...
159
+ // XXX ITER do we unwind the iterator here or wait to unwind later???
160
+ if(typeof(list) == 'object'
161
+ && Symbol.asyncIterator in list){
162
+ return list
163
+ .iter(handler, onerror)
164
+ .then(function(list){
165
+ return that.__pack(list) })
166
+ } else if(list instanceof Promise){
167
+ return list
168
+ .then(function(list){
169
+ return that.__pack(list, handler, onerror) }) }
140
170
  // do the work...
141
171
  // NOTE: packing and handling are mixed here because it's faster
142
172
  // to do them both on a single list traverse...
@@ -146,7 +176,6 @@ object.Constructor('IterablePromise', Promise, {
146
176
  return [elem] }
147
177
 
148
178
  //* XXX EXPEREMENTAL: STOP...
149
- var stoppable = false
150
179
  var stop = false
151
180
  var map = 'map'
152
181
  var pack = function(){
@@ -162,10 +191,7 @@ object.Constructor('IterablePromise', Promise, {
162
191
  && !(elem.sync() instanceof Promise)) ?
163
192
  handler(elem.sync())
164
193
  : elem && elem.then ?
165
- /*/
166
- return elem && elem.then ?
167
- //*/
168
- (stoppable ?
194
+ (handleSTOP ?
169
195
  // stoppable -- need to handle stop async...
170
196
  elem
171
197
  .then(function(res){
@@ -189,9 +215,9 @@ object.Constructor('IterablePromise', Promise, {
189
215
 
190
216
  // pack (stoppable)...
191
217
  if(!!this.constructor.STOP){
192
- stoppable = true
193
218
  map = 'smap'
194
219
  var handleSTOP = function(err){
220
+ // handle stop...
195
221
  stop = err
196
222
  if(err === that.constructor.STOP
197
223
  || err instanceof that.constructor.STOP){
@@ -221,7 +247,7 @@ object.Constructor('IterablePromise', Promise, {
221
247
  : handler(elem) }) },
222
248
  //*/
223
249
  // transform/handle packed array (sync)...
224
- __handle: function(list, handler=undefined){
250
+ __handle: function(list, handler=undefined, onerror=undefined){
225
251
  var that = this
226
252
  if(typeof(list) == 'function'){
227
253
  handler = list
@@ -231,7 +257,7 @@ object.Constructor('IterablePromise', Promise, {
231
257
  // handle promise list...
232
258
  if(list instanceof Promise){
233
259
  return list.then(function(list){
234
- return that.__handle(list, handler) }) }
260
+ return that.__handle(list, handler, onerror) }) }
235
261
  // do the work...
236
262
  // NOTE: since each section of the packed .__array is the same
237
263
  // structure as the input we'll use .__pack(..) to handle
@@ -245,9 +271,9 @@ object.Constructor('IterablePromise', Promise, {
245
271
  return list.map(function(elem){
246
272
  //*/
247
273
  return elem instanceof Array ?
248
- that.__pack(elem, handler)
274
+ that.__pack(elem, handler, onerror)
249
275
  : elem instanceof Promise ?
250
- that.__pack(elem, handler)
276
+ that.__pack(elem, handler, onerror)
251
277
  //.then(function(elem){
252
278
  .then(function([elem]){
253
279
  return elem })
@@ -274,11 +300,16 @@ object.Constructor('IterablePromise', Promise, {
274
300
  // give up on a sync solution...
275
301
  if(e instanceof Promise){
276
302
  // XXX can we return an IterablePromise???
277
- // XXX these will cause infinite recursion....
303
+ // XXX this will cause infinite recursion....
278
304
  //return Promise.iter(list).flat() }
305
+ // XXX is there a more elegant way to do this???
279
306
  return Promise.all(list)
280
307
  .then(function(list){
281
- return list.flat() }) }
308
+ return list.flat() })
309
+ .iter() }
310
+ //return Promise.all(list)
311
+ // .then(function(list){
312
+ // return list.flat() }) }
282
313
  res.push(e) }
283
314
  return res.flat() },
284
315
  /*/
@@ -658,7 +689,7 @@ object.Constructor('IterablePromise', Promise, {
658
689
  // NOTE: if 'types/Array' is imported this will support throwing STOP,
659
690
  // for more info see notes for .__pack(..)
660
691
  // XXX EXPEREMENTAL: STOP...
661
- __new__: function(_, list, handler){
692
+ __new__: function(_, list, handler=undefined, onerror=undefined){
662
693
  // instance...
663
694
  var promise
664
695
  var obj = Reflect.construct(
@@ -678,8 +709,8 @@ object.Constructor('IterablePromise', Promise, {
678
709
  // handle/pack input data...
679
710
  if(handler != 'raw'){
680
711
  list = list instanceof IterablePromise ?
681
- obj.__handle(list.__packed, handler)
682
- : obj.__pack(list, handler) }
712
+ obj.__handle(list.__packed, handler, onerror)
713
+ : obj.__pack(list, handler, onerror) }
683
714
  Object.defineProperty(obj, '__packed', {
684
715
  value: list,
685
716
  enumerable: false,
@@ -1003,10 +1034,9 @@ object.Constructor('SyncPromise', Promise, {
1003
1034
  // async...
1004
1035
  if(!error
1005
1036
  && value instanceof Promise){
1006
- return value }
1037
+ return object.ASIS(value) }
1007
1038
  // sync...
1008
1039
  var obj = Promise.resolve(value)
1009
- obj.__proto__ = this.prototype
1010
1040
  obj.value = value
1011
1041
  rejected
1012
1042
  && (obj.error = error)
@@ -1025,10 +1055,9 @@ object.Mixin('PromiseMixin', 'soft', {
1025
1055
  cooperative: CooperativePromise,
1026
1056
  sync: SyncPromise,
1027
1057
  // XXX should this be implemented via SyncPromise???
1028
- awaitOrRun: function(data, func){
1058
+ awaitOrRun: function(data, func, error){
1029
1059
  data = [...arguments]
1030
1060
  func = data.pop()
1031
- var error
1032
1061
  if(typeof(data.at(-1)) == 'function'){
1033
1062
  error = func
1034
1063
  func = data.pop() }
@@ -1067,9 +1096,14 @@ var PromiseProtoMixin =
1067
1096
  module.PromiseProtoMixin =
1068
1097
  object.Mixin('PromiseProtoMixin', 'soft', {
1069
1098
  as: ProxyPromise,
1070
- iter: function(handler=undefined){
1071
- return IterablePromise(this, handler) },
1072
- sync: function(){
1099
+ iter: function(handler=undefined, onerror=undefined){
1100
+ return IterablePromise(this, handler, onerror) },
1101
+ // unify the general promise API with other promise types...
1102
+ // XXX should this be here???
1103
+ // XXX error if given must return the result... (???)
1104
+ sync: function(error){
1105
+ typeof(error) == 'function'
1106
+ && this.catch(error)
1073
1107
  return this },
1074
1108
  })
1075
1109
 
package/README.md CHANGED
@@ -88,6 +88,9 @@ Library of JavaScript type extensions, types and utilities.
88
88
  - [Array proxy methods returning `<promise-iter>`](#array-proxy-methods-returning-promise-iter)
89
89
  - [Array proxy methods returning a `<promise>`](#array-proxy-methods-returning-a-promise)
90
90
  - [`<promise-iter>.then(..)` / `<promise-iter>.catch(..)` / `<promise-iter>.finally(..)`](#promise-iterthen--promise-itercatch--promise-iterfinally)
91
+ - [`<promise-iter>.iterthen(..)`](#promise-iterthen)
92
+ - [`<promise-iter>.isSync(..)`](#promise-issync)
93
+ - [`<promise-iter>.sync(..)`](#promise-sync)
91
94
  - [`promise.IterablePromise.STOP` / `promise.IterablePromise.STOP(..)`](#promiseiterablepromisestop--promiseiterablepromisestop)
92
95
  - [Advanced handler](#advanced-handler)
93
96
  - [Stopping the iteration](#stopping-the-iteration)
@@ -1823,6 +1826,7 @@ These are similar to [`<array>.some(..)`](https://developer.mozilla.org/en-US/do
1823
1826
  and [`<array>.find(..)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)
1824
1827
  see them for more info.
1825
1828
 
1829
+
1826
1830
  #### Array proxy methods returning `<promise-iter>`
1827
1831
 
1828
1832
  - `<promise-iter>.sort(..)`
@@ -1887,6 +1891,24 @@ For more info see: [Stopping the iteration](#stopping-the-iteration) below, and
1887
1891
  [the 'Array' STOP section](#arraystop--arraystop)
1888
1892
 
1889
1893
 
1894
+ #### `<promise-iter>.iterthen(..)`
1895
+
1896
+ Like `.then(..)` but will return an `IterablePromise` instance.
1897
+
1898
+
1899
+ #### `<promise-iter>.isSync()`
1900
+
1901
+ Return `true` if all content is resolved, otherwise return `false`.
1902
+
1903
+
1904
+ #### `<promise-iter>.sync(..)`
1905
+
1906
+ If all content is resolved return the promise value, otherwise return a promise.
1907
+
1908
+ For more info see: [`<promise>.sync(..)`](#promisesync)
1909
+
1910
+
1911
+
1890
1912
  #### Advanced handler
1891
1913
 
1892
1914
  ```bnf
@@ -2008,12 +2030,17 @@ the main `<promise>` is resolved.
2008
2030
 
2009
2031
  ### Sync/async promise
2010
2032
 
2033
+ The goal of this is to handle both sync and asynchronous data flows with
2034
+ one promise-like API, if all of the data can be obtained in a sync manner
2035
+ this will be sync otherwise we will revert to a normal promise.
2036
+
2011
2037
 
2012
2038
  #### `Promise.sync(..)` / `promise.SyncPromise(..)`
2013
2039
 
2014
2040
  ```dnf
2015
2041
  Promise.sync(<func>)
2016
2042
  -> <sync-promise>
2043
+ -> <promise>
2017
2044
 
2018
2045
  <func>(<resolve>, <reject>)
2019
2046
 
package/generator.js CHANGED
@@ -308,21 +308,31 @@ var GeneratorProtoMixin =
308
308
  module.GeneratorProtoMixin =
309
309
  object.Mixin('GeneratorProtoMixin', 'soft', {
310
310
  // XXX use module.iter(..) ???
311
- iter: stoppable(function*(handler){
312
- if(handler){
313
- var i = 0
314
- for(var elem of this){
315
- var res = handler.call(this, elem, i)
316
- // expand iterables...
317
- if(typeof(res) == 'object'
318
- && Symbol.iterator in res){
319
- yield* res
320
- // as-is...
321
- } else {
322
- yield res }}
323
- // no handler...
324
- } else {
325
- yield* this } }),
311
+ iter: stoppable(function*(handler, onerror){
312
+ try{
313
+ if(handler){
314
+ var i = 0
315
+ for(var elem of this){
316
+ var res = handler.call(this, elem, i)
317
+ // expand iterables...
318
+ if(typeof(res) == 'object'
319
+ && Symbol.iterator in res){
320
+ yield* res
321
+ // as-is...
322
+ } else {
323
+ yield res }}
324
+ // no handler...
325
+ } else {
326
+ yield* this }
327
+ }catch(err){
328
+ if(onerror){
329
+ if(!(err === STOP
330
+ || err instanceof STOP)){
331
+ var res = onerror(err)
332
+ if(res){
333
+ yield res
334
+ return } } }
335
+ throw err }}),
326
336
  //*/
327
337
 
328
338
  at: function(i){
@@ -556,18 +566,27 @@ object.Mixin('AsyncGeneratorProtoMixin', 'soft', {
556
566
  return this.unwind.finally(...arguments) },
557
567
 
558
568
  // XXX might be a good idea to use this approach above...
559
- iter: stoppable(async function*(handler=undefined){
560
- var i = 0
561
- if(handler){
562
- for await(var e of this){
563
- var res = handler.call(this, e, i++)
564
- if(typeof(res) == 'object'
565
- && Symbol.iterator in res){
566
- yield* res
567
- } else {
568
- yield res } }
569
- } else {
570
- yield* this } }),
569
+ iter: stoppable(async function*(handler=undefined, onerror=undefined){
570
+ try{
571
+ var i = 0
572
+ if(handler){
573
+ for await(var e of this){
574
+ var res = handler.call(this, e, i++)
575
+ if(typeof(res) == 'object'
576
+ && Symbol.iterator in res){
577
+ yield* res
578
+ } else {
579
+ yield res } }
580
+ } else {
581
+ yield* this }
582
+ }catch(err){
583
+ if(onerror){
584
+ if(!(err === STOP || err instanceof STOP)){
585
+ var res = onerror(err)
586
+ if(res){
587
+ yield res }
588
+ return } }
589
+ throw err } }),
571
590
 
572
591
  map: async function*(func){
573
592
  yield* this.iter(function(elem, i){
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.24.5",
3
+ "version": "6.24.8",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {