| 1 |
// https://d3js.org/d3-queue/ Version 3.0.7. Copyright 2017 Mike Bostock. |
| 2 |
(function (global, factory) { |
| 3 |
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : |
| 4 |
typeof define === 'function' && define.amd ? define(['exports'], factory) : |
| 5 |
(factory((global.d3 = global.d3 || {}))); |
| 6 |
}(this, (function (exports) { 'use strict'; |
| 7 |
|
| 8 |
var slice = [].slice; |
| 9 |
|
| 10 |
var noabort = {}; |
| 11 |
|
| 12 |
function Queue(size) { |
| 13 |
this._size = size; |
| 14 |
this._call = |
| 15 |
this._error = null; |
| 16 |
this._tasks = []; |
| 17 |
this._data = []; |
| 18 |
this._waiting = |
| 19 |
this._active = |
| 20 |
this._ended = |
| 21 |
this._start = 0; // inside a synchronous task callback? |
| 22 |
} |
| 23 |
|
| 24 |
Queue.prototype = queue.prototype = { |
| 25 |
constructor: Queue, |
| 26 |
defer: function(callback) { |
| 27 |
if (typeof callback !== "function") throw new Error("invalid callback"); |
| 28 |
if (this._call) throw new Error("defer after await"); |
| 29 |
if (this._error != null) return this; |
| 30 |
var t = slice.call(arguments, 1); |
| 31 |
t.push(callback); |
| 32 |
++this._waiting, this._tasks.push(t); |
| 33 |
poke(this); |
| 34 |
return this; |
| 35 |
}, |
| 36 |
abort: function() { |
| 37 |
if (this._error == null) abort(this, new Error("abort")); |
| 38 |
return this; |
| 39 |
}, |
| 40 |
await: function(callback) { |
| 41 |
if (typeof callback !== "function") throw new Error("invalid callback"); |
| 42 |
if (this._call) throw new Error("multiple await"); |
| 43 |
this._call = function(error, results) { callback.apply(null, [error].concat(results)); }; |
| 44 |
maybeNotify(this); |
| 45 |
return this; |
| 46 |
}, |
| 47 |
awaitAll: function(callback) { |
| 48 |
if (typeof callback !== "function") throw new Error("invalid callback"); |
| 49 |
if (this._call) throw new Error("multiple await"); |
| 50 |
this._call = callback; |
| 51 |
maybeNotify(this); |
| 52 |
return this; |
| 53 |
} |
| 54 |
}; |
| 55 |
|
| 56 |
function poke(q) { |
| 57 |
if (!q._start) { |
| 58 |
try { start(q); } // let the current task complete |
| 59 |
catch (e) { |
| 60 |
if (q._tasks[q._ended + q._active - 1]) abort(q, e); // task errored synchronously |
| 61 |
else if (!q._data) throw e; // await callback errored synchronously |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
function start(q) { |
| 67 |
while (q._start = q._waiting && q._active < q._size) { |
| 68 |
var i = q._ended + q._active, |
| 69 |
t = q._tasks[i], |
| 70 |
j = t.length - 1, |
| 71 |
c = t[j]; |
| 72 |
t[j] = end(q, i); |
| 73 |
--q._waiting, ++q._active; |
| 74 |
t = c.apply(null, t); |
| 75 |
if (!q._tasks[i]) continue; // task finished synchronously |
| 76 |
q._tasks[i] = t || noabort; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
function end(q, i) { |
| 81 |
return function(e, r) { |
| 82 |
if (!q._tasks[i]) return; // ignore multiple callbacks |
| 83 |
--q._active, ++q._ended; |
| 84 |
q._tasks[i] = null; |
| 85 |
if (q._error != null) return; // ignore secondary errors |
| 86 |
if (e != null) { |
| 87 |
abort(q, e); |
| 88 |
} else { |
| 89 |
q._data[i] = r; |
| 90 |
if (q._waiting) poke(q); |
| 91 |
else maybeNotify(q); |
| 92 |
} |
| 93 |
}; |
| 94 |
} |
| 95 |
|
| 96 |
function abort(q, e) { |
| 97 |
var i = q._tasks.length, t; |
| 98 |
q._error = e; // ignore active callbacks |
| 99 |
q._data = undefined; // allow gc |
| 100 |
q._waiting = NaN; // prevent starting |
| 101 |
|
| 102 |
while (--i >= 0) { |
| 103 |
if (t = q._tasks[i]) { |
| 104 |
q._tasks[i] = null; |
| 105 |
if (t.abort) { |
| 106 |
try { t.abort(); } |
| 107 |
catch (e) { /* ignore */ } |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
q._active = NaN; // allow notification |
| 113 |
maybeNotify(q); |
| 114 |
} |
| 115 |
|
| 116 |
function maybeNotify(q) { |
| 117 |
if (!q._active && q._call) { |
| 118 |
var d = q._data; |
| 119 |
q._data = undefined; // allow gc |
| 120 |
q._call(q._error, d); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
function queue(concurrency) { |
| 125 |
if (concurrency == null) concurrency = Infinity; |
| 126 |
else if (!((concurrency = +concurrency) >= 1)) throw new Error("invalid concurrency"); |
| 127 |
return new Queue(concurrency); |
| 128 |
} |
| 129 |
|
| 130 |
exports.queue = queue; |
| 131 |
|
| 132 |
Object.defineProperty(exports, '__esModule', { value: true }); |
| 133 |
|
| 134 |
}))); |
| 135 |
|