From cfbf6f667e00fcc0350d295ed6a85e79da31529d Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Mon, 13 Jun 2022 03:27:47 +0300 Subject: [PATCH] experimenting with STOP... Signed-off-by: Alex A. Naanou --- Array.js | 3 ++- Promise.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Array.js b/Array.js index 5a1edf7..f49543e 100644 --- a/Array.js +++ b/Array.js @@ -139,7 +139,8 @@ var makeChunkIter = function(iter, wrapper){ try { // handle iteration... res.push( - val = (chunk = chunks.shift())[iter](_wrapper, ...rest)) + val = (chunk = chunks.shift()) + [iter](_wrapper, ...rest)) // handle chunk... postChunk && postChunk.call(that, diff --git a/Promise.js b/Promise.js index e647906..5c74d88 100644 --- a/Promise.js +++ b/Promise.js @@ -131,11 +131,58 @@ object.Constructor('IterablePromise', Promise, { handler = handler ?? function(elem){ return [elem] } + + // XXX EXPEREMENTAL... + var stoppable = !!Array.STOP + var stop = false + var map = stoppable ? + 'smap' + : 'map' + var _handler = handler + handler = !handler.stoppable ? + Object.assign( + function(){ + try{ + console.log('####', stop) + return !stop ? + _handler(...arguments) + : [] + }catch(err){ + stop = err + if(err === Array.STOP + || err instanceof Array.STOP){ + return 'value' in err ? + err.value + : [] } + throw err } }, + // prevent double+ wrapping... + // XXX revise nameing... + { stoppable: true }) + : handler + return [list].flat() - .map(function(elem){ + // XXX supporting STOP here would require both using .smap(..) + // and tracking STOPs thrown in promises -- this could + // throw things out of sync for us not to be able to + // guarantee a full stop if a throw happened in a delayed + // handler effectively making the handlers race for the + // throw... + // ...not sure if this is good or not... + //.map(function(elem){ + [map](function(elem){ return elem && elem.then ? //that.__pack(elem, handler) - elem.then(handler) + //elem.then(handler) + elem + .then(handler) + .catch(function(err){ + stop = err + if(err === Array.STOP + || err instanceof Array.STOP){ + return 'value' in err ? + err.value + : [] } + throw err }) : elem instanceof Array ? handler(elem) // NOTE: we keep things that do not need protecting @@ -159,7 +206,12 @@ object.Constructor('IterablePromise', Promise, { // NOTE: since each section of the packed .__array is the same // structure as the input we'll use .__pack(..) to handle // them, this also keeps all the handling code in one place. - return list.map(function(elem){ + // XXX EXPEREMENTAL... + var map = !!Array.STOP ? + 'smap' + : 'map' + //return list.map(function(elem){ + return list[map](function(elem){ return elem instanceof Array ? that.__pack(elem, handler) : elem instanceof Promise ?