-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ece94a5
commit 4a1efaa
Showing
12 changed files
with
245 additions
and
113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : | ||
typeof define === 'function' && define.amd ? define('queue', factory) : | ||
(global.queue = factory()); | ||
}(this, function () { 'use strict'; | ||
|
||
var slice = [].slice; | ||
|
||
function noop() {} | ||
|
||
var noabort = {}; | ||
var success = [null]; | ||
function newQueue(concurrency) { | ||
if (!(concurrency >= 1)) throw new Error; | ||
|
||
var q, | ||
tasks = [], | ||
results = [], | ||
waiting = 0, | ||
active = 0, | ||
ended = 0, | ||
starting, // inside a synchronous task callback? | ||
error, | ||
callback = noop, | ||
callbackAll = true; | ||
|
||
function start() { | ||
if (starting) return; // let the current task complete | ||
while (starting = waiting && active < concurrency) { | ||
var i = ended + active, | ||
t = tasks[i], | ||
j = t.length - 1, | ||
c = t[j]; | ||
t[j] = end(i); | ||
--waiting, ++active, tasks[i] = c.apply(null, t) || noabort; | ||
} | ||
} | ||
|
||
function end(i) { | ||
return function(e, r) { | ||
if (!tasks[i]) throw new Error; // detect multiple callbacks | ||
--active, ++ended, tasks[i] = null; | ||
if (error != null) return; // only report the first error | ||
if (e != null) { | ||
abort(e); | ||
} else { | ||
results[i] = r; | ||
if (waiting) start(); | ||
else if (!active) notify(); | ||
} | ||
}; | ||
} | ||
|
||
function abort(e) { | ||
error = e; // ignore new tasks and squelch active callbacks | ||
waiting = NaN; // stop queued tasks from starting | ||
notify(); | ||
} | ||
|
||
function notify() { | ||
if (error != null) callback(error); | ||
else if (callbackAll) callback(null, results); | ||
else callback.apply(null, success.concat(results)); | ||
} | ||
|
||
return q = { | ||
defer: function(f) { | ||
if (callback !== noop) throw new Error; | ||
var t = slice.call(arguments, 1); | ||
t.push(f); | ||
++waiting, tasks.push(t); | ||
start(); | ||
return q; | ||
}, | ||
abort: function() { | ||
if (error == null) { | ||
var i = ended + active, t; | ||
while (--i >= 0) (t = tasks[i]) && t.abort && t.abort(); | ||
abort(new Error("abort")); | ||
} | ||
return q; | ||
}, | ||
await: function(f) { | ||
if (callback !== noop) throw new Error; | ||
callback = f, callbackAll = false; | ||
if (!waiting && !active) notify(); | ||
return q; | ||
}, | ||
awaitAll: function(f) { | ||
if (callback !== noop) throw new Error; | ||
callback = f, callbackAll = true; | ||
if (!waiting && !active) notify(); | ||
return q; | ||
} | ||
}; | ||
} | ||
|
||
function queue(concurrency) { | ||
return newQueue(arguments.length ? +concurrency : Infinity); | ||
} | ||
|
||
queue.version = "1.2.1"; | ||
|
||
return queue; | ||
|
||
})); |
Oops, something went wrong.