2020-11-02 18:24:20 +03:00
|
|
|
/**********************************************************************
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
**********************************************/ /* c8 ignore next 2 */
|
|
|
|
|
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
|
|
|
|
|
(function(require){ var module={} // make module AMD/node compatible...
|
|
|
|
|
/*********************************************************************/
|
|
|
|
|
|
|
|
|
|
var object = require('ig-object')
|
|
|
|
|
|
2020-11-17 21:35:04 +03:00
|
|
|
//var {StopIteration} = require('./Array')
|
|
|
|
|
|
2020-11-02 18:24:20 +03:00
|
|
|
|
2020-11-17 16:12:13 +03:00
|
|
|
|
2020-11-02 18:24:20 +03:00
|
|
|
/*********************************************************************/
|
|
|
|
|
|
2020-11-17 21:35:04 +03:00
|
|
|
// XXX does this need to be a distinct object/constructor???
|
2020-11-16 02:38:19 +03:00
|
|
|
Promise.cooperative = function(){
|
|
|
|
|
var handlers
|
|
|
|
|
return object.mixinFlat(
|
|
|
|
|
new Promise(function(resolve, reject){
|
|
|
|
|
handlers = { resolve, reject, } }),
|
|
|
|
|
{
|
|
|
|
|
get isSet(){
|
|
|
|
|
return handlers === false },
|
|
|
|
|
set: function(value){
|
|
|
|
|
// can't set twice...
|
|
|
|
|
if(this.isSet){
|
|
|
|
|
throw new Error('Promise.cooperative().set(..): can not set twice') }
|
|
|
|
|
// bind to promise...
|
|
|
|
|
if(value && value.then && value.catch){
|
|
|
|
|
value.then(handlers.resolve)
|
|
|
|
|
value.catch(handlers.reject)
|
|
|
|
|
// resolve with value...
|
|
|
|
|
} else {
|
|
|
|
|
handlers.resolve(value) }
|
|
|
|
|
// cleanup and prevent setting twice...
|
|
|
|
|
handlers = false
|
|
|
|
|
return this },
|
|
|
|
|
}) }
|
2020-11-02 18:24:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-09 06:22:32 +03:00
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
// promise iterators...
|
|
|
|
|
|
|
|
|
|
var IterablePromise =
|
|
|
|
|
module.IterablePromise =
|
|
|
|
|
Promise.iter =
|
|
|
|
|
object.Constructor('IterablePromise', Promise, {
|
2020-11-17 21:35:04 +03:00
|
|
|
// XXX do we need this to be public???
|
2020-11-10 01:58:51 +03:00
|
|
|
__list: null,
|
|
|
|
|
|
2020-11-17 21:35:04 +03:00
|
|
|
// iterator methods...
|
|
|
|
|
//
|
|
|
|
|
// These will return a new IterablePromise instance...
|
|
|
|
|
//
|
|
|
|
|
// NOTE: these are different to Array's equivalents in that the handler
|
|
|
|
|
// is called not in the order of the elements but rather in order
|
|
|
|
|
// of promise resolution...
|
|
|
|
|
// NOTE: index of items is unknowable because items can expand on
|
|
|
|
|
// contract depending on handlrs (e.g. .filter(..) can remove
|
|
|
|
|
// items)...
|
2020-11-10 01:58:51 +03:00
|
|
|
map: function(func){
|
2020-11-17 21:35:04 +03:00
|
|
|
return this.constructor(this.__list, function(e){
|
|
|
|
|
return [func(e)] }) },
|
2020-11-16 02:38:19 +03:00
|
|
|
filter: function(func){
|
2020-11-17 21:35:04 +03:00
|
|
|
return this.constructor(this.__list, function(e){
|
|
|
|
|
return func(e) ?
|
2020-11-16 02:38:19 +03:00
|
|
|
[e]
|
|
|
|
|
: [] }) },
|
2020-11-17 16:12:13 +03:00
|
|
|
reduce: function(func, res){
|
2020-11-17 21:35:04 +03:00
|
|
|
return this.constructor(this.__list,
|
|
|
|
|
function(e){
|
|
|
|
|
res = func(res, e)
|
|
|
|
|
return [] })
|
|
|
|
|
.then(function(){
|
|
|
|
|
return res }) },
|
2020-11-17 16:12:13 +03:00
|
|
|
flat: function(depth=1){
|
2020-11-17 21:35:04 +03:00
|
|
|
return this.constructor(this.__list, function(e){
|
2020-11-17 16:12:13 +03:00
|
|
|
return (e && e.flat) ?
|
|
|
|
|
e.flat(depth)
|
|
|
|
|
: e }) },
|
|
|
|
|
|
2020-11-10 01:58:51 +03:00
|
|
|
|
2020-11-17 21:35:04 +03:00
|
|
|
// NOTE: these return a Promise instance...
|
|
|
|
|
then: function(handler){
|
|
|
|
|
var that = this
|
|
|
|
|
return new Promise(function(resolve, reject){
|
|
|
|
|
// then...
|
|
|
|
|
object.parentCall(IterablePromise.prototype.then, that,
|
|
|
|
|
function(){
|
|
|
|
|
resolve(handler.call(this, ...arguments)) })
|
|
|
|
|
that.catch(reject) }) },
|
|
|
|
|
catch: function(handler){
|
|
|
|
|
var that = this
|
|
|
|
|
return new Promise(function(resolve, reject){
|
|
|
|
|
that.then(resolve)
|
|
|
|
|
// catch...
|
|
|
|
|
object.parentCall(IterablePromise.prototype.catch, that,
|
|
|
|
|
function(){
|
|
|
|
|
reject(handler.call(this, ...arguments)) }) }) },
|
|
|
|
|
finally: function(handler){
|
|
|
|
|
var that = this
|
|
|
|
|
return new Promise(function(resolve, reject){
|
|
|
|
|
// bind promise state to that...
|
|
|
|
|
that.then(resolve)
|
|
|
|
|
that.catch(reject)
|
|
|
|
|
// finally...
|
|
|
|
|
object.parentCall(IterablePromise.prototype.finally, that,
|
|
|
|
|
function(){
|
|
|
|
|
handler.call(this, ...arguments) }) }) },
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Promise.iter([ .. ])
|
|
|
|
|
// -> iterable-promise
|
|
|
|
|
//
|
|
|
|
|
// Promise.iter([ .. ], handler)
|
|
|
|
|
// -> iterable-promise
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// handler(e)
|
2020-11-16 02:38:19 +03:00
|
|
|
// -> [value]
|
|
|
|
|
// -> []
|
|
|
|
|
//
|
2020-11-17 21:35:04 +03:00
|
|
|
//
|
|
|
|
|
// NOTE: element index is unknowable untill the full list is expanded
|
|
|
|
|
// as handler(..)'s return value can expand to any number of
|
|
|
|
|
// items...
|
|
|
|
|
// XXX we can make the index a promise, then if the client needs
|
|
|
|
|
// the value they can wait for it...
|
|
|
|
|
__new__: function(_, list, handler){
|
2020-11-17 16:12:13 +03:00
|
|
|
var promise
|
|
|
|
|
|
2020-11-10 01:58:51 +03:00
|
|
|
// instance...
|
|
|
|
|
var obj = Reflect.construct(IterablePromise.__proto__, [
|
|
|
|
|
function(resolve, reject){
|
|
|
|
|
// NOTE: this is here for Promise compatibilty...
|
|
|
|
|
if(typeof(list) == 'function'){
|
2020-11-17 16:12:13 +03:00
|
|
|
return list.call(this, ...arguments) }
|
|
|
|
|
promise = {resolve, reject} }],
|
2020-11-10 01:58:51 +03:00
|
|
|
IterablePromise)
|
|
|
|
|
|
2020-11-17 16:12:13 +03:00
|
|
|
if(promise){
|
|
|
|
|
// apply handler(..) to the list...
|
2020-11-17 21:35:04 +03:00
|
|
|
// NOTE: the top level promises are not wrapped in arrays...
|
2020-11-17 16:12:13 +03:00
|
|
|
// XXX should this be aborted on reject???
|
2020-11-17 21:35:04 +03:00
|
|
|
// ...need to be able to do a deep abort...
|
|
|
|
|
list =
|
2020-11-17 16:12:13 +03:00
|
|
|
handler ?
|
|
|
|
|
list.map(function(block){
|
|
|
|
|
return (block instanceof Array ?
|
|
|
|
|
block
|
|
|
|
|
: [block])
|
|
|
|
|
.map(function(e){
|
|
|
|
|
// NOTE: we are counting actual expanded
|
|
|
|
|
// values and not the "blocks"...
|
|
|
|
|
return (e && e.then && e.catch) ?
|
|
|
|
|
// promise...
|
|
|
|
|
e.then(function(v){
|
2020-11-17 21:35:04 +03:00
|
|
|
return handler(v) })
|
2020-11-17 16:12:13 +03:00
|
|
|
// basic value...
|
2020-11-17 21:35:04 +03:00
|
|
|
: handler(e) }) })
|
2020-11-17 16:12:13 +03:00
|
|
|
.flat()
|
2020-11-17 21:35:04 +03:00
|
|
|
: handler === undefined ?
|
|
|
|
|
list.map(function(e){
|
2020-11-17 16:12:13 +03:00
|
|
|
return e instanceof Promise ?
|
|
|
|
|
e
|
|
|
|
|
: [e] })
|
2020-11-17 21:35:04 +03:00
|
|
|
: list.slice()
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(obj, '__list', {
|
|
|
|
|
value: list,
|
|
|
|
|
enumerable: false,
|
|
|
|
|
})
|
2020-11-17 16:12:13 +03:00
|
|
|
|
|
|
|
|
// handle promise state...
|
|
|
|
|
Promise.all(list)
|
|
|
|
|
.then(function(res){
|
|
|
|
|
promise.resolve(res.flat()) })
|
|
|
|
|
// XXX do we need to pass the results here???
|
|
|
|
|
.catch(promise.reject) }
|
|
|
|
|
|
2020-11-10 01:58:51 +03:00
|
|
|
return obj },
|
2020-11-09 06:22:32 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-11-02 18:24:20 +03:00
|
|
|
|
|
|
|
|
/**********************************************************************
|
|
|
|
|
* vim:set ts=4 sw=4 : */ return module })
|