ig-types 6.24.7 → 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.
Files changed (3) hide show
  1. package/Promise.js +46 -29
  2. package/generator.js +46 -27
  3. package/package.json +1 -1
package/Promise.js CHANGED
@@ -124,23 +124,49 @@ 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
+ } 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
+ : [] } } } }
134
155
  // handle iterable promise...
135
156
  if(list instanceof IterablePromise){
136
- return this.__handle(list.__packed, handler) }
157
+ return this.__handle(list.__packed, handler, onerror) }
137
158
  // handle promise / async-iterator...
138
159
  // 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) }) }
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) }) }
144
170
  // do the work...
145
171
  // NOTE: packing and handling are mixed here because it's faster
146
172
  // to do them both on a single list traverse...
@@ -150,16 +176,11 @@ object.Constructor('IterablePromise', Promise, {
150
176
  return [elem] }
151
177
 
152
178
  //* XXX EXPEREMENTAL: STOP...
153
- var stoppable = false
154
179
  var stop = false
155
180
  var map = 'map'
156
181
  var pack = function(){
157
182
  return [list].flat()
158
183
  [map](function(elem){
159
- // XXX EXPEREMENTAL -- not sure about this yet...
160
- //elem = Symbol.iterator in elem ?
161
- // [...elem]
162
- // : elem
163
184
  // XXX EXPEREMENTAL...
164
185
  return elem instanceof IterablePromise ?
165
186
  (elem.isSync() ?
@@ -170,10 +191,7 @@ object.Constructor('IterablePromise', Promise, {
170
191
  && !(elem.sync() instanceof Promise)) ?
171
192
  handler(elem.sync())
172
193
  : elem && elem.then ?
173
- /*/
174
- return elem && elem.then ?
175
- //*/
176
- (stoppable ?
194
+ (handleSTOP ?
177
195
  // stoppable -- need to handle stop async...
178
196
  elem
179
197
  .then(function(res){
@@ -197,9 +215,9 @@ object.Constructor('IterablePromise', Promise, {
197
215
 
198
216
  // pack (stoppable)...
199
217
  if(!!this.constructor.STOP){
200
- stoppable = true
201
218
  map = 'smap'
202
219
  var handleSTOP = function(err){
220
+ // handle stop...
203
221
  stop = err
204
222
  if(err === that.constructor.STOP
205
223
  || err instanceof that.constructor.STOP){
@@ -229,7 +247,7 @@ object.Constructor('IterablePromise', Promise, {
229
247
  : handler(elem) }) },
230
248
  //*/
231
249
  // transform/handle packed array (sync)...
232
- __handle: function(list, handler=undefined){
250
+ __handle: function(list, handler=undefined, onerror=undefined){
233
251
  var that = this
234
252
  if(typeof(list) == 'function'){
235
253
  handler = list
@@ -239,7 +257,7 @@ object.Constructor('IterablePromise', Promise, {
239
257
  // handle promise list...
240
258
  if(list instanceof Promise){
241
259
  return list.then(function(list){
242
- return that.__handle(list, handler) }) }
260
+ return that.__handle(list, handler, onerror) }) }
243
261
  // do the work...
244
262
  // NOTE: since each section of the packed .__array is the same
245
263
  // structure as the input we'll use .__pack(..) to handle
@@ -253,9 +271,9 @@ object.Constructor('IterablePromise', Promise, {
253
271
  return list.map(function(elem){
254
272
  //*/
255
273
  return elem instanceof Array ?
256
- that.__pack(elem, handler)
274
+ that.__pack(elem, handler, onerror)
257
275
  : elem instanceof Promise ?
258
- that.__pack(elem, handler)
276
+ that.__pack(elem, handler, onerror)
259
277
  //.then(function(elem){
260
278
  .then(function([elem]){
261
279
  return elem })
@@ -671,7 +689,7 @@ object.Constructor('IterablePromise', Promise, {
671
689
  // NOTE: if 'types/Array' is imported this will support throwing STOP,
672
690
  // for more info see notes for .__pack(..)
673
691
  // XXX EXPEREMENTAL: STOP...
674
- __new__: function(_, list, handler){
692
+ __new__: function(_, list, handler=undefined, onerror=undefined){
675
693
  // instance...
676
694
  var promise
677
695
  var obj = Reflect.construct(
@@ -691,8 +709,8 @@ object.Constructor('IterablePromise', Promise, {
691
709
  // handle/pack input data...
692
710
  if(handler != 'raw'){
693
711
  list = list instanceof IterablePromise ?
694
- obj.__handle(list.__packed, handler)
695
- : obj.__pack(list, handler) }
712
+ obj.__handle(list.__packed, handler, onerror)
713
+ : obj.__pack(list, handler, onerror) }
696
714
  Object.defineProperty(obj, '__packed', {
697
715
  value: list,
698
716
  enumerable: false,
@@ -1037,10 +1055,9 @@ object.Mixin('PromiseMixin', 'soft', {
1037
1055
  cooperative: CooperativePromise,
1038
1056
  sync: SyncPromise,
1039
1057
  // XXX should this be implemented via SyncPromise???
1040
- awaitOrRun: function(data, func){
1058
+ awaitOrRun: function(data, func, error){
1041
1059
  data = [...arguments]
1042
1060
  func = data.pop()
1043
- var error
1044
1061
  if(typeof(data.at(-1)) == 'function'){
1045
1062
  error = func
1046
1063
  func = data.pop() }
@@ -1079,8 +1096,8 @@ var PromiseProtoMixin =
1079
1096
  module.PromiseProtoMixin =
1080
1097
  object.Mixin('PromiseProtoMixin', 'soft', {
1081
1098
  as: ProxyPromise,
1082
- iter: function(handler=undefined){
1083
- return IterablePromise(this, handler) },
1099
+ iter: function(handler=undefined, onerror=undefined){
1100
+ return IterablePromise(this, handler, onerror) },
1084
1101
  // unify the general promise API with other promise types...
1085
1102
  // XXX should this be here???
1086
1103
  // 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,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.7",
3
+ "version": "6.24.8",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {