ig-types 6.11.2 → 6.11.3

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 (2) hide show
  1. package/package.json +1 -1
  2. package/Promise.js.bak +0 -548
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ig-types",
3
- "version": "6.11.2",
3
+ "version": "6.11.3",
4
4
  "description": "Generic JavaScript types and type extensions...",
5
5
  "main": "main.js",
6
6
  "scripts": {
package/Promise.js.bak DELETED
@@ -1,548 +0,0 @@
1
- /**********************************************************************
2
- *
3
- *
4
- *
5
- *
6
- * This defines the following extensions to Promise:
7
- *
8
- * Promise.iter(seq)
9
- * Iterable promise object.
10
- * Similar to Promise.all(..) but adds basic iterator/generator
11
- * API and will resolve the items as they are ready (resolved).
12
- *
13
- * Promise.interactive(handler)
14
- * Interactive promise object.
15
- * This adds a basic message passing API to the promise.
16
- *
17
- * Promise.cooperative()
18
- * Cooperative promise object.
19
- * Exposes the API to resolve/reject the promise object
20
- * externally.
21
- *
22
- *
23
- *
24
- **********************************************/ /* c8 ignore next 2 */
25
- ((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
26
- (function(require){ var module={} // make module AMD/node compatible...
27
- /*********************************************************************/
28
-
29
- var object = require('ig-object')
30
-
31
- // XXX required for STOP...
32
- //var generator = require('./generator')
33
-
34
-
35
- /*********************************************************************/
36
-
37
- /* XXX not used yet...
38
- // NOTE: this is used in a similar fashion to Python's StopIteration...
39
- var STOP =
40
- module.STOP =
41
- object.STOP
42
- //*/
43
-
44
-
45
- //---------------------------------------------------------------------
46
- // Iterable promise...
47
- //
48
- // Like Promise.all(..) but adds ability to iterate through results
49
- // via generators .map(..)/.reduce(..) and friends...
50
- //
51
-
52
- var IterablePromise =
53
- module.IterablePromise =
54
- object.Constructor('IterablePromise', Promise, {
55
- // XXX
56
- //STOP: object.STOP,
57
-
58
- //
59
- // Format:
60
- // [
61
- // [ <value> ],
62
- // <promise>,
63
- // ...
64
- // ]
65
- //
66
- __list: null,
67
-
68
-
69
- // iterator methods...
70
- //
71
- // These will return a new IterablePromise instance...
72
- //
73
- // When called from a resolved promise these will return a new
74
- // resolved promise with updated values...
75
- //
76
- // When called from a rejected promise these will return a rejected
77
- // with the same reason promise...
78
- //
79
- //
80
- // NOTE: these are different to Array's equivalents in that the handler
81
- // is called not in the order of the elements but rather in order
82
- // of promise resolution...
83
- // NOTE: index of items is unknowable because items can expand and
84
- // contract depending on handlrs (e.g. .filter(..) can remove
85
- // items)...
86
- // This the following can not be implemented here:
87
- // .slice(..)
88
- // .splice(..)
89
- // .values() / .keys()
90
- // .at(..)
91
- // [Symbol.iterator]() - needs to be sync...
92
- // The followng methods are questionable:
93
- // .indexOf(..)
94
- // .includes(..)
95
- // .some(..) / .every(..)
96
- // .sort(..)
97
- // XXX BUG:
98
- // Promise.iter([1,2,3])
99
- // .map(e => e)
100
- // is not the same as:
101
- // Promise.iter([1,2,3])
102
- // .map(e => e)
103
- // .map(e => e)
104
- // XXX should these support STOP???
105
- map: function(func){
106
- return this.constructor(this.__list.flat(),
107
- function(e){
108
- return [func(e)] }) },
109
- filter: function(func){
110
- return this.constructor(this.__list.flat(),
111
- function(e){
112
- return func(e) ?
113
- [e]
114
- : [] }) },
115
- // NOTE: this does not return an iterable promise as we can't know
116
- // what the user reduces to...
117
- // NOTE: the items can be handled out of order because the nested
118
- // promises can resolve in any order.
119
- // XXX write how to go around this...
120
- // NOTE: since order of execution can not be guaranteed there is no
121
- // point in implementing .reduceRight(..)
122
- reduce: function(func, res){
123
- return this.constructor(this.__list.flat(),
124
- function(e){
125
- res = func(res, e)
126
- return [] })
127
- .then(function(){
128
- return res }) },
129
- flat: function(depth=1){
130
- return this.constructor(this.__list.flat(),
131
- function(e){
132
- return (depth > 1
133
- && e != null
134
- && e.flat) ?
135
- e.flat(depth-1)
136
- : depth != 0 ?
137
- e
138
- : [e] }) },
139
- // XXX REVERSE...
140
- // XXX BUG:
141
- // await Promise.iter([1, Promise.iter(['a', ['b', 'c']]), [2, 3], 4])
142
- // .flat()
143
- // -> [ 1, 'a', [ 'b', 'c' ], 2, 3, 4 ]
144
- // await Promise.iter([1, Promise.iter(['a', ['b', 'c']]), [2, 3], 4])
145
- // .flat()
146
- // .reverse()
147
- // -> [ 4, 2, 3, [ 'b', 'c', 'a' ], 1 ]
148
- // ...should be:
149
- // -> [ 4, 2, 3, [ 'b', 'c' ], 'a', 1 ]
150
- // it's odd we are not seeing this in other places...
151
- reverse: function(){
152
- return this.constructor(this.__list
153
- .map(function(elems){
154
- return elems && elems.then ?
155
- elems.then(function(res){
156
- return res
157
- .reverse()
158
- .flat() })
159
- : elems })
160
- .reverse()
161
- .flat()) },
162
-
163
- // compatibility with root promise...
164
- // XXX BUG:
165
- // await Promise.iter(Promise.resolve([1,2,[3, 4]])).iter()
166
- // -> [ [ 1, 2, 3, 4 ] ]
167
- iter: function(){
168
- return this.constructor(
169
- // clone and unwrap the .__list
170
- this.__list
171
- .map(function(elems){
172
- return elems && elems.then ?
173
- elems.then(function(res){
174
- return res instanceof Array ?
175
- res.flat()
176
- : res })
177
- : elems })
178
- .flat()) },
179
-
180
- // XXX do we need these?
181
- // .pop()
182
- // .shift()
183
- // .first() / .last()
184
- // XXX these can change the "resolved" state...
185
- // ...i.e. return a pending promise when called from a fulfilled
186
- // promise....
187
- // .concat(..)
188
- // .push(..)
189
- // .unshift(..)
190
- // .first(..) / .last(..)
191
-
192
-
193
- // Overload .then(..), .catch(..) and .finally(..) to return a plain
194
- // Promise instnace...
195
- //
196
- // NOTE: .catch(..) and .finally(..) are implemented through .then(..)
197
- // so we do not need to overload those...
198
- // NOTE: this is slightly different from .then(..) in that it can be
199
- // called without arguments and return a promise wrapper. This can
200
- // be useful to hide special promise functionality...
201
- then: function (onfulfilled, onrejected){
202
- var p = new Promise(
203
- function(resolve, reject){
204
- Promise.prototype.then.call(this,
205
- // NOTE: resolve(..) / reject(..) return undefined so
206
- // we can't pass them directly here...
207
- function(res){
208
- resolve(res)
209
- return res },
210
- function(res){
211
- reject(res)
212
- return res }) }.bind(this))
213
- return arguments.length > 0 ?
214
- p.then(...arguments)
215
- : p },
216
-
217
-
218
- //
219
- // Promise.iter([ .. ])
220
- // -> iterable-promise
221
- //
222
- // Promise.iter([ .. ], handler)
223
- // -> iterable-promise
224
- //
225
- //
226
- // handler(e)
227
- // -> [value]
228
- // -> []
229
- //
230
- //
231
- // NOTE: element index is unknowable untill the full list is expanded
232
- // as handler(..)'s return value can expand to any number of
233
- // items...
234
- // XXX we can make the index a promise, then if the client needs
235
- // the value they can wait for it...
236
- //
237
- //
238
- // Spectial cases usefull for extending this constructor...
239
- //
240
- // Clone the iterator...
241
- // Promise.iter([ .. ], false)
242
- // -> iterable-promise
243
- //
244
- // Create a rejected iterator...
245
- // Promise.iter(false)
246
- // -> iterable-promise
247
- //
248
- //
249
- // XXX if list is an iterator, can we fill this async???
250
- // XXX iterator/generator as input:
251
- // - do we unwind here or externally?
252
- // ...feels like with the generator external unwinding is
253
- // needed...
254
- // XXX would be nice to support trowing STOP...
255
- // - this is more complicated than simply suing .smap(..) instead
256
- // of .map(..) because the list can contain async promises...
257
- // ...would need to wrap each .then(..) call in try-catch and
258
- // manually handle the stop...
259
- // - another issue here is that the stop would happen in order of
260
- // execution and not order of elements...
261
- // XXX add support for list as a promise....
262
- __new__: function(_, list, handler){
263
- // handle: IterablePromise(<list>, <mode>)
264
- if(typeof(handler) == 'string'){
265
- mode = handler
266
- handler = undefined }
267
-
268
- // instance...
269
- var promise
270
- var obj = Reflect.construct(
271
- IterablePromise.__proto__,
272
- [function(resolve, reject){
273
- // NOTE: this is here for Promise compatibilty...
274
- if(typeof(list) == 'function'){
275
- return list.call(this, ...arguments) }
276
- // initial reject...
277
- if(list === false){
278
- return reject() }
279
- promise = {resolve, reject} }],
280
- IterablePromise)
281
-
282
- if(promise){
283
- // clone/normalize...
284
- list =
285
- // special case: promised list...
286
- list instanceof Promise ?
287
- list.then(function(res){
288
- return [res] })
289
- : [list].flat()
290
- .map(function(e){
291
- return (e && e.then) ?
292
- e.then(function(e){
293
- return [e] })
294
- : [e] })
295
-
296
- if(handler){
297
- // NOTE: this is recursive to handle expanding nested promises...
298
- var handle = function(elem){
299
- // call the handler...
300
- return (elem && elem.then) ?
301
- elem.then(function(elems){
302
- return elems
303
- .map(handle)
304
- .flat() })
305
- : elem
306
- .map(handler)
307
- .flat() }
308
-
309
- // handle the list...
310
- //list = list.map(handle) }
311
- list =
312
- list instanceof Promise ?
313
- // special case: promised list...
314
- list.then(function(list){
315
- return list.map(handle) })
316
- : list.map(handle) }
317
-
318
- Object.defineProperty(obj, '__list', {
319
- value: list,
320
- enumerable: false,
321
- })
322
-
323
- // handle promise state...
324
- //Promise.all(list)
325
- ;(list instanceof Promise ?
326
- // special case: promised list...
327
- list
328
- : Promise.all(list))
329
- .then(function(res){
330
- promise.resolve(res.flat()) })
331
- .catch(promise.reject) }
332
-
333
- return obj },
334
- })
335
-
336
-
337
-
338
- //---------------------------------------------------------------------
339
- // Interactive promise...
340
- //
341
- // Adds ability to send messages to the running promise.
342
- //
343
-
344
- var InteractivePromise =
345
- module.InteractivePromise =
346
- object.Constructor('InteractivePromise', Promise, {
347
- // XXX do we need a way to remove handlers???
348
- __message_handlers: null,
349
-
350
- send: function(...args){
351
- var that = this
352
- ;(this.__message_handlers || [])
353
- .forEach(function(h){ h.call(that, ...args) })
354
- return this },
355
-
356
- then: IterablePromise.prototype.then,
357
-
358
- //
359
- // Promise.interactive(handler)
360
- // -> interacive-promise
361
- //
362
- // handler(resolve, reject, onmessage)
363
- //
364
- // onmessage(func)
365
- //
366
- //
367
- __new__: function(_, handler){
368
- var handlers = []
369
-
370
- var onmessage = function(func){
371
- // remove all handlers...
372
- if(func === false){
373
- var h = (obj == null ?
374
- handlers
375
- : (obj.__message_handlers || []))
376
- h.splice(0, handlers.length)
377
- // remove a specific handler...
378
- } else if(arguments[1] === false){
379
- var h = (obj == null ?
380
- handlers
381
- : (obj.__message_handlers || []))
382
- h.splice(h.indexOf(func), 1)
383
- // register a handler...
384
- } else {
385
- var h = obj == null ?
386
- // NOTE: we need to get the handlers from .__message_handlers
387
- // unless we are not fully defined yet, then use the
388
- // bootstrap container (handlers)...
389
- // ...since we can call onmessage(..) while the promise
390
- // is still defined there is no way to .send(..) until it
391
- // returns a promise object, this races here are highly
392
- // unlikely...
393
- handlers
394
- : (obj.__message_handlers =
395
- obj.__message_handlers ?? [])
396
- handlers.push(func) } }
397
-
398
- var obj = Reflect.construct(
399
- InteractivePromise.__proto__,
400
- !handler ?
401
- []
402
- : [function(resolve, reject){
403
- return handler(resolve, reject, onmessage) }],
404
- InteractivePromise)
405
- Object.defineProperty(obj, '__message_handlers', {
406
- value: handlers,
407
- enumerable: false,
408
- // XXX should this be .configurable???
409
- configurable: true,
410
- })
411
- return obj },
412
- })
413
-
414
-
415
-
416
- //---------------------------------------------------------------------
417
- // Cooperative promise...
418
- //
419
- // A promise that can be resolved/rejected externally.
420
- // NOTE: normally this has no internal resolver logic...
421
- //
422
-
423
- var CooperativePromise =
424
- module.CooperativePromise =
425
- object.Constructor('CooperativePromise', Promise, {
426
- __handlers: null,
427
-
428
- get isSet(){
429
- return this.__handlers === false },
430
-
431
- set: function(value, resolve=true){
432
- // can't set twice...
433
- if(this.isSet){
434
- throw new Error('.set(..): can not set twice') }
435
- // bind to promise...
436
- if(value && value.then && value.catch){
437
- value.then(handlers.resolve)
438
- value.catch(handlers.reject)
439
- // resolve with value...
440
- } else {
441
- resolve ?
442
- this.__handlers.resolve(value)
443
- : this.__handlers.reject(value) }
444
- // cleanup and prevent setting twice...
445
- this.__handlers = false
446
- return this },
447
-
448
- then: IterablePromise.prototype.then,
449
-
450
- __new__: function(){
451
- var handlers
452
- var resolver = arguments[1]
453
-
454
- var obj = Reflect.construct(
455
- CooperativePromise.__proto__,
456
- [function(resolve, reject){
457
- handlers = {resolve, reject}
458
- // NOTE: this is here to support builtin .then(..)
459
- resolver
460
- && resolver(resolve, reject) }],
461
- CooperativePromise)
462
-
463
- Object.defineProperty(obj, '__handlers', {
464
- value: handlers,
465
- enumerable: false,
466
- writable: true,
467
- })
468
- return obj },
469
- })
470
-
471
-
472
-
473
- //---------------------------------------------------------------------
474
-
475
- // XXX EXPEREMENTAL...
476
- var ProxyPromise =
477
- module.ProxyPromise =
478
- object.Constructor('ProxyPromise', Promise, {
479
- __new__: function(context, constructor){
480
- var proto = 'prototype' in constructor ?
481
- constructor.prototype
482
- : constructor
483
- var obj = Reflect.construct(
484
- ProxyPromise.__proto__,
485
- [function(resolve, reject){
486
- context.then(resolve)
487
- context.catch(reject) }],
488
- ProxyPromise)
489
- // populate...
490
- // NOTE: we are not using object.deepKeys(..) here as we need
491
- // the key origin not to trigger property getters...
492
- var seen = new Set()
493
- while(proto != null){
494
- Object.entries(Object.getOwnPropertyDescriptors(proto))
495
- .forEach(function([key, value]){
496
- // skip overloaded keys...
497
- if(seen.has(key)){
498
- return }
499
- // skip non-functions...
500
- if(typeof(value.value) != 'function'){
501
- return }
502
- // skip non-enumerable except for Object.prototype.run(..)...
503
- if(!(key == 'run'
504
- && Object.prototype.run === value.value)
505
- && !value.enumerable){
506
- return }
507
- // proxy...
508
- obj[key] = function(...args){
509
- // XXX should we also .catch(..) here???
510
- return context.then(function(res){
511
- return res[key](...args) }) } })
512
- proto = proto.__proto__ }
513
- return obj },
514
- })
515
-
516
-
517
-
518
- //---------------------------------------------------------------------
519
-
520
- var PromiseMixin =
521
- module.PromiseMixin =
522
- object.Mixin('PromiseMixin', 'soft', {
523
- iter: IterablePromise,
524
- interactive: InteractivePromise,
525
- cooperative: CooperativePromise,
526
- })
527
-
528
- PromiseMixin(Promise)
529
-
530
-
531
- // XXX EXPEREMENTAL...
532
- var PromiseProtoMixin =
533
- module.PromiseProtoMixin =
534
- object.Mixin('PromiseProtoMixin', 'soft', {
535
- as: ProxyPromise,
536
-
537
- // XXX
538
- iter: function(){
539
- return IterablePromise(this) },
540
- })
541
-
542
- PromiseProtoMixin(Promise.prototype)
543
-
544
-
545
-
546
-
547
- /**********************************************************************
548
- * vim:set ts=4 sw=4 : */ return module })