From 7cd162fe500ddf8aa745e24fe32fda6cd0384dd5 Mon Sep 17 00:00:00 2001 From: "Harish.K" Date: Tue, 30 Sep 2014 07:28:39 +0530 Subject: [PATCH] feature: real pause and resume functions instead of dummy pause and resume pause jobmanager if tasks are finished and loading is taking place and resume it after loading --- src/JobManager.js | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/JobManager.js b/src/JobManager.js index f7c7651..a52ee84 100644 --- a/src/JobManager.js +++ b/src/JobManager.js @@ -2,7 +2,7 @@ var utils = require('./utils'); -var STATE = utils.createEnum([ 'RUNNING' , 'NOT_RUNNING']); +var STATE = utils.createEnum([ 'RUNNING', 'PAUSED', 'NOT_RUNNING']); function JobManager(opts){ @@ -75,33 +75,37 @@ JobManager.prototype.getFromPool = function(){ JobManager.prototype.$doWork_ = function( cb ){ var self = this; - var task = self.tasks.splice(0, 1)[0]; + var task = this.tasks.splice(0, 1)[0]; + if(self.tasks.length/self.concurrency < self.notifyAt && (!self.isLoadingTakingPlace) ){ + if( self.onLoadMore && !self.endReached ) { + self.$onLoadMore(); + } + } if( task == undefined ){ - this.stop(); + if(this.isLoadingTakingPlace){ + this.pause(); + } else { + this.stop(); + } return; } - var worker = self.getFromPool(); + var worker = this.getFromPool(); this.work( task, worker, function(err){ if(err && self.onError ){ self.onError(err, task, worker ); } - if(self.tasks.length/self.concurrency < self.notifyAt && (!self.isLoadingTakingPlace) ){ - if( self.onLoadMore && !self.endReached ) { - self.$onLoadMore(); - } - } - cb(); - if( self.state == STATE.NOT_RUNNING ){ - if( ( self.runningTasks == 1 ) && self.onStopped ) { self.onStopped(); } - } self.returnToPool( worker ); + if( self.state === STATE.NOT_RUNNING ){ + if( ( self.runningTasks === 0 ) && self.onStopped ) { self.onStopped(); } + } + return cb(); }); }; JobManager.prototype.$trigger = function (){ var self = this; - while( this.runningTasks < this.concurrency && this.state == STATE.RUNNING ){ + while( this.runningTasks < this.concurrency && this.state === STATE.RUNNING ){ this.$doWork_( function(){ self.$trigger(); }); @@ -126,16 +130,22 @@ JobManager.prototype.$onLoadMore = function( cb ){ self.isLoadingTakingPlace = true; self.onLoadMore(function(){ self.isLoadingTakingPlace = false; + if(self.state === STATE.PAUSED ){ + self.resume(); + } return cb(); }); }; JobManager.prototype.pause = function(){ - this.state = STATE.NOT_RUNNING; + this.state = STATE.PAUSED; }; JobManager.prototype.stop = function(){ this.state = STATE.NOT_RUNNING; + if( ( this.tasks.length === 0 ) && ( this.runningTasks === 0 ) && ( !this.isLoadingTakingPlace ) ){ + this.onStopped(); + } };