queue-async
Advanced tools
Comparing version 1.0.3 to 1.0.4
{ | ||
"name": "queue-async", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"main": "./queue.js" | ||
} |
{ | ||
"name": "queue-async", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A little helper for asynchronous JavaScript.", | ||
@@ -24,4 +24,4 @@ "keywords": [ | ||
"scripts": { | ||
"test": "node_modules/.bin/vows" | ||
"test": "node_modules/.bin/vows; echo" | ||
} | ||
} |
80
queue.js
(function() { | ||
if (typeof module === "undefined") self.queue = queue; | ||
else module.exports = queue; | ||
queue.version = "1.0.3"; | ||
queue.version = "1.0.4"; | ||
@@ -9,9 +9,9 @@ var slice = [].slice; | ||
function queue(parallelism) { | ||
var queue = {}, | ||
deferrals = [], | ||
started = 0, // number of deferrals that have been started (and perhaps finished) | ||
active = 0, // number of deferrals currently being executed (started but not finished) | ||
remaining = 0, // number of deferrals not yet finished | ||
popping, // inside a synchronous deferral callback? | ||
error, | ||
var q, | ||
tasks = [], | ||
started = 0, // number of tasks that have been started (and perhaps finished) | ||
active = 0, // number of tasks currently being executed (started but not finished) | ||
remaining = 0, // number of tasks not yet finished | ||
popping, // inside a synchronous task callback? | ||
error = null, | ||
await = noop, | ||
@@ -22,33 +22,10 @@ all; | ||
queue.defer = function() { | ||
if (!error) { | ||
deferrals.push(arguments); | ||
++remaining; | ||
pop(); | ||
} | ||
return queue; | ||
}; | ||
queue.await = function(f) { | ||
await = f; | ||
all = false; | ||
if (!remaining) notify(); | ||
return queue; | ||
}; | ||
queue.awaitAll = function(f) { | ||
await = f; | ||
all = true; | ||
if (!remaining) notify(); | ||
return queue; | ||
}; | ||
function pop() { | ||
while (popping = started < deferrals.length && active < parallelism) { | ||
while (popping = started < tasks.length && active < parallelism) { | ||
var i = started++, | ||
d = deferrals[i], | ||
a = slice.call(d, 1); | ||
t = tasks[i], | ||
a = slice.call(t, 1); | ||
a.push(callback(i)); | ||
++active; | ||
d[0].apply(null, a); | ||
t[0].apply(null, a); | ||
} | ||
@@ -62,7 +39,7 @@ } | ||
if (e != null) { | ||
error = e; // ignore new deferrals and squelch active callbacks | ||
started = remaining = NaN; // stop queued deferrals from starting | ||
error = e; // ignore new tasks and squelch active callbacks | ||
started = remaining = NaN; // stop queued tasks from starting | ||
notify(); | ||
} else { | ||
deferrals[i] = r; | ||
tasks[i] = r; | ||
if (--remaining) popping || pop(); | ||
@@ -76,7 +53,28 @@ else notify(); | ||
if (error != null) await(error); | ||
else if (all) await(null, deferrals); | ||
else await.apply(null, [null].concat(deferrals)); | ||
else if (all) await(error, tasks); | ||
else await.apply(null, [error].concat(tasks)); | ||
} | ||
return queue; | ||
return q = { | ||
defer: function() { | ||
if (!error) { | ||
tasks.push(arguments); | ||
++remaining; | ||
pop(); | ||
} | ||
return q; | ||
}, | ||
await: function(f) { | ||
await = f; | ||
all = false; | ||
if (!remaining) notify(); | ||
return q; | ||
}, | ||
awaitAll: function(f) { | ||
await = f; | ||
all = true; | ||
if (!remaining) notify(); | ||
return q; | ||
} | ||
}; | ||
} | ||
@@ -83,0 +81,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(){function n(n){function t(){for(;o=p<a.length&&n>s;){var u=p++,t=a[u],r=l.call(t,1);r.push(e(u)),++s,t[0].apply(null,r)}}function e(n){return function(u,l){--s,null==f&&(null!=u?(f=u,p=d=0/0,r()):(a[n]=l,--d?o||t():r()))}}function r(){null!=f?v(f):i?v(null,a):v.apply(null,[null].concat(a))}var o,f,i,c={},a=[],p=0,s=0,d=0,v=u;return n||(n=1/0),c.defer=function(){return f||(a.push(arguments),++d,t()),c},c.await=function(n){return v=n,i=!1,d||r(),c},c.awaitAll=function(n){return v=n,i=!0,d||r(),c},c}function u(){}"undefined"==typeof module?self.queue=n:module.exports=n,n.version="1.0.3";var l=[].slice})(); | ||
(function(){function n(n){function t(){for(;f=a<c.length&&n>p;){var u=a++,t=c[u],r=l.call(t,1);r.push(e(u)),++p,t[0].apply(null,r)}}function e(n){return function(u,l){--p,null==d&&(null!=u?(d=u,a=s=0/0,r()):(c[n]=l,--s?f||t():r()))}}function r(){null!=d?v(d):i?v(d,c):v.apply(null,[d].concat(c))}var o,f,i,c=[],a=0,p=0,s=0,d=null,v=u;return n||(n=1/0),o={defer:function(){return d||(c.push(arguments),++s,t()),o},await:function(n){return v=n,i=!1,s||r(),o},awaitAll:function(n){return v=n,i=!0,s||r(),o}}}function u(){}"undefined"==typeof module?self.queue=n:module.exports=n,n.version="1.0.4";var l=[].slice})(); |
@@ -24,2 +24,18 @@ # queue.js | ||
## Installation | ||
In a browser, you can use the official hosted copy on [d3js.org](http://d3js.org): | ||
```html | ||
<script src="http://d3js.org/queue.v1.min.js"></script> | ||
``` | ||
In Node, use [NPM](http://npmjs.org) to install: | ||
```bash | ||
npm install queue-async | ||
``` | ||
Note that the package name is [queue-async](https://npmjs.org/package/queue-async); the name “queue” was already taken. | ||
## API Reference | ||
@@ -31,5 +47,5 @@ | ||
### queue.defer(method[, arguments…]) | ||
### queue.defer(task[, arguments…]) | ||
Adds the specified *method* to the queue, with any optional *arguments*. The *method* is called with the optional arguments and a final callback argument, which should be called when the task has finished. | ||
Adds the specified asynchronous *task* function to the queue, with any optional *arguments*. The *task* will be called with the optional arguments and an additional callback argument; the callback should be invoked when the task has finished. Tasks can only be deferred before the *await* callback is set. If a task is deferred after the await callback is set, the behavior of the queue is undefined. | ||
@@ -39,6 +55,6 @@ ### queue.await(callback) | ||
Sets the *callback* to be notified when all deferred tasks have finished. If *await* is used, each result is passed as a separate argument; if *awaitAll* is used, the entire array of results is passed as a single argument. | ||
Sets the *callback* to be invoked when all deferred tasks have finished. The first argument to the *callback* is the first error that occurred, or null if no error occurred. If *await* is used, each result is passed as an additional separate argument; if *awaitAll* is used, the entire array of results is passed as the second argument to the callback. If all callbacks have already been completed by the time the *await* or *awaitAll* callback is set, the callback will be invoked immediately. This method should only be called once, after any tasks have been deferred. If the await callback is set multiple times, or set before a task is deferred, the behavior of the queue is undefined. | ||
## Callbacks | ||
The callbacks follow the Node.js convention where the first argument is an optional error object, and the second is used to pass on the result of an operation. Queue.js does not directly support asynchronous functions that return multiple results; however, you can homogenize such functions by wrapping them and converting multiple results into a single object or array. | ||
The callbacks follow the Node.js convention where the first argument is an optional error object and the second argument is the result of the task. Queue.js does not support asynchronous functions that return multiple results; however, you can homogenize such functions by wrapping them and converting multiple results into a single object or array. |
@@ -26,4 +26,4 @@ var queue = require("../queue"); | ||
"scripts": { | ||
"test": "node_modules/.bin/vows" | ||
"test": "node_modules/.bin/vows; echo" | ||
} | ||
}, null, 2)); |
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
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
9017
109
58