vow-queue
Advanced tools
Comparing version 0.2.1 to 0.3.0
120
lib/queue.js
/** | ||
* @module vow-queue | ||
* @author Filatov Dmitry <dfilatov@yandex-team.ru> | ||
* @version 0.2.1 | ||
* @version 0.3.0 | ||
* @license | ||
@@ -11,4 +11,7 @@ * Dual licensed under the MIT and GPL licenses: | ||
var vow = require('vow'), | ||
extend = function() { | ||
(function() { | ||
function getModule(vow, nextTick) { | ||
var extend = function() { | ||
var res = {}; | ||
@@ -27,3 +30,2 @@ | ||
}, | ||
nextTick = typeof setImmediate !== 'undefined'? setImmediate : process.nextTick, | ||
@@ -36,22 +38,27 @@ DEFAULT_QUEUE_PARAMS = { | ||
priority : 1 | ||
}, | ||
}; | ||
/** | ||
* @class Queue | ||
* @exports vow-queue | ||
*/ | ||
/** | ||
* @class Queue | ||
* @exports vow-queue | ||
*/ | ||
/** | ||
* @constructor | ||
* @param {Object} [params] | ||
* @param {Number} [params.weightLimit=100] | ||
*/ | ||
Queue = module.exports = function(params) { | ||
this._pendingTasks = []; | ||
this._params = extend(DEFAULT_QUEUE_PARAMS, params); | ||
this._curWeight = 0; | ||
this._isRunScheduled = false; | ||
this._isStopped = true; | ||
this._processedBuffer = []; | ||
/** | ||
* @constructor | ||
* @param {Object} [params] | ||
* @param {Number} [params.weightLimit=100] | ||
*/ | ||
function Queue(params) { | ||
this._pendingTasks = []; | ||
this._params = extend(DEFAULT_QUEUE_PARAMS, params); | ||
this._curWeight = 0; | ||
this._isRunScheduled = false; | ||
this._isStopped = true; | ||
this._processedBuffer = []; | ||
this._stats = { | ||
pendingTasksCount : 0, | ||
processingTasksCount : 0, | ||
processedTasksCount : 0 | ||
}; | ||
} | ||
@@ -81,2 +88,9 @@ Queue.prototype = /** @lends Queue.prototype */ { | ||
task.defer.promise().always( | ||
function() { | ||
this._stats.processingTasksCount--; | ||
this._stats.processedTasksCount++; | ||
}, | ||
this); | ||
return task.defer.promise(); | ||
@@ -135,2 +149,6 @@ }, | ||
getStats : function() { | ||
return this._stats; | ||
}, | ||
_buildTask : function(taskFn, taskParams) { | ||
@@ -148,2 +166,4 @@ return { | ||
this._stats.pendingTasksCount++; | ||
while(i) { | ||
@@ -187,18 +207,50 @@ if(pendingTasks[i - 1].params.priority >= task.params.priority) { | ||
this._stats.pendingTasksCount--; | ||
this._stats.processingTasksCount++; | ||
var taskRes = vow.invoke(task.fn); | ||
taskRes.always( | ||
function() { | ||
this._curWeight -= task.params.weight; | ||
if(this._isStopped) { | ||
this._processedBuffer.push(function() { | ||
taskRes | ||
.progress( | ||
task.defer.notify, | ||
task.defer) | ||
.always( | ||
function() { | ||
this._curWeight -= task.params.weight; | ||
if(this._isStopped) { | ||
this._processedBuffer.push(function() { | ||
task.defer.resolve(taskRes); | ||
}); | ||
} | ||
else { | ||
task.defer.resolve(taskRes); | ||
}); | ||
} | ||
else { | ||
task.defer.resolve(taskRes); | ||
this._scheduleRun(); | ||
} | ||
}, | ||
this); | ||
this._scheduleRun(); | ||
} | ||
}, | ||
this); | ||
} | ||
}; | ||
return Queue; | ||
} | ||
var nextTick = typeof setImmediate !== 'undefined'? | ||
setImmediate : | ||
typeof process === 'object' && process.nextTick? | ||
process.nextTick : | ||
function(fn) { | ||
setTimeout(fn, 0); | ||
}; | ||
if(typeof modules !== 'undefined') { | ||
/* global modules */ | ||
modules.define('vow-queue', ['vow'], function(provide, vow) { | ||
provide(getModule(vow, nextTick)); | ||
}); | ||
} | ||
else if(typeof exports === 'object') { | ||
module.exports = getModule(require('vow'), nextTick); | ||
} | ||
})(); |
@@ -5,3 +5,3 @@ { | ||
"description": "Vow-based task queue", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"contributors": [ | ||
@@ -8,0 +8,0 @@ { |
12087
13
237