nativeloop
Version:
⭐ Axway Amplify module for using nativeloop with Appcelerator Titanium SDK Framework
976 lines (974 loc) • 204 kB
JavaScript
"use strict";
var _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
return typeof obj;
} : function(obj) {
return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
!function(e) {
if ("object" == ("undefined" == typeof exports ? "undefined" : _typeof(exports)) && "undefined" != typeof module) module.exports = e(); else if ("function" == typeof define && define.amd) define([], e); else {
var f;
"undefined" != typeof window ? f = window : "undefined" != typeof global ? f = global : "undefined" != typeof self && (f = self),
f.Promise = e();
}
}(function() {
var define, module, exports;
return function e(t, n, r) {
function s(o, u) {
if (!n[o]) {
if (!t[o]) {
var a = "function" == typeof _dereq_ && _dereq_;
if (!u && a) return a(o, !0);
if (i) return i(o, !0);
var f = new Error("Cannot find module '" + o + "'");
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 = "function" == typeof _dereq_ && _dereq_;
for (var o = 0; o < r.length; o++) s(r[o]);
return s;
}({
1: [ function(_dereq_, module) {
module.exports = function(Promise) {
function any(promises) {
var ret = new SomePromiseArray(promises);
var promise = ret.promise();
ret.setHowMany(1);
ret.setUnwrap();
ret.init();
return promise;
}
var SomePromiseArray = Promise._SomePromiseArray;
Promise.any = function(promises) {
return any(promises);
};
Promise.prototype.any = function() {
return any(this);
};
};
}, {} ],
2: [ function(_dereq_, module) {
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;
}
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();
}
var firstLineError;
try {
throw new Error();
} catch (e) {
firstLineError = e;
}
var schedule = _dereq_("./schedule");
var Queue = _dereq_("./queue");
var util = _dereq_("./util");
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() {
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 (1 === arguments.length) {
arg = fn;
fn = function() {
throw arg;
};
}
if ("undefined" != typeof setTimeout) setTimeout(function() {
fn(arg);
}, 0); else try {
this._schedule(function() {
fn(arg);
});
} catch (e) {
throw new Error("No async scheduler available\n\n See http://goo.gl/MqrFmX\n");
}
};
if (util.hasDevTools) {
Async.prototype.invokeLater = function(fn, receiver, arg) {
this._trampolineEnabled ? AsyncInvokeLater.call(this, fn, receiver, arg) : this._schedule(function() {
setTimeout(function() {
fn.call(receiver, arg);
}, 100);
});
};
Async.prototype.invoke = function(fn, receiver, arg) {
this._trampolineEnabled ? AsyncInvoke.call(this, fn, receiver, arg) : this._schedule(function() {
fn.call(receiver, arg);
});
};
Async.prototype.settlePromises = function(promise) {
this._trampolineEnabled ? AsyncSettlePromises.call(this, promise) : this._schedule(function() {
promise._settlePromises();
});
};
} else {
Async.prototype.invokeLater = AsyncInvokeLater;
Async.prototype.invoke = AsyncInvoke;
Async.prototype.settlePromises = AsyncSettlePromises;
}
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 ("function" != typeof fn) {
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) {
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) {
0 === (50397184 & this._bitField) && this._resolveCallback(context.target);
};
var bindingRejected = function(e, context) {
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, void 0, ret, context);
maybePromise._then(bindingResolved, bindingRejected, void 0, ret, context);
ret._setOnCancel(maybePromise);
} else ret._resolveCallback(target);
return ret;
};
Promise.prototype._setBoundTo = function(obj) {
if (void 0 !== obj) {
this._bitField = 2097152 | this._bitField;
this._boundTo = obj;
} else this._bitField = -2097153 & this._bitField;
};
Promise.prototype._isBound = function() {
return 2097152 === (2097152 & this._bitField);
};
Promise.bind = function(thisArg, value) {
return Promise.resolve(value).bind(thisArg);
};
};
}, {} ],
4: [ function(_dereq_, module) {
function noConflict() {
try {
Promise === bluebird && (Promise = old);
} catch (e) {}
return bluebird;
}
var old;
"undefined" != typeof Promise && (old = Promise);
var bluebird = _dereq_("./promise")();
bluebird.noConflict = noConflict;
module.exports = bluebird;
}, {
"./promise": 22
} ],
5: [ function(_dereq_, module) {
var cr = Object.create;
if (cr) {
var callerCache = cr(null);
var getterCache = cr(null);
callerCache[" size"] = getterCache[" size"] = 0;
}
module.exports = function(Promise) {
function ensureMethod(obj, methodName) {
var fn;
null != obj && (fn = obj[methodName]);
if ("function" != typeof fn) {
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);
}
function namedGetter(obj) {
return obj[this];
}
function indexedGetter(obj) {
var index = +this;
0 > index && (index = Math.max(0, index + obj.length));
return obj[index];
}
var util = _dereq_("./util");
var canEvaluate = util.canEvaluate;
util.isIdentifier;
var getGetter;
Promise.prototype.call = function(methodName) {
var args = [].slice.call(arguments, 1);
args.push(methodName);
return this._then(caller, void 0, void 0, args, void 0);
};
Promise.prototype.get = function(propertyName) {
var isIndex = "number" == typeof propertyName;
var getter;
if (isIndex) getter = indexedGetter; else if (canEvaluate) {
var maybeGetter = getGetter(propertyName);
getter = null !== maybeGetter ? maybeGetter : namedGetter;
} else getter = namedGetter;
return this._then(getter, void 0, void 0, propertyName, void 0);
};
};
}, {
"./util": 36
} ],
6: [ function(_dereq_, module) {
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)) {
child._isFollowing() ? child._followee().cancel() : child._cancelBranched();
break;
}
var parent = promise._cancellationParent;
if (null == parent || !parent._isCancellable()) {
promise._isFollowing() ? promise._followee().cancel() : promise._cancelBranched();
break;
}
promise._isFollowing() && promise._followee().cancel();
promise._setWillBeCancelled();
child = promise;
promise = parent;
}
};
Promise.prototype._branchHasCancelled = function() {
this._branchesRemainingToCancel--;
};
Promise.prototype._enoughBranchesHaveCancelled = function() {
return void 0 === this._branchesRemainingToCancel || this._branchesRemainingToCancel <= 0;
};
Promise.prototype._cancelBy = function(canceller) {
if (canceller === this) {
this._branchesRemainingToCancel = 0;
this._invokeOnCancel();
return true;
}
this._branchHasCancelled();
if (this._enoughBranchesHaveCancelled()) {
this._invokeOnCancel();
return true;
}
return false;
};
Promise.prototype._cancelBranched = function() {
this._enoughBranchesHaveCancelled() && this._cancel();
};
Promise.prototype._cancel = function() {
if (!this._isCancellable()) return;
this._setCancelled();
async.invoke(this._cancelPromises, this, void 0);
};
Promise.prototype._cancelPromises = function() {
this._length() > 0 && this._settlePromises();
};
Promise.prototype._unsetOnCancel = function() {
this._onCancelField = void 0;
};
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 (void 0 !== onCancelCallback) if ("function" == typeof onCancelCallback) {
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) {
module.exports = function(NEXT_FILTER) {
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 || null != item && item.prototype instanceof Error) {
if (e instanceof item) return tryCatch(cb).call(boundTo, e);
} else if ("function" == typeof item) {
var matchesPredicate = tryCatch(item).call(boundTo, e);
if (matchesPredicate === errorObj) return matchesPredicate;
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;
};
}
var util = _dereq_("./util");
var getKeys = _dereq_("./es5").keys;
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
return catchFilter;
};
}, {
"./es5": 13,
"./util": 36
} ],
8: [ function(_dereq_, module) {
module.exports = function(Promise) {
function Context() {
this._trace = new Context.CapturedTrace(peekContext());
}
function createContext() {
if (longStackTraces) return new Context();
}
function peekContext() {
var lastIndex = contextStack.length - 1;
if (lastIndex >= 0) return contextStack[lastIndex];
return void 0;
}
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() {};
Context.prototype._pushContext = function() {
if (void 0 !== this._trace) {
this._trace._promiseCreated = null;
contextStack.push(this._trace);
}
};
Context.prototype._popContext = function() {
if (void 0 !== this._trace) {
var trace = contextStack.pop();
var ret = trace._promiseCreated;
trace._promiseCreated = null;
return ret;
}
return null;
};
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();
ctx && null == ctx._promiseCreated && (ctx._promiseCreated = this);
};
};
return Context;
};
}, {} ],
9: [ function(_dereq_, module) {
module.exports = function(Promise, Context) {
function generatePromiseLifecycleEventObject(name, promise) {
return {
promise: promise
};
}
function defaultFireEvent() {
return false;
}
function cancellationExecute(executor, resolve, reject) {
var promise = this;
try {
executor(resolve, reject, function(onCancel) {
if ("function" != typeof onCancel) 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();
void 0 !== previousOnCancel ? util.isArray(previousOnCancel) ? previousOnCancel.push(onCancel) : this._setOnCancel([ previousOnCancel, onCancel ]) : this._setOnCancel(onCancel);
}
function cancellationOnCancel() {
return this._onCancelField;
}
function cancellationSetOnCancel(onCancel) {
this._onCancelField = onCancel;
}
function cancellationClearCancellationData() {
this._cancellationParent = void 0;
this._onCancelField = void 0;
}
function cancellationPropagateFrom(parent, flags) {
if (0 !== (1 & flags)) {
this._cancellationParent = parent;
var branchesRemainingToCancel = parent._branchesRemainingToCancel;
void 0 === branchesRemainingToCancel && (branchesRemainingToCancel = 0);
parent._branchesRemainingToCancel = branchesRemainingToCancel + 1;
}
0 !== (2 & flags) && parent._isBound() && this._setBoundTo(parent._boundTo);
}
function bindingPropagateFrom(parent, flags) {
0 !== (2 & flags) && parent._isBound() && this._setBoundTo(parent._boundTo);
}
function _boundValueFunction() {
var ret = this._boundTo;
if (void 0 !== ret && ret instanceof Promise) return ret.isFulfilled() ? ret.value() : void 0;
return ret;
}
function longStackTracesCaptureStackTrace() {
this._trace = new CapturedTrace(this._peekContext());
}
function longStackTracesAttachExtraTrace(error, ignoreSelf) {
if (canAttachTrace(error)) {
var trace = this._trace;
void 0 !== trace && ignoreSelf && (trace = trace._parent);
if (void 0 !== trace) trace.attachExtraTrace(error); else if (!error.__stackCleaned__) {
var parsed = parseStackAndMessage(error);
util.notEnumerableProp(error, "stack", parsed.message + "\n" + parsed.stack.join("\n"));
util.notEnumerableProp(error, "__stackCleaned__", true);
}
}
}
function checkForgottenReturns(returnValue, promiseCreated, name, promise, parent) {
if (void 0 === returnValue && null !== promiseCreated && wForgottenReturn) {
if (void 0 !== parent && parent._returnedNonUndefined()) return;
if (0 === (65535 & promise._bitField)) return;
name && (name += " ");
var handlerLine = "";
var creatorLine = "";
if (promiseCreated._trace) {
var traceLines = promiseCreated._trace.stack.split("\n");
var stack = cleanStack(traceLines);
for (var i = stack.length - 1; i >= 0; --i) {
var line = stack[i];
if (!nodeFramePattern.test(line)) {
var lineMatches = line.match(parseLinePattern);
lineMatches && (handlerLine = "at " + lineMatches[1] + ":" + lineMatches[2] + ":" + lineMatches[3] + " ");
break;
}
}
if (stack.length > 0) {
var firstUserLine = stack[0];
for (var i = 0; i < traceLines.length; ++i) if (traceLines[i] === firstUserLine) {
i > 0 && (creatorLine = "\n" + traceLines[i - 1]);
break;
}
}
}
var msg = "a promise was created in a " + name + "handler " + handlerLine + "but was not returned from it, see http://goo.gl/rRqMUw" + creatorLine;
promise._warn(msg, true, promiseCreated);
}
}
function deprecated(name, replacement) {
var message = name + " is deprecated and will be removed in a future version.";
replacement && (message += " Use " + replacement + " instead.");
return warn(message);
}
function warn(message, shouldUseOwnTrace, promise) {
if (!config.warnings) return;
var warning = new Warning(message);
var ctx;
if (shouldUseOwnTrace) promise._attachExtraTrace(warning); else if (config.longStackTraces && (ctx = Promise._peekContext())) ctx.attachExtraTrace(warning); else {
var parsed = parseStackAndMessage(warning);
warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
}
activeFireEvent("warning", warning) || formatAndLogError(warning, "", true);
}
function reconstructStack(message, stacks) {
for (var i = 0; i < stacks.length - 1; ++i) {
stacks[i].push("From previous event:");
stacks[i] = stacks[i].join("\n");
}
i < stacks.length && (stacks[i] = stacks[i].join("\n"));
return message + "\n" + stacks.join("\n");
}
function removeDuplicateOrEmptyJumps(stacks) {
for (var i = 0; i < stacks.length; ++i) if (0 === stacks[i].length || i + 1 < stacks.length && stacks[i][0] === stacks[i + 1][0]) {
stacks.splice(i, 1);
i--;
}
}
function removeCommonRoots(stacks) {
var current = stacks[0];
for (var i = 1; i < stacks.length; ++i) {
var prev = stacks[i];
var currentLastIndex = current.length - 1;
var currentLastLine = current[currentLastIndex];
var commonRootMeetPoint = -1;
for (var j = prev.length - 1; j >= 0; --j) if (prev[j] === currentLastLine) {
commonRootMeetPoint = j;
break;
}
for (var j = commonRootMeetPoint; j >= 0; --j) {
var line = prev[j];
if (current[currentLastIndex] !== line) break;
current.pop();
currentLastIndex--;
}
current = prev;
}
}
function cleanStack(stack) {
var ret = [];
for (var i = 0; i < stack.length; ++i) {
var line = stack[i];
var isTraceLine = " (No stack trace)" === line || stackFramePattern.test(line);
var isInternalFrame = isTraceLine && shouldIgnore(line);
if (isTraceLine && !isInternalFrame) {
indentStackFrames && " " !== line.charAt(0) && (line = " " + line);
ret.push(line);
}
}
return ret;
}
function stackFramesAsArray(error) {
var stack = error.stack.replace(/\s+$/g, "").split("\n");
for (var i = 0; i < stack.length; ++i) {
var line = stack[i];
if (" (No stack trace)" === line || stackFramePattern.test(line)) break;
}
i > 0 && (stack = stack.slice(i));
return stack;
}
function parseStackAndMessage(error) {
var stack = error.stack;
var message = error.toString();
stack = "string" == typeof stack && stack.length > 0 ? stackFramesAsArray(error) : [ " (No stack trace)" ];
return {
message: message,
stack: cleanStack(stack)
};
}
function formatAndLogError(error, title, isSoft) {
if ("undefined" != typeof console) {
var message;
if (util.isObject(error)) {
var stack = error.stack;
message = title + formatStack(stack, error);
} else message = title + String(error);
"function" == typeof printWarning ? printWarning(message, isSoft) : ("function" == typeof console.log || "object" === _typeof(console.log)) && console.log(message);
}
}
function fireRejectionEvent(name, localHandler, reason, promise) {
var localEventFired = false;
try {
if ("function" == typeof localHandler) {
localEventFired = true;
"rejectionHandled" === name ? localHandler(promise) : localHandler(reason, promise);
}
} catch (e) {
async.throwLater(e);
}
"unhandledRejection" === name ? activeFireEvent(name, reason, promise) || localEventFired || formatAndLogError(reason, "Unhandled rejection ") : activeFireEvent(name, promise);
}
function formatNonError(obj) {
var str;
if ("function" == typeof obj) str = "[function " + (obj.name || "anonymous") + "]"; else {
str = obj && "function" == typeof obj.toString ? obj.toString() : util.toString(obj);
var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/;
if (ruselessToString.test(str)) try {
var newStr = JSON.stringify(obj);
str = newStr;
} catch (e) {}
0 === str.length && (str = "(empty array)");
}
return "(<" + snip(str) + ">, no stack trace)";
}
function snip(str) {
var maxChars = 41;
if (str.length < maxChars) return str;
return str.substr(0, maxChars - 3) + "...";
}
function longStackTracesIsSupported() {
return "function" == typeof captureStackTrace;
}
function parseLineInfo(line) {
var matches = line.match(parseLineInfoRegex);
if (matches) return {
fileName: matches[1],
line: parseInt(matches[2], 10)
};
}
function setBounds(firstLineError, lastLineError) {
if (!longStackTracesIsSupported()) return;
var firstStackLines = firstLineError.stack.split("\n");
var lastStackLines = lastLineError.stack.split("\n");
var firstIndex = -1;
var lastIndex = -1;
var firstFileName;
var lastFileName;
for (var i = 0; i < firstStackLines.length; ++i) {
var result = parseLineInfo(firstStackLines[i]);
if (result) {
firstFileName = result.fileName;
firstIndex = result.line;
break;
}
}
for (var i = 0; i < lastStackLines.length; ++i) {
var result = parseLineInfo(lastStackLines[i]);
if (result) {
lastFileName = result.fileName;
lastIndex = result.line;
break;
}
}
if (0 > firstIndex || 0 > lastIndex || !firstFileName || !lastFileName || firstFileName !== lastFileName || firstIndex >= lastIndex) return;
shouldIgnore = function(line) {
if (bluebirdFramePattern.test(line)) return true;
var info = parseLineInfo(line);
if (info && info.fileName === firstFileName && firstIndex <= info.line && info.line <= lastIndex) return true;
return false;
};
}
function CapturedTrace(parent) {
this._parent = parent;
this._promisesCreated = 0;
var length = this._length = 1 + (void 0 === parent ? 0 : parent._length);
captureStackTrace(this, CapturedTrace);
length > 32 && this.uncycle();
}
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 = !!(0 != util.env("BLUEBIRD_DEBUG") && true);
var warnings = !!(0 != util.env("BLUEBIRD_WARNINGS") && (debugging || util.env("BLUEBIRD_WARNINGS")));
var longStackTraces = !!(0 != util.env("BLUEBIRD_LONG_STACK_TRACES") && (debugging || util.env("BLUEBIRD_LONG_STACK_TRACES")));
var wForgottenReturn = 0 != util.env("BLUEBIRD_W_FORGOTTEN_RETURN") && (warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));
Promise.prototype.suppressUnhandledRejections = function() {
var target = this._target();
target._bitField = -1048577 & target._bitField | 524288;
};
Promise.prototype._ensurePossibleRejectionHandled = function() {
if (0 !== (524288 & this._bitField)) return;
this._setRejectionIsUnhandled();
async.invokeLater(this._notifyUnhandledRejection, this, void 0);
};
Promise.prototype._notifyUnhandledRejectionIsHandled = function() {
fireRejectionEvent("rejectionHandled", unhandledRejectionHandled, void 0, this);
};
Promise.prototype._setReturnedNonUndefined = function() {
this._bitField = 268435456 | this._bitField;
};
Promise.prototype._returnedNonUndefined = function() {
return 0 !== (268435456 & this._bitField);
};
Promise.prototype._notifyUnhandledRejection = function() {
if (this._isRejectionUnhandled()) {
var reason = this._settledValue();
this._setUnhandledRejectionIsNotified();
fireRejectionEvent("unhandledRejection", possiblyUnhandledRejection, reason, this);
}
};
Promise.prototype._setUnhandledRejectionIsNotified = function() {
this._bitField = 262144 | this._bitField;
};
Promise.prototype._unsetUnhandledRejectionIsNotified = function() {
this._bitField = -262145 & this._bitField;
};
Promise.prototype._isUnhandledRejectionNotified = function() {
return (262144 & this._bitField) > 0;
};
Promise.prototype._setRejectionIsUnhandled = function() {
this._bitField = 1048576 | this._bitField;
};
Promise.prototype._unsetRejectionIsUnhandled = function() {
this._bitField = -1048577 & this._bitField;
if (this._isUnhandledRejectionNotified()) {
this._unsetUnhandledRejectionIsNotified();
this._notifyUnhandledRejectionIsHandled();
}
};
Promise.prototype._isRejectionUnhandled = function() {
return (1048576 & this._bitField) > 0;
};
Promise.prototype._warn = function(message, shouldUseOwnTrace, promise) {
return warn(message, shouldUseOwnTrace, promise || this);
};
Promise.onPossiblyUnhandledRejection = function(fn) {
var domain = getDomain();
possiblyUnhandledRejection = "function" == typeof fn ? null === domain ? fn : util.domainBind(domain, fn) : void 0;
};
Promise.onUnhandledRejectionHandled = function(fn) {
var domain = getDomain();
unhandledRejectionHandled = "function" == typeof fn ? null === domain ? fn : util.domainBind(domain, fn) : void 0;
};
var disableLongStackTraces = function() {};
Promise.longStackTraces = function() {
if (async.haveItemsQueued() && !config.longStackTraces) throw new Error("cannot enable long stack traces after promises have been created\n\n See http://goo.gl/MqrFmX\n");
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\n\n See http://goo.gl/MqrFmX\n");
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 ("function" == typeof CustomEvent) {
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);
};
}
if ("function" == typeof Event) {
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);
};
}
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);
};
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;
};
}();
var eventToObjectGenerator = {
promiseCreated: generatePromiseLifecycleEventObject,
promiseFulfilled: generatePromiseLifecycleEventObject,
promiseRejected: generatePromiseLifecycleEventObject,
promiseResolved: generatePromiseLifecycleEventObject,
promiseCancelled: generatePromiseLifecycleEventObject,
promiseChained: function(name, promise, child) {
return {
promise: promise,
child: child
};
},
warning: function(name, _warning) {
return {
warning: _warning
};
},
unhandledRejection: function(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);
"longStackTraces" in opts && (opts.longStackTraces ? Promise.longStackTraces() : !opts.longStackTraces && Promise.hasLongStackTraces() && disableLongStackTraces());
if ("warnings" in opts) {
var warningsO