ig-types 6.24.7 → 6.24.9

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 +39 -29
  2. package/generator.js +48 -27
  3. package/package.json +1 -1
package/Promise.js CHANGED
@@ -124,23 +124,42 @@ object.Constructor('IterablePromise', Promise, {
124
124
  // handlers in sequence.
125
125
  // XXX EXPEREMENTAL: STOP...
126
126
  // XXX ITER can we unwind (sync/async) generators one by one???
127
- __pack: function(list, handler=undefined){
127
+ __pack: function(list, handler=undefined, onerror=undefined){
128
128
  var that = this
129
129
  // handle iterator...
130
130
  // XXX ITER do we unwind the iterator here or wait to unwind later???
131
131
  if(typeof(list) == 'object'
132
132
  && Symbol.iterator in list){
133
- list = [...list] }
133
+ if(!onerror){
134
+ list = [...list]
135
+ // handle errors in input generator...
136
+ // NOTE: this does not offer the most control because semantically
137
+ // we bust behave in the same manner as <generator>.iter(..)
138
+ } else {
139
+ var res = []
140
+ try{
141
+ for(var e of list){
142
+ res.push(e) }
143
+ }catch(err){
144
+ var r = onerror(err)
145
+ r !== undefined
146
+ && res.push(r) }
147
+ list = res } }
134
148
  // handle iterable promise...
135
149
  if(list instanceof IterablePromise){
136
- return this.__handle(list.__packed, handler) }
150
+ return this.__handle(list.__packed, handler, onerror) }
137
151
  // handle promise / async-iterator...
138
152
  // XXX ITER do we unwind the iterator here or wait to unwind later???
139
- if(list instanceof Promise
140
- || (typeof(list) == 'object'
141
- && Symbol.asyncIterator in list)){
142
- return list.then(function(list){
143
- return that.__pack(list, handler) }) }
153
+ if(typeof(list) == 'object'
154
+ && Symbol.asyncIterator in list){
155
+ return list
156
+ .iter(handler, onerror)
157
+ .then(function(list){
158
+ return that.__pack(list) }) }
159
+ if(list instanceof Promise){
160
+ return list
161
+ .then(function(list){
162
+ return that.__pack(list, handler, onerror) }) }
144
163
  // do the work...
145
164
  // NOTE: packing and handling are mixed here because it's faster
146
165
  // to do them both on a single list traverse...
@@ -150,16 +169,11 @@ object.Constructor('IterablePromise', Promise, {
150
169
  return [elem] }
151
170
 
152
171
  //* XXX EXPEREMENTAL: STOP...
153
- var stoppable = false
154
172
  var stop = false
155
173
  var map = 'map'
156
174
  var pack = function(){
157
175
  return [list].flat()
158
176
  [map](function(elem){
159
- // XXX EXPEREMENTAL -- not sure about this yet...
160
- //elem = Symbol.iterator in elem ?
161
- // [...elem]
162
- // : elem
163
177
  // XXX EXPEREMENTAL...
164
178
  return elem instanceof IterablePromise ?
165
179
  (elem.isSync() ?
@@ -170,10 +184,7 @@ object.Constructor('IterablePromise', Promise, {
170
184
  && !(elem.sync() instanceof Promise)) ?
171
185
  handler(elem.sync())
172
186
  : elem && elem.then ?
173
- /*/
174
- return elem && elem.then ?
175
- //*/
176
- (stoppable ?
187
+ (handleSTOP ?
177
188
  // stoppable -- need to handle stop async...
178
189
  elem
179
190
  .then(function(res){
@@ -197,9 +208,9 @@ object.Constructor('IterablePromise', Promise, {
197
208
 
198
209
  // pack (stoppable)...
199
210
  if(!!this.constructor.STOP){
200
- stoppable = true
201
211
  map = 'smap'
202
212
  var handleSTOP = function(err){
213
+ // handle stop...
203
214
  stop = err
204
215
  if(err === that.constructor.STOP
205
216
  || err instanceof that.constructor.STOP){
@@ -229,7 +240,7 @@ object.Constructor('IterablePromise', Promise, {
229
240
  : handler(elem) }) },
230
241
  //*/
231
242
  // transform/handle packed array (sync)...
232
- __handle: function(list, handler=undefined){
243
+ __handle: function(list, handler=undefined, onerror=undefined){
233
244
  var that = this
234
245
  if(typeof(list) == 'function'){
235
246
  handler = list
@@ -239,7 +250,7 @@ object.Constructor('IterablePromise', Promise, {
239
250
  // handle promise list...
240
251
  if(list instanceof Promise){
241
252
  return list.then(function(list){
242
- return that.__handle(list, handler) }) }
253
+ return that.__handle(list, handler, onerror) }) }
243
254
  // do the work...
244
255
  // NOTE: since each section of the packed .__array is the same
245
256
  // structure as the input we'll use .__pack(..) to handle
@@ -253,9 +264,9 @@ object.Constructor('IterablePromise', Promise, {
253
264
  return list.map(function(elem){
254
265
  //*/
255
266
  return elem instanceof Array ?
256
- that.__pack(elem, handler)
267
+ that.__pack(elem, handler, onerror)
257
268
  : elem instanceof Promise ?
258
- that.__pack(elem, handler)
269
+ that.__pack(elem, handler, onerror)
259
270
  //.then(function(elem){
260
271
  .then(function([elem]){
261
272
  return elem })
@@ -671,7 +682,7 @@ object.Constructor('IterablePromise', Promise, {
671
682
  // NOTE: if 'types/Array' is imported this will support throwing STOP,
672
683
  // for more info see notes for .__pack(..)
673
684
  // XXX EXPEREMENTAL: STOP...
674
- __new__: function(_, list, handler){
685
+ __new__: function(_, list, handler=undefined, onerror=undefined){
675
686
  // instance...
676
687
  var promise
677
688
  var obj = Reflect.construct(
@@ -691,8 +702,8 @@ object.Constructor('IterablePromise', Promise, {
691
702
  // handle/pack input data...
692
703
  if(handler != 'raw'){
693
704
  list = list instanceof IterablePromise ?
694
- obj.__handle(list.__packed, handler)
695
- : obj.__pack(list, handler) }
705
+ obj.__handle(list.__packed, handler, onerror)
706
+ : obj.__pack(list, handler, onerror) }
696
707
  Object.defineProperty(obj, '__packed', {
697
708
  value: list,
698
709
  enumerable: false,
@@ -1037,10 +1048,9 @@ object.Mixin('PromiseMixin', 'soft', {
1037
1048
  cooperative: CooperativePromise,
1038
1049
  sync: SyncPromise,
1039
1050
  // XXX should this be implemented via SyncPromise???
1040
- awaitOrRun: function(data, func){
1051
+ awaitOrRun: function(data, func, error){
1041
1052
  data = [...arguments]
1042
1053
  func = data.pop()
1043
- var error
1044
1054
  if(typeof(data.at(-1)) == 'function'){
1045
1055
  error = func
1046
1056
  func = data.pop() }
@@ -1079,8 +1089,8 @@ var PromiseProtoMixin =
1079
1089
  module.PromiseProtoMixin =
1080
1090
  object.Mixin('PromiseProtoMixin', 'soft', {
1081
1091
  as: ProxyPromise,
1082
- iter: function(handler=undefined){
1083
- return IterablePromise(this, handler) },
1092
+ iter: function(handler=undefined, onerror=undefined){
1093
+ return IterablePromise(this, handler, onerror) },
1084
1094
  // unify the general promise API with other promise types...
1085
1095
  // XXX should this be here???
1086
1096
  // XXX error if given must return the result... (???)
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,29 @@ 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 !== undefined){
587
+ yield handler ?
588
+ handler(res)
589
+ : res }
590
+ return } }
591
+ throw err } }),
571
592
 
572
593
  map: async function*(func){
573
594
  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.7",
3
+ "version": "6.24.9",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {