added sync_start option to Queue...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-24 06:00:51 +03:00
parent ee91cd2601
commit bbf9d739f0
3 changed files with 50 additions and 30 deletions

View File

@ -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": {

View File

@ -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...

17
test.js
View File

@ -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)