pax_global_header 0000666 0000000 0000000 00000000064 13104374662 0014520 g ustar 00root root 0000000 0000000 52 comment=1d1353f628e7e5a1b20d8aec917eb44f0bd86fdc
d3-queue-3.0.7/ 0000775 0000000 0000000 00000000000 13104374662 0013157 5 ustar 00root root 0000000 0000000 d3-queue-3.0.7/.eslintrc 0000664 0000000 0000000 00000000217 13104374662 0015003 0 ustar 00root root 0000000 0000000 parserOptions:
sourceType: "module"
env:
es6: true
browser: true
extends:
"eslint:recommended"
rules:
no-cond-assign: 0
d3-queue-3.0.7/.gitignore 0000664 0000000 0000000 00000000100 13104374662 0015136 0 ustar 00root root 0000000 0000000 *.sublime-workspace
.DS_Store
build/
node_modules
npm-debug.log
d3-queue-3.0.7/.npmignore 0000664 0000000 0000000 00000000036 13104374662 0015155 0 ustar 00root root 0000000 0000000 *.sublime-*
build/*.zip
test/
d3-queue-3.0.7/LICENSE 0000664 0000000 0000000 00000002625 13104374662 0014171 0 ustar 00root root 0000000 0000000 Copyright (c) 2012-2016, Michael Bostock
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* The name Michael Bostock may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL MICHAEL BOSTOCK BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d3-queue-3.0.7/README.md 0000664 0000000 0000000 00000024156 13104374662 0014446 0 ustar 00root root 0000000 0000000 # d3-queue
A **queue** evaluates zero or more *deferred* asynchronous tasks with configurable concurrency: you control how many tasks run at the same time. When all the tasks complete, or an error occurs, the queue passes the results to your *await* callback. This library is similar to [Async.js](https://github.com/caolan/async)’s [parallel](https://github.com/caolan/async#paralleltasks-callback) (when *concurrency* is infinite), [series](https://github.com/caolan/async#seriestasks-callback) (when *concurrency* is 1), and [queue](https://github.com/caolan/async#queue), but features a much smaller footprint: as of release 2, d3-queue is about 700 bytes gzipped, compared to 4,300 for Async.
Each task is defined as a function that takes a callback as its last argument. For example, here’s a task that says hello after a short delay:
```js
function delayedHello(callback) {
setTimeout(function() {
console.log("Hello!");
callback(null);
}, 250);
}
```
When a task completes, it must call the provided callback. The first argument to the callback should be null if the task is successful, or the error if the task failed. The optional second argument to the callback is the return value of the task. (To return multiple values from a single callback, wrap the results in an object or array.)
To run multiple tasks simultaneously, create a queue, *defer* your tasks, and then register an *await* callback to be called when all of the tasks complete (or an error occurs):
```js
var q = d3.queue();
q.defer(delayedHello);
q.defer(delayedHello);
q.await(function(error) {
if (error) throw error;
console.log("Goodbye!");
});
```
Of course, you can also use a `for` loop to defer many tasks:
```js
var q = d3.queue();
for (var i = 0; i < 1000; ++i) {
q.defer(delayedHello);
}
q.awaitAll(function(error) {
if (error) throw error;
console.log("Goodbye!");
});
```
Tasks can take optional arguments. For example, here’s how to configure the delay before hello and provide a name:
```js
function delayedHello(name, delay, callback) {
setTimeout(function() {
console.log("Hello, " + name + "!");
callback(null);
}, delay);
}
```
Any additional arguments provided to [*queue*.defer](#queue_defer) are automatically passed along to the task function before the callback argument. You can also use method chaining for conciseness, avoiding the need for a local variable:
```js
d3.queue()
.defer(delayedHello, "Alice", 250)
.defer(delayedHello, "Bob", 500)
.defer(delayedHello, "Carol", 750)
.await(function(error) {
if (error) throw error;
console.log("Goodbye!");
});
```
The [asynchronous callback pattern](https://github.com/maxogden/art-of-node#callbacks) is very common in Node.js, so Queue works directly with many Node APIs. For example, to [stat two files](https://nodejs.org/dist/latest/docs/api/fs.html#fs_fs_stat_path_callback) concurrently:
```js
d3.queue()
.defer(fs.stat, __dirname + "/../Makefile")
.defer(fs.stat, __dirname + "/../package.json")
.await(function(error, file1, file2) {
if (error) throw error;
console.log(file1, file2);
});
```
You can also make abortable tasks: these tasks return an object with an *abort* method which terminates the task. So, if a task calls [setTimeout](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout) on start, it can call [clearTimeout](https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/clearTimeout) on abort. For example:
```js
function delayedHello(name, delay, callback) {
var id = setTimeout(function() {
console.log("Hello, " + name + "!");
callback(null);
}, delay);
return {
abort: function() {
clearTimeout(id);
}
};
}
```
When you call [*queue*.abort](#queue_abort), any in-progress tasks will be immediately aborted; in addition, any pending (not-yet-started) tasks will not be started. Note that you can also use *queue*.abort *without* abortable tasks, in which case pending tasks will be cancelled, though active tasks will continue to run. Conveniently, the [d3-request](https://github.com/d3/d3-request) library implements abort atop XMLHttpRequest. For example:
```js
var q = d3.queue()
.defer(d3.request, "http://www.google.com:81")
.defer(d3.request, "http://www.google.com:81")
.defer(d3.request, "http://www.google.com:81")
.awaitAll(function(error, results) {
if (error) throw error;
console.log(results);
});
```
To abort these requests, call `q.abort()`.
## Installing
If you use NPM, `npm install d3-queue`. If you use Bower, `bower install d3-queue`. Otherwise, download the [latest release](https://github.com/d3/d3-queue/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-queue.v3.min.js) or as part of [D3 4.0](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported:
```html
```
[Try d3-queue in your browser.](https://tonicdev.com/npm/d3-queue)
## API Reference
# d3.queue([concurrency]) [<>](https://github.com/d3/d3-queue/blob/master/src/queue.js "Source")
Constructs a new queue with the specified *concurrency*. If *concurrency* is not specified, the queue has infinite concurrency. Otherwise, *concurrency* is a positive integer. For example, if *concurrency* is 1, then all tasks will be run in series. If *concurrency* is 3, then at most three tasks will be allowed to proceed concurrently; this is useful, for example, when loading resources in a web browser.
# queue.defer(task[, arguments…]) [<>](https://github.com/d3/d3-queue/blob/master/src/queue.js#L20 "Source")
Adds the specified asynchronous *task* callback to the queue, with any optional *arguments*. The *task* is a function that will be called when the task should start. It is passed the specified optional *arguments* and an additional *callback* as the last argument; the callback must be invoked by the task when it finishes. The task must invoke the callback with two arguments: the *error*, if any, and the *result* of the task. To return multiple results from a single callback, wrap the results in an object or array.
For example, here’s a task which computes the answer to the ultimate question of life, the universe, and everything after a short delay:
```js
function simpleTask(callback) {
setTimeout(function() {
callback(null, {answer: 42});
}, 250);
}
```
If the task calls back with an error, any tasks that were scheduled *but not yet started* will not run. For a serial queue (of *concurrency* 1), this means that a task will only run if all previous tasks succeed. For a queue with higher concurrency, only the first error that occurs is reported to the await callback, and tasks that were started before the error occurred will continue to run; note, however, that their results will not be reported to the await callback.
Tasks can only be deferred before [*queue*.await](#queue_await) or [*queue*.awaitAll](#queue_awaitAll) is called. If a task is deferred after then, an error is thrown. If the *task* is not a function, an error is thrown.
# queue.abort() [<>](https://github.com/d3/d3-queue/blob/master/src/queue.js#L29 "Source")
Aborts any active tasks, invoking each active task’s *task*.abort function, if any. Also prevents any new tasks from starting, and immediately invokes the [*queue*.await](#queue_await) or [*queue*.awaitAll](#queue_awaitAll) callback with an error indicating that the queue was aborted. See the [introduction](#d3-queue) for an example implementation of an abortable task. Note that if your tasks are not abortable, any running tasks will continue to run, even after the await callback has been invoked with the abort error. The await callback is invoked exactly once on abort, and so is not called when any running tasks subsequently succeed or fail.
# queue.await(callback) [<>](https://github.com/d3/d3-queue/blob/master/src/queue.js#L33 "Source")
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 an error occurred, there are no additional arguments to the callback. Otherwise, the *callback* is passed each result as an additional argument. For example:
```js
d3.queue()
.defer(fs.stat, __dirname + "/../Makefile")
.defer(fs.stat, __dirname + "/../package.json")
.await(function(error, file1, file2) { console.log(file1, file2); });
```
If all [deferred](#queue_defer) tasks have already completed, the callback will be invoked immediately. This method may only be called once, after any tasks have been deferred. If this method is called multiple times, or if it is called after [*queue*.awaitAll](#queue_awaitAll), an error is thrown. If the *callback* is not a function, an error is thrown.
# queue.awaitAll(callback) [<>](https://github.com/d3/d3-queue/blob/master/src/queue.js#L39 "Source")
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 an error occurred, there are no additional arguments to the callback. Otherwise, the *callback* is also passed an array of results as the second argument. For example:
```js
d3.queue()
.defer(fs.stat, __dirname + "/../Makefile")
.defer(fs.stat, __dirname + "/../package.json")
.awaitAll(function(error, files) { console.log(files); });
```
If all [deferred](#queue_defer) tasks have already completed, the callback will be invoked immediately. This method may only be called once, after any tasks have been deferred. If this method is called multiple times, or if it is called after [*queue*.await](#queue_await), an error is thrown. If the *callback* is not a function, an error is thrown.
d3-queue-3.0.7/d3-queue.sublime-project 0000664 0000000 0000000 00000000271 13104374662 0017635 0 ustar 00root root 0000000 0000000 {
"folders": [
{
"path": ".",
"file_exclude_patterns": [
"*.sublime-workspace"
],
"folder_exclude_patterns": [
"build"
]
}
]
}
d3-queue-3.0.7/index.js 0000664 0000000 0000000 00000000056 13104374662 0014625 0 ustar 00root root 0000000 0000000 export {default as queue} from "./src/queue";
d3-queue-3.0.7/package.json 0000664 0000000 0000000 00000003277 13104374662 0015456 0 ustar 00root root 0000000 0000000 {
"name": "d3-queue",
"version": "3.0.7",
"description": "Evaluate asynchronous tasks with configurable concurrency.",
"keywords": [
"d3",
"d3-module",
"asynchronous",
"async",
"queue"
],
"homepage": "https://d3js.org/d3-queue/",
"license": "BSD-3-Clause",
"author": {
"name": "Mike Bostock",
"url": "http://bost.ocks.org/mike"
},
"main": "build/d3-queue.js",
"module": "index",
"jsnext:main": "index",
"repository": {
"type": "git",
"url": "https://github.com/d3/d3-queue.git"
},
"scripts": {
"pretest": "rm -rf build && mkdir build && rollup --banner \"$(preamble)\" -f umd -n d3 -o build/d3-queue.js -- index.js",
"test": "tape 'test/**/*-test.js' && eslint index.js src",
"prepublish": "npm run test && uglifyjs --preamble \"$(preamble)\" build/d3-queue.js -c -m -o build/d3-queue.min.js",
"postpublish": "git push && git push --tags && cd ../d3-queue-bower && git pull && cp -v ../d3-queue/README.md ../d3-queue/LICENSE ../d3-queue/build/d3-queue.js . && git add README.md LICENSE d3-queue.js && git commit -m ${npm_package_version} && git tag -am ${npm_package_version} v${npm_package_version} && git push && git push --tags && cd ../d3.github.com && git pull && cp ../d3-queue/build/d3-queue.js d3-queue.v3.js && cp ../d3-queue/build/d3-queue.min.js d3-queue.v3.min.js && git add d3-queue.v3.js d3-queue.v3.min.js && git commit -m \"d3-queue ${npm_package_version}\" && git push && cd ../d3-queue && zip -j build/d3-queue.zip -- LICENSE README.md build/d3-queue.js build/d3-queue.min.js"
},
"devDependencies": {
"eslint": "3",
"package-preamble": "0.1",
"rollup": "0.41",
"tape": "4",
"uglify-js": "^2.8.11"
}
}
d3-queue-3.0.7/src/ 0000775 0000000 0000000 00000000000 13104374662 0013746 5 ustar 00root root 0000000 0000000 d3-queue-3.0.7/src/array.js 0000664 0000000 0000000 00000000035 13104374662 0015420 0 ustar 00root root 0000000 0000000 export var slice = [].slice;
d3-queue-3.0.7/src/queue.js 0000664 0000000 0000000 00000006070 13104374662 0015433 0 ustar 00root root 0000000 0000000 import {slice} from "./array";
var noabort = {};
function Queue(size) {
this._size = size;
this._call =
this._error = null;
this._tasks = [];
this._data = [];
this._waiting =
this._active =
this._ended =
this._start = 0; // inside a synchronous task callback?
}
Queue.prototype = queue.prototype = {
constructor: Queue,
defer: function(callback) {
if (typeof callback !== "function") throw new Error("invalid callback");
if (this._call) throw new Error("defer after await");
if (this._error != null) return this;
var t = slice.call(arguments, 1);
t.push(callback);
++this._waiting, this._tasks.push(t);
poke(this);
return this;
},
abort: function() {
if (this._error == null) abort(this, new Error("abort"));
return this;
},
await: function(callback) {
if (typeof callback !== "function") throw new Error("invalid callback");
if (this._call) throw new Error("multiple await");
this._call = function(error, results) { callback.apply(null, [error].concat(results)); };
maybeNotify(this);
return this;
},
awaitAll: function(callback) {
if (typeof callback !== "function") throw new Error("invalid callback");
if (this._call) throw new Error("multiple await");
this._call = callback;
maybeNotify(this);
return this;
}
};
function poke(q) {
if (!q._start) {
try { start(q); } // let the current task complete
catch (e) {
if (q._tasks[q._ended + q._active - 1]) abort(q, e); // task errored synchronously
else if (!q._data) throw e; // await callback errored synchronously
}
}
}
function start(q) {
while (q._start = q._waiting && q._active < q._size) {
var i = q._ended + q._active,
t = q._tasks[i],
j = t.length - 1,
c = t[j];
t[j] = end(q, i);
--q._waiting, ++q._active;
t = c.apply(null, t);
if (!q._tasks[i]) continue; // task finished synchronously
q._tasks[i] = t || noabort;
}
}
function end(q, i) {
return function(e, r) {
if (!q._tasks[i]) return; // ignore multiple callbacks
--q._active, ++q._ended;
q._tasks[i] = null;
if (q._error != null) return; // ignore secondary errors
if (e != null) {
abort(q, e);
} else {
q._data[i] = r;
if (q._waiting) poke(q);
else maybeNotify(q);
}
};
}
function abort(q, e) {
var i = q._tasks.length, t;
q._error = e; // ignore active callbacks
q._data = undefined; // allow gc
q._waiting = NaN; // prevent starting
while (--i >= 0) {
if (t = q._tasks[i]) {
q._tasks[i] = null;
if (t.abort) {
try { t.abort(); }
catch (e) { /* ignore */ }
}
}
}
q._active = NaN; // allow notification
maybeNotify(q);
}
function maybeNotify(q) {
if (!q._active && q._call) {
var d = q._data;
q._data = undefined; // allow gc
q._call(q._error, d);
}
}
export default function queue(concurrency) {
if (concurrency == null) concurrency = Infinity;
else if (!((concurrency = +concurrency) >= 1)) throw new Error("invalid concurrency");
return new Queue(concurrency);
}
d3-queue-3.0.7/test/ 0000775 0000000 0000000 00000000000 13104374662 0014136 5 ustar 00root root 0000000 0000000 d3-queue-3.0.7/test/abortableTask.js 0000664 0000000 0000000 00000001146 13104374662 0017254 0 ustar 00root root 0000000 0000000 module.exports = function(delay) {
var active = 0,
counter = {scheduled: 0, aborted: 0};
function task(callback) {
var index = counter.scheduled++;
++active;
var timeout = setTimeout(function() {
timeout = null;
try {
callback(null, {active: active, index: index});
} finally {
--active;
}
}, delay);
return {
abort: function() {
if (timeout) {
timeout = clearTimeout(timeout);
++counter.aborted;
}
}
};
}
task.aborted = function() {
return counter.aborted;
};
return task;
};
d3-queue-3.0.7/test/asynchronousTask.js 0000664 0000000 0000000 00000000525 13104374662 0020054 0 ustar 00root root 0000000 0000000 module.exports = function(counter) {
var active = 0;
if (!counter) counter = {scheduled: 0};
return function(callback) {
var index = counter.scheduled++;
++active;
process.nextTick(function() {
try {
callback(null, {active: active, index: index});
} finally {
--active;
}
});
};
}
d3-queue-3.0.7/test/deferredSynchronousTask.js 0000664 0000000 0000000 00000001221 13104374662 0021346 0 ustar 00root root 0000000 0000000 module.exports = function(counter) {
var active = 0, deferrals = [];
if (!counter) counter = {scheduled: 0};
function task(callback) {
if (deferrals) return deferrals.push({callback: callback, index: counter.scheduled++});
try {
callback(null, {active: ++active, index: counter.scheduled++});
} finally {
--active;
}
}
task.finish = function() {
var deferrals_ = deferrals.slice();
deferrals = null;
deferrals_.forEach(function(deferral) {
try {
deferral.callback(null, {active: ++active, index: deferral.index});
} finally {
--active;
}
});
};
return task;
};
d3-queue-3.0.7/test/queue-test.js 0000664 0000000 0000000 00000040240 13104374662 0016575 0 ustar 00root root 0000000 0000000 var fs = require("fs"),
tape = require("tape"),
abortableTask = require("./abortableTask"),
asynchronousTask = require("./asynchronousTask"),
deferredSynchronousTask = require("./deferredSynchronousTask"),
synchronousTask = require("./synchronousTask"),
queue = require("../");
tape("example queue of fs.stat", function(test) {
queue.queue()
.defer(fs.stat, __dirname + "/../index.js")
.defer(fs.stat, __dirname + "/../README.md")
.defer(fs.stat, __dirname + "/../package.json")
.await(callback);
function callback(error, one, two, three) {
test.equal(error, null);
test.ok(one.size > 0);
test.ok(two.size > 0);
test.ok(three.size > 0);
test.end();
}
});
tape("if the concurrency is invalid, an Error is thrown", function(test) {
test.throws(function() { queue.queue(NaN); }, /Error/);
test.throws(function() { queue.queue(0); }, /Error/);
test.throws(function() { queue.queue(-1); }, /Error/);
test.end();
});
tape("queue.defer throws an error if passed a non-function", function(test) {
test.throws(function() { queue.queue().defer(42); }, /Error/);
test.end();
});
tape("queue.await throws an error if passed a non-function", function(test) {
var task = asynchronousTask();
test.throws(function() { queue.queue().defer(task).await(42); }, /Error/);
test.end();
});
tape("queue.awaitAll throws an error if passed a non-function", function(test) {
var task = asynchronousTask();
test.throws(function() { queue.queue().defer(task).awaitAll(42); }, /Error/);
test.end();
});
tape("in a queue of a single synchronous task that errors, the error is returned", function(test) {
queue.queue()
.defer(function(callback) { callback(-1); })
.await(callback);
function callback(error, result) {
test.equal(error, -1);
test.equal(result, undefined);
test.end();
}
});
tape("in a queue of a single asynchronous task that errors, the error is returned", function(test) {
queue.queue()
.defer(function(callback) { process.nextTick(function() { callback(-1); }); })
.await(callback);
function callback(error, result) {
test.equal(error, -1);
test.equal(result, undefined);
test.end();
}
});
tape("in a queue with multiple tasks that error, the first error is returned", function(test) {
queue.queue()
.defer(function(callback) { setTimeout(function() { callback(-2); }, 100); })
.defer(function(callback) { process.nextTick(function() { callback(-1); }); })
.defer(function(callback) { setTimeout(function() { callback(-3); }, 200); })
.await(callback);
function callback(error, one, two, three) {
test.equal(error, -1);
test.equal(one, undefined);
test.equal(two, undefined);
test.equal(three, undefined);
test.end();
}
});
tape("in a queue with multiple tasks where one errors, the first error is returned", function(test) {
queue.queue()
.defer(function(callback) { process.nextTick(function() { callback(-1); }); })
.defer(function(callback) { process.nextTick(function() { callback(null, 'ok'); }); })
.await(callback);
function callback(error, one, two) {
test.equal(error, -1);
test.equal(one, undefined);
test.equal(two, undefined);
test.end();
}
});
tape("in a queue with multiple synchronous tasks that error, the first error prevents the other tasks from running", function(test) {
queue.queue()
.defer(function(callback) { callback(-1); })
.defer(function(callback) { callback(-2); })
.defer(function(callback) { throw new Error; })
.await(callback);
function callback(error, one, two, three) {
test.equal(error, -1);
test.equal(one, undefined);
test.equal(two, undefined);
test.equal(three, undefined);
test.end();
}
});
tape("in a queue with a task that throws an error synchronously, the error is reported to the await callback", function(test) {
queue.queue()
.defer(function(callback) { throw new Error("foo"); })
.await(callback);
function callback(error) {
test.equal(error.message, "foo");
test.end();
}
});
tape("in a queue with a task that throws an error after calling back, the error is ignored", function(test) {
queue.queue()
.defer(function(callback) { setTimeout(function() { callback(null, 1); }, 100); })
.defer(function(callback) { callback(null, 2); process.nextTick(function() { callback(new Error("foo")); }); })
.await(callback);
function callback(error, one, two) {
test.equal(error, null);
test.equal(one, 1);
test.equal(two, 2);
test.end();
}
});
tape("in a queue with a task that doesn’t terminate and another that errors synchronously, the error is still reported", function(test) {
queue.queue()
.defer(function() { /* Run forever! */ })
.defer(function(callback) { callback(new Error("foo")); })
.await(callback);
function callback(error) {
test.equal(error.message, "foo");
test.end();
}
});
tape("a serial queue of asynchronous closures processes tasks serially", function(test) {
var tasks = [],
task = asynchronousTask(),
n = 10,
q = queue.queue(1);
while (--n >= 0) tasks.push(task);
tasks.forEach(function(t) { q.defer(t); });
q.awaitAll(callback);
function callback(error, results) {
test.equal(error, null);
test.deepEqual(results, [
{active: 1, index: 0},
{active: 1, index: 1},
{active: 1, index: 2},
{active: 1, index: 3},
{active: 1, index: 4},
{active: 1, index: 5},
{active: 1, index: 6},
{active: 1, index: 7},
{active: 1, index: 8},
{active: 1, index: 9}
]);
test.end();
}
});
tape("a fully-concurrent queue of ten asynchronous tasks executes all tasks concurrently", function(test) {
var t = asynchronousTask();
queue.queue()
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.awaitAll(callback);
function callback(error, results) {
test.equal(null, error);
test.deepEqual(results, [
{active: 10, index: 0},
{active: 9, index: 1},
{active: 8, index: 2},
{active: 7, index: 3},
{active: 6, index: 4},
{active: 5, index: 5},
{active: 4, index: 6},
{active: 3, index: 7},
{active: 2, index: 8},
{active: 1, index: 9}
]);
test.end();
}
});
tape("a partly-concurrent queue of ten asynchronous tasks executes at most three tasks concurrently", function(test) {
var t = asynchronousTask();
queue.queue(3)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.awaitAll(callback);
function callback(error, results) {
test.equal(null, error);
test.deepEqual(results, [
{active: 3, index: 0},
{active: 3, index: 1},
{active: 3, index: 2},
{active: 3, index: 3},
{active: 3, index: 4},
{active: 3, index: 5},
{active: 3, index: 6},
{active: 3, index: 7},
{active: 2, index: 8},
{active: 1, index: 9}
]);
test.end();
}
});
tape("a serialized queue of ten asynchronous tasks executes all tasks in series", function(test) {
var t = asynchronousTask();
queue.queue(1)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.awaitAll(callback);
function callback(error, results) {
test.equal(null, error);
test.deepEqual(results, [
{active: 1, index: 0},
{active: 1, index: 1},
{active: 1, index: 2},
{active: 1, index: 3},
{active: 1, index: 4},
{active: 1, index: 5},
{active: 1, index: 6},
{active: 1, index: 7},
{active: 1, index: 8},
{active: 1, index: 9}
]);
test.end();
}
});
tape("a serialized queue of ten deferred synchronous tasks executes all tasks in series, within the callback of the first task", function(test) {
var t = deferredSynchronousTask();
queue.queue(1)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.awaitAll(callback);
t.finish();
function callback(error, results) {
test.equal(null, error);
test.deepEqual(results, [
{active: 1, index: 0},
{active: 2, index: 1},
{active: 2, index: 2},
{active: 2, index: 3},
{active: 2, index: 4},
{active: 2, index: 5},
{active: 2, index: 6},
{active: 2, index: 7},
{active: 2, index: 8},
{active: 2, index: 9}
]);
test.end();
}
});
tape("a partly-concurrent queue of ten synchronous tasks executes all tasks in series", function(test) {
var t = synchronousTask();
queue.queue(3)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.awaitAll(callback);
function callback(error, results) {
test.equal(null, error);
test.deepEqual(results, [
{active: 1, index: 0},
{active: 1, index: 1},
{active: 1, index: 2},
{active: 1, index: 3},
{active: 1, index: 4},
{active: 1, index: 5},
{active: 1, index: 6},
{active: 1, index: 7},
{active: 1, index: 8},
{active: 1, index: 9}
]);
test.end();
}
});
tape("a serialized queue of ten synchronous tasks executes all tasks in series", function(test) {
var t = synchronousTask();
queue.queue(1)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.defer(t)
.awaitAll(callback);
function callback(error, results) {
test.equal(null, error);
test.deepEqual(results, [
{active: 1, index: 0},
{active: 1, index: 1},
{active: 1, index: 2},
{active: 1, index: 3},
{active: 1, index: 4},
{active: 1, index: 5},
{active: 1, index: 6},
{active: 1, index: 7},
{active: 1, index: 8},
{active: 1, index: 9}
]);
test.end();
}
});
tape("a huge queue of deferred synchronous tasks does not throw a RangeError", function(test) {
var t = deferredSynchronousTask(),
q = queue.queue(),
n = 200000;
for (var i = 0; i < n; ++i) q.defer(t);
t.finish();
q.awaitAll(callback);
function callback(error, results) {
test.equal(null, error);
test.equal(results.length, n);
test.end();
}
});
tape("if a task calls back successfully more than once, subsequent calls are ignored", function(test) {
queue.queue()
.defer(function(callback) { setTimeout(function() { callback(null, 1); }, 100); })
.defer(function(callback) { callback(null, 2); process.nextTick(function() { callback(null, -1); }); })
.defer(function(callback) { callback(null, 3); process.nextTick(function() { callback(new Error("foo")); }); })
.defer(function(callback) { process.nextTick(function() { callback(null, 4); }); setTimeout(function() { callback(new Error("bar")); }, 100); })
.await(callback);
function callback(error, one, two, three, four) {
test.equal(error, null);
test.equal(one, 1);
test.equal(two, 2);
test.equal(three, 3);
test.equal(four, 4);
test.end();
}
});
tape("if a task calls back with an error more than once, subsequent calls are ignored", function(test) {
queue.queue()
.defer(function(callback) { setTimeout(function() { callback(null, 1); }, 100); })
.defer(function(callback) { callback(new Error("foo")); process.nextTick(function() { callback(new Error("bar")); }); })
.defer(function(callback) { process.nextTick(function() { callback(new Error("bar")); }); setTimeout(function() { callback(new Error("baz")); }, 100); })
.await(callback);
function callback(error) {
test.equal(error.message, "foo");
test.end();
}
});
tape("if a task throws an error aftering calling back synchronously, the error is ignored", function(test) {
queue.queue()
.defer(function(callback) { callback(null, 1); throw new Error; })
.await(callback);
function callback(error, one) {
test.equal(error, null);
test.equal(one, 1);
test.end();
}
});
tape("if the await callback throws an error aftering calling back synchronously, the error is thrown", function(test) {
queue.queue(1)
.defer(function(callback) { process.nextTick(callback); })
.defer(function(callback) { callback(null, 1); })
.await(function() { throw new Error("foo"); });
process.once("uncaughtException", function(error) {
test.equal(error.message, "foo");
test.end();
});
});
tape("if a task errors, another task can still complete successfully, and is ignored", function(test) {
queue.queue()
.defer(function(callback) { setTimeout(function() { callback(null, 1); }, 10); })
.defer(function(callback) { callback(new Error("foo")); })
.await(callback);
function callback(error) {
test.equal(error.message, "foo");
test.end();
}
});
tape("if a task errors, it is not subsequently aborted", function(test) {
var aborted = false;
var q = queue.queue()
.defer(function(callback) { process.nextTick(function() { callback(new Error("foo")); }); return {abort: function() { aborted = true; }}; })
.await(callback);
function callback(error) {
test.equal(error.message, "foo");
test.equal(aborted, false);
test.end();
}
});
tape("a task that defers another task is allowed", function(test) {
var q = queue.queue();
q.defer(function(callback) {
callback(null);
q.defer(function(callback) {
test.end();
});
});
});
tape("a falsey error is still considered an error", function(test) {
queue.queue()
.defer(function(callback) { callback(0); })
.defer(function() { throw new Error; })
.await(function(error) { test.equal(error, 0); test.end(); });
});
tape("if the await callback is set during abort, it only gets called once", function(test) {
var q = queue.queue();
q.defer(function() { return {abort: function() { q.await(callback); }}; });
q.defer(function() { throw new Error("foo"); });
function callback(error) {
test.equal(error.message, "foo");
test.end();
}
});
tape("aborts a queue of partially-completed asynchronous tasks", function(test) {
var shortTask = abortableTask(50),
longTask = abortableTask(5000);
var q = queue.queue()
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.awaitAll(callback);
setTimeout(function() {
q.abort();
}, 250);
function callback(error, results) {
test.equal(error.message, "abort");
test.equal(results, undefined);
test.equal(shortTask.aborted(), 0);
test.equal(longTask.aborted(), 5);
test.end();
}
});
tape("aborts an entire queue of asynchronous tasks", function(test) {
var longTask = abortableTask(5000);
var q = queue.queue()
.defer(longTask)
.defer(longTask)
.defer(longTask)
.defer(longTask)
.defer(longTask)
.awaitAll(callback);
setTimeout(function() {
q.abort();
}, 250);
function callback(error, results) {
test.equal(error.message, "abort");
test.equal(results, undefined);
test.equal(longTask.aborted(), 5);
test.end();
}
});
tape("does not abort tasks that have not yet started", function(test) {
var shortTask = abortableTask(50),
longTask = abortableTask(5000);
var q = queue.queue(2) // enough for two short tasks to run
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.defer(shortTask)
.defer(longTask)
.awaitAll(callback);
setTimeout(function() {
q.abort();
}, 250);
function callback(error, results) {
test.equal(error.message, "abort");
test.equal(results, undefined);
test.equal(shortTask.aborted(), 0);
test.equal(longTask.aborted(), 2);
test.end();
}
});
d3-queue-3.0.7/test/synchronousTask.js 0000664 0000000 0000000 00000000377 13104374662 0017720 0 ustar 00root root 0000000 0000000 module.exports = function(counter) {
var active = 0;
if (!counter) counter = {scheduled: 0};
return function(callback) {
try {
callback(null, {active: ++active, index: counter.scheduled++});
} finally {
--active;
}
};
};