From bbf9d739f0c9c8892a38835306dcc1c2486cbb66 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 24 Nov 2020 06:00:51 +0300 Subject: [PATCH] added sync_start option to Queue... Signed-off-by: Alex A. Naanou --- package.json | 2 +- runner.js | 61 +++++++++++++++++++++++++++++----------------------- test.js | 17 +++++++++++++-- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 8287353..cba8595 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "5.0.2", + "version": "5.0.3", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": { diff --git a/runner.js b/runner.js index 74dd9f9..707e8eb 100644 --- a/runner.js +++ b/runner.js @@ -54,6 +54,9 @@ object.Constructor('Queue', Array, { auto_stop: false, + // NOTE: this is sync only untill the pool is filled... + sync_start: false, + // // This can be: // 'wait' - wait fot the sun-queue to stop @@ -110,7 +113,6 @@ object.Constructor('Queue', Array, { // NOTE: each handler will get called once when the next time the // queue is emptied... - // XXX revise... then: function(func){ var that = this return new Promise(function(resolve, reject){ @@ -164,33 +166,38 @@ object.Constructor('Queue', Array, { __running: null, __run_tasks__: function(){ var that = this - this.state == 'running' - && setTimeout(function(){ - // handle queue... - while(this.length > 0 - && this.state == 'running' - && (this.__running || []).length < (this.pool_size || Infinity) ){ - this.runTask(this.__run_tasks__.bind(this)) } - // empty queue -> pole or stop... - // - // NOTE: we endup here in two cases: - // - the pool is full - // - the queue is empty - // NOTE: we do not care about stopping the timer when changing - // state as .__run_tasks__() will stop itself... - // - // XXX will this be collected by the GC if it is polling??? - if(this.length == 0 - && this.state == 'running'){ - this.auto_stop ? - // auto-stop... - this.stop() - // pole... - : (this.poling_delay - && setTimeout( - this.__run_tasks__.bind(this), - this.poling_delay || 200)) } }.bind(this), 0) + var run = function(){ + // handle queue... + while(this.length > 0 + && this.state == 'running' + && (this.__running || []).length < (this.pool_size || Infinity) ){ + this.runTask(this.__run_tasks__.bind(this)) } + + // empty queue -> pole or stop... + // + // NOTE: we endup here in two cases: + // - the pool is full + // - the queue is empty + // NOTE: we do not care about stopping the timer when changing + // state as .__run_tasks__() will stop itself... + // + // XXX will this be collected by the GC if it is polling??? + if(this.length == 0 + && this.state == 'running'){ + this.auto_stop ? + // auto-stop... + this.stop() + // pole... + : (this.poling_delay + && setTimeout( + this.__run_tasks__.bind(this), + this.poling_delay || 200)) } }.bind(this) + + this.state == 'running' + && (this.sync_start ? + run() + : setTimeout(run, 0)) return this }, // run one task from queue... diff --git a/test.js b/test.js index 93dd5a0..44ab40d 100755 --- a/test.js +++ b/test.js @@ -525,12 +525,25 @@ Runner.cases({ assert(q.length == 3, '.length is 3') - // for some reason this runs the tasks above multiple times... - q.start() + q.runTask() + q.runTask() + q.runTask() assert.array(tasks_run, ['a', 'b', 'c'], 'all tasks run') + // XXX need to figure out how to test async... + tasks_run = [] + + var q = assert(runner.Queue({sync_start: true}), 'Queue({sync_start: true})') + + q.push(a) + q.push(b) + q.push(c) + + q.start() + + assert.array(tasks_run, ['a', 'b', 'c'], 'all tasks run') //console.log('\n>>>', q.state)