mirror of
https://github.com/kiwix/kiwix-js-pwa.git
synced 2025-09-08 03:37:12 -04:00
Update Q to v1.5.1
Former-commit-id: 6e74d289f07ac8153af5313e68ebd5011cc1fd4b [formerly 4cab4adf1ddc2e96da8ea6f412d79239dda76728] Former-commit-id: a19454dcd69799a56546d99ce19f5526f7fc7ab4
This commit is contained in:
parent
3534459e9c
commit
a8c97c86a7
143
www/js/lib/q.js
143
www/js/lib/q.js
@ -1,8 +1,8 @@
|
||||
// vim:ts=4:sts=4:sw=4:
|
||||
/*!
|
||||
*
|
||||
* Copyright 2009-2012 Kris Kowal under the terms of the MIT
|
||||
* license found at http://github.com/kriskowal/q/raw/master/LICENSE
|
||||
* Copyright 2009-2017 Kris Kowal under the terms of the MIT
|
||||
* license found at https://github.com/kriskowal/q/blob/v1/LICENSE
|
||||
*
|
||||
* With parts by Tyler Close
|
||||
* Copyright 2007-2009 Tyler Close under the terms of the MIT X license found
|
||||
@ -55,8 +55,22 @@
|
||||
}
|
||||
|
||||
// <script>
|
||||
} else if (typeof self !== "undefined") {
|
||||
self.Q = definition();
|
||||
} else if (typeof window !== "undefined" || typeof self !== "undefined") {
|
||||
// Prefer window over self for add-on scripts. Use self for
|
||||
// non-windowed contexts.
|
||||
var global = typeof window !== "undefined" ? window : self;
|
||||
|
||||
// Get the `window` object, save the previous Q global
|
||||
// and initialize Q as a global.
|
||||
var previousQ = global.Q;
|
||||
global.Q = definition();
|
||||
|
||||
// Add a noConflict function so Q can be removed from the
|
||||
// global namespace.
|
||||
global.Q.noConflict = function () {
|
||||
global.Q = previousQ;
|
||||
return this;
|
||||
};
|
||||
|
||||
} else {
|
||||
throw new Error("This environment was not anticipated by Q. Please file a bug.");
|
||||
@ -91,21 +105,34 @@ var nextTick =(function () {
|
||||
var flushing = false;
|
||||
var requestTick = void 0;
|
||||
var isNodeJS = false;
|
||||
// queue for late tasks, used by unhandled rejection tracking
|
||||
var laterQueue = [];
|
||||
|
||||
function flush() {
|
||||
/* jshint loopfunc: true */
|
||||
var task, domain;
|
||||
|
||||
while (head.next) {
|
||||
head = head.next;
|
||||
var task = head.task;
|
||||
task = head.task;
|
||||
head.task = void 0;
|
||||
var domain = head.domain;
|
||||
domain = head.domain;
|
||||
|
||||
if (domain) {
|
||||
head.domain = void 0;
|
||||
domain.enter();
|
||||
}
|
||||
runSingle(task, domain);
|
||||
|
||||
}
|
||||
while (laterQueue.length) {
|
||||
task = laterQueue.pop();
|
||||
runSingle(task);
|
||||
}
|
||||
flushing = false;
|
||||
}
|
||||
// runs a single function in the async queue
|
||||
function runSingle(task, domain) {
|
||||
try {
|
||||
task();
|
||||
|
||||
@ -141,8 +168,6 @@ var nextTick =(function () {
|
||||
}
|
||||
}
|
||||
|
||||
flushing = false;
|
||||
}
|
||||
|
||||
nextTick = function (task) {
|
||||
tail = tail.next = {
|
||||
@ -157,9 +182,16 @@ var nextTick =(function () {
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof process !== "undefined" && process.nextTick) {
|
||||
// Node.js before 0.9. Note that some fake-Node environments, like the
|
||||
// Mocha test runner, introduce a `process` global without a `nextTick`.
|
||||
if (typeof process === "object" &&
|
||||
process.toString() === "[object process]" && process.nextTick) {
|
||||
// Ensure Q is in a real Node environment, with a `process.nextTick`.
|
||||
// To see through fake Node environments:
|
||||
// * Mocha test runner - exposes a `process` global without a `nextTick`
|
||||
// * Browserify - exposes a `process.nexTick` function that uses
|
||||
// `setTimeout`. In this case `setImmediate` is preferred because
|
||||
// it is faster. Browserify's `process.toString()` yields
|
||||
// "[object Object]", while in a real Node environment
|
||||
// `process.toString()` yields "[object process]".
|
||||
isNodeJS = true;
|
||||
|
||||
requestTick = function () {
|
||||
@ -203,7 +235,16 @@ var nextTick =(function () {
|
||||
setTimeout(flush, 0);
|
||||
};
|
||||
}
|
||||
|
||||
// runs a task after all other tasks have been run
|
||||
// this is useful for unhandled rejection tracking that needs to happen
|
||||
// after all `then`d tasks have been run.
|
||||
nextTick.runAfter = function (task) {
|
||||
laterQueue.push(task);
|
||||
if (!flushing) {
|
||||
flushing = true;
|
||||
requestTick();
|
||||
}
|
||||
};
|
||||
return nextTick;
|
||||
})();
|
||||
|
||||
@ -211,11 +252,11 @@ var nextTick =(function () {
|
||||
// modifications.
|
||||
// There is no situation where this is necessary.
|
||||
// If you need a security guarantee, these primordials need to be
|
||||
// deeply frozen anyway, and if you don’t need a security guarantee,
|
||||
// deeply frozen anyway, and if you don’t need a security guarantee,
|
||||
// this is just plain paranoid.
|
||||
// However, this **might** have the nice side-effect of reducing the size of
|
||||
// the minified code by reducing x.call() to merely x()
|
||||
// See Mark Miller’s explanation of what this does.
|
||||
// See Mark Miller’s explanation of what this does.
|
||||
// http://wiki.ecmascript.org/doku.php?id=conventions:safe_meta_programming
|
||||
var call = Function.call;
|
||||
function uncurryThis(f) {
|
||||
@ -287,6 +328,11 @@ var object_create = Object.create || function (prototype) {
|
||||
return new Type();
|
||||
};
|
||||
|
||||
var object_defineProperty = Object.defineProperty || function (obj, prop, descriptor) {
|
||||
obj[prop] = descriptor.value;
|
||||
return obj;
|
||||
};
|
||||
|
||||
var object_hasOwnProperty = uncurryThis(Object.prototype.hasOwnProperty);
|
||||
|
||||
var object_keys = Object.keys || function (object) {
|
||||
@ -337,19 +383,20 @@ function makeStackTraceLong(error, promise) {
|
||||
promise.stack &&
|
||||
typeof error === "object" &&
|
||||
error !== null &&
|
||||
error.stack &&
|
||||
error.stack.indexOf(STACK_JUMP_SEPARATOR) === -1
|
||||
error.stack
|
||||
) {
|
||||
var stacks = [];
|
||||
for (var p = promise; !!p; p = p.source) {
|
||||
if (p.stack) {
|
||||
if (p.stack && (!error.__minimumStackCounter__ || error.__minimumStackCounter__ > p.stackCounter)) {
|
||||
object_defineProperty(error, "__minimumStackCounter__", {value: p.stackCounter, configurable: true});
|
||||
stacks.unshift(p.stack);
|
||||
}
|
||||
}
|
||||
stacks.unshift(error.stack);
|
||||
|
||||
var concatedStacks = stacks.join("\n" + STACK_JUMP_SEPARATOR + "\n");
|
||||
error.stack = filterStackString(concatedStacks);
|
||||
var stack = filterStackString(concatedStacks);
|
||||
object_defineProperty(error, "stack", {value: stack, configurable: true});
|
||||
}
|
||||
}
|
||||
|
||||
@ -476,6 +523,14 @@ Q.nextTick = nextTick;
|
||||
*/
|
||||
Q.longStackSupport = false;
|
||||
|
||||
/**
|
||||
* The counter is used to determine the stopping point for building
|
||||
* long stack traces. In makeStackTraceLong we walk backwards through
|
||||
* the linked list of promises, only stacks which were created before
|
||||
* the rejection are concatenated.
|
||||
*/
|
||||
var longStackCounter = 1;
|
||||
|
||||
// enable long stacks if Q_DEBUG is set
|
||||
if (typeof process === "object" && process && process.env && process.env.Q_DEBUG) {
|
||||
Q.longStackSupport = true;
|
||||
@ -548,6 +603,7 @@ function defer() {
|
||||
// At the same time, cut off the first line; it's always just
|
||||
// "[object Promise]\n", as per the `toString`.
|
||||
promise.stack = e.stack.substring(e.stack.indexOf("\n") + 1);
|
||||
promise.stackCounter = longStackCounter++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -557,7 +613,12 @@ function defer() {
|
||||
|
||||
function become(newPromise) {
|
||||
resolvedPromise = newPromise;
|
||||
|
||||
if (Q.longStackSupport && hasStacks) {
|
||||
// Only hold a reference to the new promise if long stacks
|
||||
// are enabled to reduce memory usage
|
||||
promise.source = newPromise;
|
||||
}
|
||||
|
||||
array_reduce(messages, function (undefined, message) {
|
||||
Q.nextTick(function () {
|
||||
@ -685,7 +746,7 @@ Promise.prototype.join = function (that) {
|
||||
// TODO: "===" should be Object.is or equiv
|
||||
return x;
|
||||
} else {
|
||||
throw new Error("Can't join: not the same: " + x + " " + y);
|
||||
throw new Error("Q can't join: not the same: " + x + " " + y);
|
||||
}
|
||||
});
|
||||
};
|
||||
@ -918,8 +979,8 @@ Q.thenReject = function (promise, reason) {
|
||||
/**
|
||||
* If an object is not a promise, it is as "near" as possible.
|
||||
* If a promise is rejected, it is as "near" as possible too.
|
||||
* If it’s a fulfilled promise, the fulfillment value is nearer.
|
||||
* If it’s a deferred promise and the deferred has been resolved, the
|
||||
* If it’s a fulfilled promise, the fulfillment value is nearer.
|
||||
* If it’s a deferred promise and the deferred has been resolved, the
|
||||
* resolution is "nearer".
|
||||
* @param object
|
||||
* @returns most resolved (nearest) form of the object
|
||||
@ -997,6 +1058,7 @@ Promise.prototype.isRejected = function () {
|
||||
// shimmed environments, this would naturally be a `Set`.
|
||||
var unhandledReasons = [];
|
||||
var unhandledRejections = [];
|
||||
var reportedUnhandledRejections = [];
|
||||
var trackUnhandledRejections = true;
|
||||
|
||||
function resetUnhandledRejections() {
|
||||
@ -1012,6 +1074,14 @@ function trackRejection(promise, reason) {
|
||||
if (!trackUnhandledRejections) {
|
||||
return;
|
||||
}
|
||||
if (typeof process === "object" && typeof process.emit === "function") {
|
||||
Q.nextTick.runAfter(function () {
|
||||
if (array_indexOf(unhandledRejections, promise) !== -1) {
|
||||
process.emit("unhandledRejection", reason, promise);
|
||||
reportedUnhandledRejections.push(promise);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
unhandledRejections.push(promise);
|
||||
if (reason && typeof reason.stack !== "undefined") {
|
||||
@ -1028,6 +1098,15 @@ function untrackRejection(promise) {
|
||||
|
||||
var at = array_indexOf(unhandledRejections, promise);
|
||||
if (at !== -1) {
|
||||
if (typeof process === "object" && typeof process.emit === "function") {
|
||||
Q.nextTick.runAfter(function () {
|
||||
var atReport = array_indexOf(reportedUnhandledRejections, promise);
|
||||
if (atReport !== -1) {
|
||||
process.emit("rejectionHandled", unhandledReasons[at], promise);
|
||||
reportedUnhandledRejections.splice(atReport, 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
unhandledRejections.splice(at, 1);
|
||||
unhandledReasons.splice(at, 1);
|
||||
}
|
||||
@ -1564,13 +1643,15 @@ function any(promises) {
|
||||
function onFulfilled(result) {
|
||||
deferred.resolve(result);
|
||||
}
|
||||
function onRejected() {
|
||||
function onRejected(err) {
|
||||
pendingCount--;
|
||||
if (pendingCount === 0) {
|
||||
deferred.reject(new Error(
|
||||
"Can't get fulfillment value from any promise, all " +
|
||||
"promises were rejected."
|
||||
));
|
||||
var rejection = err || new Error("" + err);
|
||||
|
||||
rejection.message = ("Q can't get fulfillment value from any promise, all " +
|
||||
"promises were rejected. Last error message: " + rejection.message);
|
||||
|
||||
deferred.reject(rejection);
|
||||
}
|
||||
}
|
||||
function onProgress(progress) {
|
||||
@ -1694,6 +1775,9 @@ Q["finally"] = function (object, callback) {
|
||||
|
||||
Promise.prototype.fin = // XXX legacy
|
||||
Promise.prototype["finally"] = function (callback) {
|
||||
if (!callback || typeof callback.apply !== "function") {
|
||||
throw new Error("Q can't apply finally callback");
|
||||
}
|
||||
callback = Q(callback);
|
||||
return this.then(function (value) {
|
||||
return callback.fcall().then(function () {
|
||||
@ -1857,6 +1941,9 @@ Promise.prototype.nfcall = function (/*...args*/) {
|
||||
*/
|
||||
Q.nfbind =
|
||||
Q.denodeify = function (callback /*...args*/) {
|
||||
if (callback === undefined) {
|
||||
throw new Error("Q can't wrap an undefined function");
|
||||
}
|
||||
var baseArgs = array_slice(arguments, 1);
|
||||
return function () {
|
||||
var nodeArgs = baseArgs.concat(array_slice(arguments));
|
||||
@ -1978,6 +2065,10 @@ Promise.prototype.nodeify = function (nodeback) {
|
||||
}
|
||||
};
|
||||
|
||||
Q.noConflict = function() {
|
||||
throw new Error("Q.noConflict only works when Q is used as a global");
|
||||
};
|
||||
|
||||
// All code before this point will be filtered from stack traces.
|
||||
var qEndingLine = captureLine();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user