From 6076c1ac22224f9b36c574b7ab4ba3e39df3a7ef Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 16 Dec 2020 05:17:27 +0300 Subject: [PATCH] split queue into a generic and a bit more restrictive models, not sure about naming yet... Signed-off-by: Alex A. Naanou --- package.json | 2 +- runner.js | 109 +++++++++++++++++++++++++++------------------------ 2 files changed, 58 insertions(+), 53 deletions(-) diff --git a/package.json b/package.json index 0e8dd45..49e9afb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "5.1.0", + "version": "6.0.0", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": { diff --git a/runner.js b/runner.js index 1a2a128..1731bbf 100644 --- a/runner.js +++ b/runner.js @@ -188,13 +188,6 @@ object.Constructor('Queue', Array, { if(this.state == 'stopped' || this.state == 'aborted'){ return handle(false) } this.__state = 'stopped' }), - abort: events.Event('abort', function(handle){ - console.log('Queue.abort(..)') - // abort only once... - if(this.state == 'aborted'){ - return handle(false) } - this.__state = 'aborted' - /*this.clear('true')*/ }), // events... // @@ -210,41 +203,6 @@ object.Constructor('Queue', Array, { queueEmpty: events.PureEvent('queueEmpty'), - // NOTE: each handler will get called once when the next time the - // queue is emptied... - // XXX should this trigger on empty or on stop??? - promise: function(){ - var that = this - return new Promise(function(resolve, reject){ - that - .one('queueEmpty', function(){ - resolve(...(that.collect_results ? - [that.__results || []] - : [])) }) - .one('abort', reject) }) }, - then: function(onresolve, onreject){ - var that = this - return new Promise(function(resolve, reject){ - // got a queue/promise... - if(onresolve instanceof Queue - || onresolve instanceof Promise){ - onresolve.then(resolve, reject) - // got functions... - } else { - onreject - && that.one('abort', function(){ - reject(onreject(this)) }) - that.one('queueEmpty', function(){ - resolve(onresolve( - ...(this.collect_results ? - [(this.__results || [])] - : []) )) }) } }) }, - catch: function(func){ - var that = this - return this.then( - function(res){ return res }, - func) }, - // Runner API... // // Run the given task type... @@ -338,15 +296,7 @@ object.Constructor('Queue', Array, { ;(this.length == 0 && this.auto_stop) ? // auto-stop... - (timeout != null ? - // wait a bit then stop if still empty... - setTimeout(function(){ - that.length > 0 ? - that.__run_tasks__() - : that.stop() - }, timeout) - // stop now... - : this.stop()) + this.__onempty__() // pole / pause... : timeout != null && setTimeout( @@ -357,6 +307,17 @@ object.Constructor('Queue', Array, { run() : setTimeout(run, 0)) return this }, + __onempty__: function(){ + this.poling_timeout != null ? + // wait a bit then stop if still empty... + setTimeout(function(){ + that.length > 0 ? + that.__run_tasks__() + : that.stop( + }, this.poling_timeout) + // stop now... + : this.stop() + return this }, // run one task from queue... // NOTE: this does not care about .state... // XXX revise error handling... @@ -559,6 +520,50 @@ object.Constructor('Queue', Array, { })) +// Like Queue(..) but adds terminal states and conversion to promises... +// +// XXX Object.freeze(..) this when done... ??? +// XXX find a better name... +var FinalizableQueue = +module.FinalizableQueue = +object.Constructor('FinalizableQueue', Queue, { + __onempty__: function(){ + return this.trigger('done') }, + + // XXX sould these freeze??? + done: events.Event('done', function(handle){ + // abort only once... + if(this.state == 'aborted' || this.state == 'done'){ + return handle(false) } + this.__state = 'done' + Object.freeze(this) }), + abort: events.Event('abort', function(handle){ + // abort only once... + if(this.state == 'aborted' || this.state == 'done'){ + return handle(false) } + this.__state = 'aborted' + Object.freeze(this) }), + + // NOTE: each handler will get called once when the next time the + // queue is emptied... + // XXX should this trigger on empty or on stop??? + promise: function(){ + var that = this + return new Promise(function(resolve, reject){ + that + .one('done', function(){ + resolve(...(that.collect_results ? + [that.__results || []] + : [])) }) + .one('abort', reject) }) }, + then: function(onresolve, onreject){ + return this.promise().then(...arguments) }, + catch: function(onreject){ + return this.promise().catch(...arguments) }, +}) + + + //--------------------------------------------------------------------- // Task manager... @@ -843,7 +848,7 @@ object.Constructor('TaskManager', Array, events.EventMixin('flat', { var handler = // queue... // NOTE: queue is task-compatible... - task instanceof Queue ? + task instanceof FinalizableQueue ? task // task protocol... : task && task.then