Comparing version 0.0.2 to 0.0.3
@@ -0,1 +1,10 @@ | ||
/*! | ||
* quargo | ||
* https://github.com/pierreliefauche/quargo | ||
* | ||
* Copyright 2014 Pierre-Élie Fauché | ||
* Released under the MIT license | ||
*/ | ||
/*jshint onevar: false, indent:2 */ | ||
/*global setTimeout: false */ | ||
@@ -5,9 +14,21 @@ module.exports = function(worker, capacity, concurrency, delay) { | ||
if (typeof capacity === 'object') { | ||
config = capacity; | ||
/** | ||
* Initialization, async-like or with options object | ||
*/ | ||
if (typeof worker === 'function') { | ||
if (typeof capacity === 'object') { | ||
// quargo(worker, config); | ||
config = capacity; | ||
} | ||
else { | ||
// quargo(worker, capacity, concurrency, delay); | ||
config.capacity = capacity; | ||
config.concurrency = concurrency; | ||
config.delay = delay; | ||
} | ||
} | ||
else { | ||
config.capacity = capacity; | ||
config.concurrency = concurrency; | ||
config.delay = delay; | ||
// quargo(config, worker); | ||
config = worker; | ||
worker = capacity; | ||
} | ||
@@ -17,3 +38,3 @@ | ||
var self = { | ||
capacity: config.capacity || null, | ||
capacity: config.capacity || 1, | ||
delay: config.delay || 0, | ||
@@ -27,7 +48,14 @@ concurrency: config.concurrency || 1, | ||
empty: null, | ||
drain: null, | ||
empty: config.empty || null, | ||
drain: config.drain || null, | ||
}; | ||
/** | ||
* Add a task or an array of tasks to the quargo | ||
* | ||
* @param {Object|Array|Number|String} data Task or array of tasks | ||
* @param {Function} callback Optional callback called when task has been processed | ||
* @return {Object} Self for chaining | ||
*/ | ||
self.push = function(data, callback) { | ||
@@ -45,5 +73,10 @@ [].concat(data).forEach(function(task) { | ||
self.mayProcess(); | ||
setTimeout(self.mayProcess, 0); | ||
return self; | ||
}; | ||
/** | ||
* Timer expired, process tasks even if capacity is not reached | ||
*/ | ||
self.shouldProcess = function() { | ||
@@ -53,2 +86,9 @@ self.mayProcess(true); | ||
/** | ||
* Trigger processing if workers are available and | ||
* - capacity is reached | ||
* - or tasks should be processed ASAP | ||
* | ||
* @param {Boolean} urgent A batch should be processed ASAP | ||
*/ | ||
self.mayProcess = function(urgent) { | ||
@@ -59,2 +99,3 @@ if (urgent) { | ||
// Try to process while at least one worker is available | ||
while (self.processing < self.concurrency) { | ||
@@ -74,2 +115,6 @@ if (self.urging > 0) { | ||
/** | ||
* Process a batch of tasks. | ||
* Tries to process as much as possible, up to `capacity`. | ||
*/ | ||
self.process = function() { | ||
@@ -104,3 +149,3 @@ self.processing++; | ||
self.mayProcess(); | ||
setTimeout(self.mayProcess, 0); | ||
}); | ||
@@ -110,2 +155,6 @@ }; | ||
/** | ||
* Number of tasks waiting to be processed | ||
* @return {Number} | ||
*/ | ||
self.length = function() { | ||
@@ -115,2 +164,6 @@ return self.tasks.length; | ||
/** | ||
* Are any tasks waiting to be processed or being currently processed? | ||
* @return {Boolean} | ||
*/ | ||
self.idle = function() { | ||
@@ -120,2 +173,6 @@ return self.tasks.length === 0 && self.processing === 0; | ||
/** | ||
* Are any tasks being currently processed? | ||
* @return {Boolean} | ||
*/ | ||
self.running = function() { | ||
@@ -125,8 +182,3 @@ return self.processing > 0; | ||
return self; | ||
}; |
{ | ||
"name": "quargo", | ||
"description": "Queued cargo, with load optimization", | ||
"version": "0.0.2", | ||
"description": "A cargo with queue-like parallel processing and capacity optimization.", | ||
"version": "0.0.3", | ||
"main": "./lib/quargo", | ||
@@ -30,4 +30,5 @@ "author": "Pierre-Élie Fauché <github@pierre-elie.me>", | ||
"scripts": { | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha --check-leaks test/*" | ||
"test": "NODE_ENV=test ./node_modules/.bin/mocha --check-leaks test/*", | ||
"test-watch": "NODE_ENV=test ./node_modules/.bin/mocha --watch --check-leaks test/*" | ||
} | ||
} |
# Quargo | ||
A mix of [async](https://github.com/caolan/async)’s [`queue`](https://github.com/caolan/async#queue) and [`cargo`](https://github.com/caolan/async#cargo) with capacity optimization. | ||
A [`cargo`](https://github.com/caolan/async#cargo) with [`queue`](https://github.com/caolan/async#queue)-like parallel processing and capacity optimization. | ||
@@ -9,3 +9,3 @@ [![NPM](https://nodei.co/npm/quargo.png)](https://nodei.co/npm/quargo/) | ||
### quargo(worker, [capacity, [concurrency, [delay]]]) | ||
### quargo(worker, capacity, [concurrency, [delay]]) | ||
@@ -25,3 +25,3 @@ Creates a `quargo` object with the specified `capacity`, `concurrency` and `delay`. Tasks added to the `quargo` will be processed altogether (up to the `capacity` limit) in parallel batches (up to the `concurrency` limit). If all workers are in progress, the task is queued until one becomes available. If the `quargo` hasn’t reached `capacity`, the task is queued for `delay` milliseconds. Once a worker has completed some tasks, each callback of those tasks is called. | ||
- `worker(tasks, callback)` - An asynchronous function for processing an array of queued tasks, which must call its `callback(err)` argument when finished, with an optional `err` argument. | ||
- `capacity` - An optional integer for determining how many tasks should be processed per round; if omitted, the default is unlimited. | ||
- `capacity` - An integer for determining how many tasks should be processed per round. | ||
- `concurrency` - An optional integer for determining how many worker functions should be run in parallel; if omitted, the default is `1`. | ||
@@ -44,2 +44,24 @@ - `delay` - An optional integer for determining how long should the `quargo` wait to reach `capacity`; if omitted, the default is `0`. | ||
### Initialization | ||
#### Async-like | ||
- `quargo(worker, capacity)` | ||
- `quargo(worker, capacity, concurrency)` | ||
- `quargo(worker, capacity, concurrency, delay)` | ||
#### With options | ||
- `quargo(worker, options)` | ||
- `quargo(options, worker)` | ||
Possible `options` are | ||
- `capacity` | ||
- `concurrency` | ||
- `delay` | ||
- `empty` callback | ||
- `drain` callback | ||
--- | ||
### Compared to Async | ||
@@ -46,0 +68,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27650
712
113
1