@qooxdoo/framework
Version:
The JS Framework for Coders
1,360 lines (1,240 loc) • 251 kB
JavaScript
/**
* Bluebird implementation for the qx.Promise API
*
*
*@ignore(global.*)
*@ignore(Symbol.*)
*@ignore(chrome.*)
*
*/
/* global global, setImmediate, chrome, _dereq_ */
/* eslint-disable no-global-assign */
qx.Class.define("qx.promise.BluebirdImpl", {
type: "static",
statics: {
/**
* The Bluebird Promise class
* Always available
*/
Bluebird: null,
/**
* Called when the Bluebird Promise class is loaded
* @param Promise {Class} the Promise class
*/
__attachBluebird(Promise) {
qx.promise.BluebirdImpl.Bluebird = Promise;
Promise.config({
warnings: qx.core.Environment.get("qx.promise.warnings"),
longStackTraces: qx.core.Environment.get("qx.promise.longStackTraces"),
cancellation: true
});
}
}
});
/*eslint curly: ["off"]*/
/**
* @lint ignoreUnused(exports)
* @lint ignoreUnused(module)
* @lint ignoreUnused(define)
* @lint ignoreUnused(CapturedTrace)
* @lint ignoreUnused(bitField)
* @lint ignoreUnused(isArray)
* @lint ignoreDeprecated(eval)
* @lint ignoreNoLoopBlock()
* @ignore(Map)
* @ignore(MutationObserver)
* @ignore(Symbol)
* @ignore(Symbol.iterator)
* @ignore(_dereq_)
* @ignore(chrome)
* @ignore(chrome.loadTimes)
* @ignore(CustomEvent)
* @ignore(define)
* @ignore(define.amd)
* @ignore(enumeration)
* @ignore(Event)
* @ignore(exports)
* @ignore(global)
* @ignore(global.setImmediate)
* @ignore(module)
* @ignore(module.exports)
* @ignore(MutationObserver)
* @ignore(obj)
* @ignore(obj.toPromise)
* @ignore(predicateLoop)
* @ignore(process)
* @ignore(process.domain)
* @ignore(process.emit.apply)
* @ignore(process.env)
* @ignore(process.exit)
* @ignore(process.nextTick)
* @ignore(process.stderr.isTTY)
* @ignore(process.stderr.write)
* @ignore(process.versions.node.split)
* @ignore(promise)
* @ignore(Promise)
* @ignore(self)
* @ignore(setImmediate)
*/
(function () {
/* @preserve
* The MIT License (MIT)
*
* Copyright (c) 2013-2015 Petka Antonov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
/**
* bluebird build version 3.4.5
* Features enabled: core, race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, using, timers, filter, any, each
*/
!(function (e) {
qx.promise.BluebirdImpl.__attachBluebird(e());
})(function () {
var define, module, exports;
return (function e(t, n, r) {
function s(o, u) {
if (!n[o]) {
if (!t[o]) {
var a = typeof _dereq_ == "function" && _dereq_;
if (!u && a) return a(o, !0);
if (i) return i(o, !0);
var f = new Error("Cannot find module '" + o + "'");
/* eslint-disable-next-line no-sequences */
throw ((f.code = "MODULE_NOT_FOUND"), f);
}
var l = (n[o] = { exports: {} });
t[o][0].call(
l.exports,
function (e) {
var n = t[o][1][e];
return s(n ? n : e);
},
l,
l.exports,
e,
t,
n,
r
);
}
return n[o].exports;
}
var i = typeof _dereq_ == "function" && _dereq_;
for (var o = 0; o < r.length; o++) s(r[o]);
return s;
})(
{
1: [
function (_dereq_, module, exports) {
"use strict";
module.exports = function (Promise) {
var SomePromiseArray = Promise._SomePromiseArray;
function any(promises) {
var ret = new SomePromiseArray(promises);
var promise = ret.promise();
ret.setHowMany(1);
ret.setUnwrap();
ret.init();
return promise;
}
Promise.any = function (promises) {
return any(promises);
};
Promise.prototype.any = function () {
return any(this);
};
};
},
{}
],
2: [
function (_dereq_, module, exports) {
"use strict";
var firstLineError;
try {
throw new Error();
} catch (e) {
firstLineError = e;
}
var schedule = _dereq_("./schedule");
var Queue = _dereq_("./queue");
var util = _dereq_("./util");
function Async() {
this._customScheduler = false;
this._isTickUsed = false;
this._lateQueue = new Queue(16);
this._normalQueue = new Queue(16);
this._haveDrainedQueues = false;
this._trampolineEnabled = true;
var self = this;
this.drainQueues = function () {
self._drainQueues();
};
this._schedule = schedule;
}
Async.prototype.setScheduler = function (fn) {
var prev = this._schedule;
this._schedule = fn;
this._customScheduler = true;
return prev;
};
Async.prototype.hasCustomScheduler = function () {
return this._customScheduler;
};
Async.prototype.enableTrampoline = function () {
this._trampolineEnabled = true;
};
Async.prototype.disableTrampolineIfNecessary = function () {
if (util.hasDevTools) {
this._trampolineEnabled = false;
}
};
Async.prototype.haveItemsQueued = function () {
return this._isTickUsed || this._haveDrainedQueues;
};
Async.prototype.fatalError = function (e, isNode) {
if (isNode) {
process.stderr.write(
"Fatal " + (e instanceof Error ? e.stack : e) + "\n"
);
process.exit(2);
} else {
this.throwLater(e);
}
};
Async.prototype.throwLater = function (fn, arg) {
if (arguments.length === 1) {
arg = fn;
fn = function () {
throw arg;
};
}
if (typeof setTimeout !== "undefined") {
setTimeout(function () {
fn(arg);
}, 0);
} else
try {
this._schedule(function () {
fn(arg);
});
} catch (e) {
throw new Error(
"No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a"
);
}
};
function AsyncInvokeLater(fn, receiver, arg) {
this._lateQueue.push(fn, receiver, arg);
this._queueTick();
}
function AsyncInvoke(fn, receiver, arg) {
this._normalQueue.push(fn, receiver, arg);
this._queueTick();
}
function AsyncSettlePromises(promise) {
this._normalQueue._pushOne(promise);
this._queueTick();
}
if (!util.hasDevTools) {
Async.prototype.invokeLater = AsyncInvokeLater;
Async.prototype.invoke = AsyncInvoke;
Async.prototype.settlePromises = AsyncSettlePromises;
} else {
Async.prototype.invokeLater = function (fn, receiver, arg) {
if (this._trampolineEnabled) {
AsyncInvokeLater.call(this, fn, receiver, arg);
} else {
this._schedule(function () {
setTimeout(function () {
fn.call(receiver, arg);
}, 100);
});
}
};
Async.prototype.invoke = function (fn, receiver, arg) {
if (this._trampolineEnabled) {
AsyncInvoke.call(this, fn, receiver, arg);
} else {
this._schedule(function () {
fn.call(receiver, arg);
});
}
};
Async.prototype.settlePromises = function (promise) {
if (this._trampolineEnabled) {
AsyncSettlePromises.call(this, promise);
} else {
this._schedule(function () {
promise._settlePromises();
});
}
};
}
Async.prototype.invokeFirst = function (fn, receiver, arg) {
this._normalQueue.unshift(fn, receiver, arg);
this._queueTick();
};
Async.prototype._drainQueue = function (queue) {
while (queue.length() > 0) {
var fn = queue.shift();
if (typeof fn !== "function") {
fn._settlePromises();
continue;
}
var receiver = queue.shift();
var arg = queue.shift();
fn.call(receiver, arg);
}
};
Async.prototype._drainQueues = function () {
this._drainQueue(this._normalQueue);
this._reset();
this._haveDrainedQueues = true;
this._drainQueue(this._lateQueue);
};
Async.prototype._queueTick = function () {
if (!this._isTickUsed) {
this._isTickUsed = true;
this._schedule(this.drainQueues);
}
};
Async.prototype._reset = function () {
this._isTickUsed = false;
};
module.exports = Async;
module.exports.firstLineError = firstLineError;
},
{ "./queue": 26, "./schedule": 29, "./util": 36 }
],
3: [
function (_dereq_, module, exports) {
"use strict";
module.exports = function (
Promise,
INTERNAL,
tryConvertToPromise,
debug
) {
var calledBind = false;
var rejectThis = function (_, e) {
this._reject(e);
};
var targetRejected = function (e, context) {
context.promiseRejectionQueued = true;
context.bindingPromise._then(
rejectThis,
rejectThis,
null,
this,
e
);
};
var bindingResolved = function (thisArg, context) {
if ((this._bitField & 50397184) === 0) {
this._resolveCallback(context.target);
}
};
var bindingRejected = function (e, context) {
if (!context.promiseRejectionQueued) this._reject(e);
};
Promise.prototype.bind = function (thisArg) {
if (!calledBind) {
calledBind = true;
Promise.prototype._propagateFrom =
debug.propagateFromFunction();
Promise.prototype._boundValue = debug.boundValueFunction();
}
var maybePromise = tryConvertToPromise(thisArg);
var ret = new Promise(INTERNAL);
ret._propagateFrom(this, 1);
var target = this._target();
ret._setBoundTo(maybePromise);
if (maybePromise instanceof Promise) {
var context = {
promiseRejectionQueued: false,
promise: ret,
target: target,
bindingPromise: maybePromise
};
target._then(
INTERNAL,
targetRejected,
undefined,
ret,
context
);
maybePromise._then(
bindingResolved,
bindingRejected,
undefined,
ret,
context
);
ret._setOnCancel(maybePromise);
} else {
ret._resolveCallback(target);
}
return ret;
};
Promise.prototype._setBoundTo = function (obj) {
if (obj !== undefined) {
this._bitField = this._bitField | 2097152;
this._boundTo = obj;
} else {
this._bitField = this._bitField & ~2097152;
}
};
Promise.prototype._isBound = function () {
return (this._bitField & 2097152) === 2097152;
};
Promise.bind = function (thisArg, value) {
return Promise.resolve(value).bind(thisArg);
};
};
},
{}
],
4: [
function (_dereq_, module, exports) {
"use strict";
var old;
if (typeof Promise !== "undefined") old = Promise;
function noConflict() {
try {
if (Promise === bluebird) Promise = old;
} catch (e) {}
return bluebird;
}
var bluebird = _dereq_("./promise")();
bluebird.noConflict = noConflict;
module.exports = bluebird;
},
{ "./promise": 22 }
],
5: [
function (_dereq_, module, exports) {
"use strict";
var cr = Object.create;
if (cr) {
var callerCache = cr(null);
var getterCache = cr(null);
callerCache[" size"] = getterCache[" size"] = 0;
}
module.exports = function (Promise) {
var util = _dereq_("./util");
var canEvaluate = util.canEvaluate;
var isIdentifier = util.isIdentifier;
var getMethodCaller;
var getGetter;
if (!true) {
var makeMethodCaller = function (methodName) {
return new Function(
"ensureMethod",
" \n\
return function(obj) { \n\
'use strict' \n\
var len = this.length; \n\
ensureMethod(obj, 'methodName'); \n\
switch(len) { \n\
case 1: return obj.methodName(this[0]); \n\
case 2: return obj.methodName(this[0], this[1]); \n\
case 3: return obj.methodName(this[0], this[1], this[2]); \n\
case 0: return obj.methodName(); \n\
default: \n\
return obj.methodName.apply(obj, this); \n\
} \n\
}; \n\
".replace(/methodName/g, methodName)
)(ensureMethod);
};
var makeGetter = function (propertyName) {
return new Function(
"obj",
" \n\
'use strict'; \n\
return obj.propertyName; \n\
".replace("propertyName", propertyName)
);
};
var getCompiled = function (name, compiler, cache) {
var ret = cache[name];
if (typeof ret !== "function") {
if (!isIdentifier(name)) {
return null;
}
ret = compiler(name);
cache[name] = ret;
cache[" size"]++;
if (cache[" size"] > 512) {
var keys = Object.keys(cache);
for (var i = 0; i < 256; ++i) delete cache[keys[i]];
cache[" size"] = keys.length - 256;
}
}
return ret;
};
getMethodCaller = function (name) {
return getCompiled(name, makeMethodCaller, callerCache);
};
getGetter = function (name) {
return getCompiled(name, makeGetter, getterCache);
};
}
function ensureMethod(obj, methodName) {
var fn;
if (obj != null) fn = obj[methodName];
if (typeof fn !== "function") {
var message =
"Object " +
util.classString(obj) +
" has no method '" +
util.toString(methodName) +
"'";
throw new Promise.TypeError(message);
}
return fn;
}
function caller(obj) {
var methodName = this.pop();
var fn = ensureMethod(obj, methodName);
return fn.apply(obj, this);
}
Promise.prototype.call = function (methodName) {
var args = [].slice.call(arguments, 1);
if (!true) {
if (canEvaluate) {
var maybeCaller = getMethodCaller(methodName);
if (maybeCaller !== null) {
return this._then(
maybeCaller,
undefined,
undefined,
args,
undefined
);
}
}
}
args.push(methodName);
return this._then(
caller,
undefined,
undefined,
args,
undefined
);
};
function namedGetter(obj) {
return obj[this];
}
function indexedGetter(obj) {
var index = +this;
if (index < 0) index = Math.max(0, index + obj.length);
return obj[index];
}
Promise.prototype.get = function (propertyName) {
var isIndex = typeof propertyName === "number";
var getter;
if (!isIndex) {
if (canEvaluate) {
var maybeGetter = getGetter(propertyName);
getter = maybeGetter !== null ? maybeGetter : namedGetter;
} else {
getter = namedGetter;
}
} else {
getter = indexedGetter;
}
return this._then(
getter,
undefined,
undefined,
propertyName,
undefined
);
};
};
},
{ "./util": 36 }
],
6: [
function (_dereq_, module, exports) {
"use strict";
module.exports = function (
Promise,
PromiseArray,
apiRejection,
debug
) {
var util = _dereq_("./util");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
var async = Promise._async;
Promise.prototype["break"] = Promise.prototype.cancel =
function () {
if (!debug.cancellation())
return this._warn("cancellation is disabled");
var promise = this;
var child = promise;
while (promise._isCancellable()) {
if (!promise._cancelBy(child)) {
if (child._isFollowing()) {
child._followee().cancel();
} else {
child._cancelBranched();
}
break;
}
var parent = promise._cancellationParent;
if (parent == null || !parent._isCancellable()) {
if (promise._isFollowing()) {
promise._followee().cancel();
} else {
promise._cancelBranched();
}
break;
} else {
if (promise._isFollowing()) promise._followee().cancel();
promise._setWillBeCancelled();
child = promise;
promise = parent;
}
}
};
Promise.prototype._branchHasCancelled = function () {
this._branchesRemainingToCancel--;
};
Promise.prototype._enoughBranchesHaveCancelled = function () {
return (
this._branchesRemainingToCancel === undefined ||
this._branchesRemainingToCancel <= 0
);
};
Promise.prototype._cancelBy = function (canceller) {
if (canceller === this) {
this._branchesRemainingToCancel = 0;
this._invokeOnCancel();
return true;
} else {
this._branchHasCancelled();
if (this._enoughBranchesHaveCancelled()) {
this._invokeOnCancel();
return true;
}
}
return false;
};
Promise.prototype._cancelBranched = function () {
if (this._enoughBranchesHaveCancelled()) {
this._cancel();
}
};
Promise.prototype._cancel = function () {
if (!this._isCancellable()) return;
this._setCancelled();
async.invoke(this._cancelPromises, this, undefined);
};
Promise.prototype._cancelPromises = function () {
if (this._length() > 0) this._settlePromises();
};
Promise.prototype._unsetOnCancel = function () {
this._onCancelField = undefined;
};
Promise.prototype._isCancellable = function () {
return this.isPending() && !this._isCancelled();
};
Promise.prototype.isCancellable = function () {
return this.isPending() && !this.isCancelled();
};
Promise.prototype._doInvokeOnCancel = function (
onCancelCallback,
internalOnly
) {
if (util.isArray(onCancelCallback)) {
for (var i = 0; i < onCancelCallback.length; ++i) {
this._doInvokeOnCancel(onCancelCallback[i], internalOnly);
}
} else if (onCancelCallback !== undefined) {
if (typeof onCancelCallback === "function") {
if (!internalOnly) {
var e = tryCatch(onCancelCallback).call(
this._boundValue()
);
if (e === errorObj) {
this._attachExtraTrace(e.e);
async.throwLater(e.e);
}
}
} else {
onCancelCallback._resultCancelled(this);
}
}
};
Promise.prototype._invokeOnCancel = function () {
var onCancelCallback = this._onCancel();
this._unsetOnCancel();
async.invoke(this._doInvokeOnCancel, this, onCancelCallback);
};
Promise.prototype._invokeInternalOnCancel = function () {
if (this._isCancellable()) {
this._doInvokeOnCancel(this._onCancel(), true);
this._unsetOnCancel();
}
};
Promise.prototype._resultCancelled = function () {
this.cancel();
};
};
},
{ "./util": 36 }
],
7: [
function (_dereq_, module, exports) {
"use strict";
module.exports = function (NEXT_FILTER) {
var util = _dereq_("./util");
var getKeys = _dereq_("./es5").keys;
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
function catchFilter(instances, cb, promise) {
return function (e) {
var boundTo = promise._boundValue();
predicateLoop: for (var i = 0; i < instances.length; ++i) {
var item = instances[i];
if (
item === Error ||
(item != null && item.prototype instanceof Error)
) {
if (e instanceof item) {
return tryCatch(cb).call(boundTo, e);
}
} else if (typeof item === "function") {
var matchesPredicate = tryCatch(item).call(boundTo, e);
if (matchesPredicate === errorObj) {
return matchesPredicate;
} else if (matchesPredicate) {
return tryCatch(cb).call(boundTo, e);
}
} else if (util.isObject(e)) {
var keys = getKeys(item);
for (var j = 0; j < keys.length; ++j) {
var key = keys[j];
if (item[key] != e[key]) {
continue predicateLoop;
}
}
return tryCatch(cb).call(boundTo, e);
}
}
return NEXT_FILTER;
};
}
return catchFilter;
};
},
{ "./es5": 13, "./util": 36 }
],
8: [
function (_dereq_, module, exports) {
"use strict";
module.exports = function (Promise) {
var longStackTraces = false;
var contextStack = [];
Promise.prototype._promiseCreated = function () {};
Promise.prototype._pushContext = function () {};
Promise.prototype._popContext = function () {
return null;
};
Promise._peekContext = Promise.prototype._peekContext =
function () {};
function Context() {
this._trace = new Context.CapturedTrace(peekContext());
}
Context.prototype._pushContext = function () {
if (this._trace !== undefined) {
this._trace._promiseCreated = null;
contextStack.push(this._trace);
}
};
Context.prototype._popContext = function () {
if (this._trace !== undefined) {
var trace = contextStack.pop();
var ret = trace._promiseCreated;
trace._promiseCreated = null;
return ret;
}
return null;
};
function createContext() {
if (longStackTraces) return new Context();
}
function peekContext() {
var lastIndex = contextStack.length - 1;
if (lastIndex >= 0) {
return contextStack[lastIndex];
}
return undefined;
}
Context.CapturedTrace = null;
Context.create = createContext;
Context.deactivateLongStackTraces = function () {};
Context.activateLongStackTraces = function () {
var Promise_pushContext = Promise.prototype._pushContext;
var Promise_popContext = Promise.prototype._popContext;
var Promise_PeekContext = Promise._peekContext;
var Promise_peekContext = Promise.prototype._peekContext;
var Promise_promiseCreated = Promise.prototype._promiseCreated;
Context.deactivateLongStackTraces = function () {
Promise.prototype._pushContext = Promise_pushContext;
Promise.prototype._popContext = Promise_popContext;
Promise._peekContext = Promise_PeekContext;
Promise.prototype._peekContext = Promise_peekContext;
Promise.prototype._promiseCreated = Promise_promiseCreated;
longStackTraces = false;
};
longStackTraces = true;
Promise.prototype._pushContext = Context.prototype._pushContext;
Promise.prototype._popContext = Context.prototype._popContext;
Promise._peekContext = Promise.prototype._peekContext =
peekContext;
Promise.prototype._promiseCreated = function () {
var ctx = this._peekContext();
if (ctx && ctx._promiseCreated == null)
ctx._promiseCreated = this;
};
};
return Context;
};
},
{}
],
9: [
function (_dereq_, module, exports) {
"use strict";
module.exports = function (Promise, Context) {
var getDomain = Promise._getDomain;
var async = Promise._async;
var Warning = _dereq_("./errors").Warning;
var util = _dereq_("./util");
var canAttachTrace = util.canAttachTrace;
var unhandledRejectionHandled;
var possiblyUnhandledRejection;
var bluebirdFramePattern =
/[\\\/]bluebird[\\\/]js[\\\/](release|debug|instrumented)/;
var nodeFramePattern = /\((?:timers\.js):\d+:\d+\)/;
var parseLinePattern = /[\/<\(](.+?):(\d+):(\d+)\)?\s*$/;
var stackFramePattern = null;
var formatStack = null;
var indentStackFrames = false;
var printWarning;
var debugging = !!(
util.env("BLUEBIRD_DEBUG") != 0 &&
(true ||
util.env("BLUEBIRD_DEBUG") ||
util.env("NODE_ENV") === "development")
);
var warnings = !!(
util.env("BLUEBIRD_WARNINGS") != 0 &&
(debugging || util.env("BLUEBIRD_WARNINGS"))
);
var longStackTraces = !!(
util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 &&
(debugging || util.env("BLUEBIRD_LONG_STACK_TRACES"))
);
var wForgottenReturn =
util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 &&
(warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));
Promise.prototype.suppressUnhandledRejections = function () {
var target = this._target();
target._bitField = (target._bitField & ~1048576) | 524288;
};
Promise.prototype._ensurePossibleRejectionHandled = function () {
if ((this._bitField & 524288) !== 0) return;
this._setRejectionIsUnhandled();
async.invokeLater(
this._notifyUnhandledRejection,
this,
undefined
);
};
Promise.prototype._notifyUnhandledRejectionIsHandled =
function () {
fireRejectionEvent(
"rejectionHandled",
unhandledRejectionHandled,
undefined,
this
);
};
Promise.prototype._setReturnedNonUndefined = function () {
this._bitField = this._bitField | 268435456;
};
Promise.prototype._returnedNonUndefined = function () {
return (this._bitField & 268435456) !== 0;
};
Promise.prototype._notifyUnhandledRejection = function () {
if (this._isRejectionUnhandled()) {
var reason = this._settledValue();
this._setUnhandledRejectionIsNotified();
fireRejectionEvent(
"unhandledRejection",
possiblyUnhandledRejection,
reason,
this
);
}
};
Promise.prototype._setUnhandledRejectionIsNotified = function () {
this._bitField = this._bitField | 262144;
};
Promise.prototype._unsetUnhandledRejectionIsNotified =
function () {
this._bitField = this._bitField & ~262144;
};
Promise.prototype._isUnhandledRejectionNotified = function () {
return (this._bitField & 262144) > 0;
};
Promise.prototype._setRejectionIsUnhandled = function () {
this._bitField = this._bitField | 1048576;
};
Promise.prototype._unsetRejectionIsUnhandled = function () {
this._bitField = this._bitField & ~1048576;
if (this._isUnhandledRejectionNotified()) {
this._unsetUnhandledRejectionIsNotified();
this._notifyUnhandledRejectionIsHandled();
}
};
Promise.prototype._isRejectionUnhandled = function () {
return (this._bitField & 1048576) > 0;
};
Promise.prototype._warn = function (
message,
shouldUseOwnTrace,
promise
) {
return warn(message, shouldUseOwnTrace, promise || this);
};
Promise.onPossiblyUnhandledRejection = function (fn) {
var domain = getDomain();
possiblyUnhandledRejection =
typeof fn === "function"
? domain === null
? fn
: util.domainBind(domain, fn)
: undefined;
};
Promise.onUnhandledRejectionHandled = function (fn) {
var domain = getDomain();
unhandledRejectionHandled =
typeof fn === "function"
? domain === null
? fn
: util.domainBind(domain, fn)
: undefined;
};
var disableLongStackTraces = function () {};
Promise.longStackTraces = function () {
if (async.haveItemsQueued() && !config.longStackTraces) {
throw new Error(
"cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a"
);
}
if (!config.longStackTraces && longStackTracesIsSupported()) {
var Promise_captureStackTrace =
Promise.prototype._captureStackTrace;
var Promise_attachExtraTrace =
Promise.prototype._attachExtraTrace;
config.longStackTraces = true;
disableLongStackTraces = function () {
if (async.haveItemsQueued() && !config.longStackTraces) {
throw new Error(
"cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/MqrFmX\u000a"
);
}
Promise.prototype._captureStackTrace =
Promise_captureStackTrace;
Promise.prototype._attachExtraTrace =
Promise_attachExtraTrace;
Context.deactivateLongStackTraces();
async.enableTrampoline();
config.longStackTraces = false;
};
Promise.prototype._captureStackTrace =
longStackTracesCaptureStackTrace;
Promise.prototype._attachExtraTrace =
longStackTracesAttachExtraTrace;
Context.activateLongStackTraces();
async.disableTrampolineIfNecessary();
}
};
Promise.hasLongStackTraces = function () {
return config.longStackTraces && longStackTracesIsSupported();
};
var fireDomEvent = (function () {
try {
if (typeof CustomEvent === "function") {
var event = new CustomEvent("CustomEvent");
util.global.dispatchEvent(event);
return function (name, event) {
var domEvent = new CustomEvent(name.toLowerCase(), {
detail: event,
cancelable: true
});
return !util.global.dispatchEvent(domEvent);
};
} else if (typeof Event === "function") {
var event = new Event("CustomEvent");
util.global.dispatchEvent(event);
return function (name, event) {
var domEvent = new Event(name.toLowerCase(), {
cancelable: true
});
domEvent.detail = event;
return !util.global.dispatchEvent(domEvent);
};
} else {
var event = document.createEvent("CustomEvent");
event.initCustomEvent("testingtheevent", false, true, {});
util.global.dispatchEvent(event);
return function (name, event) {
var domEvent = document.createEvent("CustomEvent");
domEvent.initCustomEvent(
name.toLowerCase(),
false,
true,
event
);
return !util.global.dispatchEvent(domEvent);
};
}
} catch (e) {}
return function () {
return false;
};
})();
var fireGlobalEvent = (function () {
if (util.isNode) {
return function () {
return process.emit.apply(process, arguments);
};
} else {
if (!util.global) {
return function () {
return false;
};
}
return function (name) {
var methodName = "on" + name.toLowerCase();
var method = util.global[methodName];
if (!method) return false;
method.apply(util.global, [].slice.call(arguments, 1));
return true;
};
}
})();
function generatePromiseLifecycleEventObject(name, promise) {
return { promise: promise };
}
var eventToObjectGenerator = {
promiseCreated: generatePromiseLifecycleEventObject,
promiseFulfilled: generatePromiseLifecycleEventObject,
promiseRejected: generatePromiseLifecycleEventObject,
promiseResolved: generatePromiseLifecycleEventObject,
promiseCancelled: generatePromiseLifecycleEventObject,
promiseChained(name, promise, child) {
return { promise: promise, child: child };
},
warning(name, warning) {
return { warning: warning };
},
unhandledRejection(name, reason, promise) {
return { reason: reason, promise: promise };
},
rejectionHandled: generatePromiseLifecycleEventObject
};
var activeFireEvent = function (name) {
var globalEventFired = false;
try {
globalEventFired = fireGlobalEvent.apply(null, arguments);
} catch (e) {
async.throwLater(e);
globalEventFired = true;
}
var domEventFired = false;
try {
domEventFired = fireDomEvent(
name,
eventToObjectGenerator[name].apply(null, arguments)
);
} catch (e) {
async.throwLater(e);
domEventFired = true;
}
return domEventFired || globalEventFired;
};
Promise.config = function (opts) {
opts = Object(opts);
if ("longStackTraces" in opts) {
if (opts.longStackTraces) {
Promise.longStackTraces();
} else if (
!opts.longStackTraces &&
Promise.hasLongStackTraces()
) {
disableLongStackTraces();
}
}
if ("warnings" in opts) {
var warningsOption = opts.warnings;
config.warnings = !!warningsOption;
wForgottenReturn = config.warnings;
if (util.isObject(warningsOption)) {
if ("wForgottenReturn" in warningsOption) {
wForgottenReturn = !!warningsOption.wForgottenReturn;
}
}
}
if (
"cancellation" in opts &&
opts.cancellation &&
!config.cancellation
) {
if (async.haveItemsQueued()) {
throw new Error(
"cannot enable cancellation after promises are in use"
);
}
Promise.prototype._clearCancellationData =
cancellationClearCancellationData;
Promise.prototype._propagateFrom = cancellationPropagateFrom;
Promise.prototype._onCancel = cancellationOnCancel;
Promise.prototype._setOnCancel = cancellationSetOnCancel;
Promise.prototype._attachCancellationCallback =
cancellationAttachCancellationCallback;
Promise.prototype._execute = cancellationExecute;
propagateFromFunction = cancellationPropagateFrom;
config.cancellation = true;
}
if ("monitoring" in opts) {
if (opts.monitoring && !config.monitoring) {
config.monitoring = true;
Promise.prototype._fireEvent = activeFireEvent;
} else if (!opts.monitoring && config.monitoring) {
config.monitoring = false;
Promise.prototype._fireEvent = defaultFireEvent;
}
}
};
function defaultFireEvent() {
return false;
}
Promise.prototype._fireEvent = defaultFireEvent;
Promise.prototype._execute = function (
executor,
resolve,
reject
) {
try {
executor(resolve, reject);
} catch (e) {
return e;
}
};
Promise.prototype._onCancel = function () {};
Promise.prototype._setOnCancel = function (handler) {};
Promise.prototype._attachCancellationCallback = function (
onCancel
) {};
Promise.prototype._captureStackTrace = function () {};
Promise.prototype._attachExtraTrace = function () {};
Promise.prototype._clearCancellationData = function () {};
Promise.prototype._propagateFrom = function (parent, flags) {};
function cancellationExecute(executor, resolve, reject) {
var promise = this;
try {
executor(resolve, reject, function (onCancel) {
if (typeof onCancel !== "function") {
throw new TypeError(
"onCancel must be a function, got: " +
util.toString(onCancel)
);
}
promise._attachCancellationCallback(onCancel);
});
} catch (e) {
return e;
}
}
function cancellationAttachCancellationCallback(onCancel) {
if (!this._isCancellable()) return this;
var previousOnCancel = this._onCancel();
if (previousOnCancel !== undefined) {
if (util.isArray(previousOnCancel)) {
previousOnCancel.push(onCancel);
} else {
this._setOnCancel([previousOnCancel, onCancel]);
}
} else {
this._setOnCancel(onCancel);
}
}
function cancellationOnCancel() {
return this._onCancelField;
}
function cancellationSetOnCancel(onCancel) {
this._onCancelField = onCancel;
}
function cancellationClearCancellationData() {
this._cancellationParent = undefined;
this._onCancelField = undefined;
}
function cancellationPropagateFrom(parent, flags) {
if ((flags & 1) !== 0) {
this._cancellationParent = parent;
var branchesRemainingToCancel =
parent._branchesRemainingToCancel;
if (branchesRemainingToCancel === undefined) {
branchesRemainingToCancel = 0;
}
parent._branchesRemainingToCancel =
branchesRemainingToCancel + 1;
}
if ((flags & 2) !== 0 && parent._isBound()) {
this._setBoundTo(parent._boundTo);
}
}
function bindingPropagateFrom(parent, flags) {
if ((flags & 2) !== 0 && parent._isBound()) {
this._setBoundTo(parent._boundTo);
}
}
var propagateFromFunction = bindingPropagateFrom;
function boundValueFunction() {
var ret = this._boundTo;
if (ret !== undefined) {
if (ret instanceof Promise) {
if (ret.isFulfilled()) {
return ret.value();
} else {
return undefined;
}
}