commit 2ac27be95ecad35f52e407fc6faabddf0ca75ed0
parent a8c364d830588d28289d8411a2c8bf48ab3cb766
Author: Dan Stillman <dstillman@zotero.org>
Date: Fri, 8 Jan 2016 21:07:10 -0500
Upgrade to Bluebird 3.1.1
Diffstat:
2 files changed, 2261 insertions(+), 1830 deletions(-)
diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js
@@ -582,7 +582,7 @@ Zotero.DBConnection.prototype.inTransaction = function () {
Zotero.DBConnection.prototype.waitForTransaction = function () {
if (!this._inTransaction) {
- return Zotero.Promise.resolve().cancellable();
+ return Zotero.Promise.resolve();
}
if (Zotero.Debug.enabled) {
Zotero.debug("Waiting for transaction to finish", 5);
diff --git a/resource/bluebird.js b/resource/bluebird.js
@@ -23,8 +23,8 @@
*
*/
/**
- * bluebird build version 2.10.2
- * Features enabled: core, race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, cancel, using, filter, any, each, timers
+ * bluebird build version 3.1.1
+ * Features enabled: core, race, call_get, generators, map, nodeify, promisify, props, reduce, settle, some, using, timers, filter, any, each
*/
!function(e){
//
@@ -89,8 +89,12 @@
};
Promise = e();
+ Promise.config({
+ warnings: true,
+ longStackTraces: true,
+ cancellation: true
+ });
// TEMP: Only turn on if debug logging enabled?
- Promise.longStackTraces();
Promise.onPossiblyUnhandledRejection(function (e, promise) {
if (e.name == 'ZoteroPromiseInterrupt') {
return;
@@ -104,8 +108,8 @@
});
return;
-
- if("object"==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=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+"'");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){
+
+ if("object"==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=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+"'");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;
@@ -132,40 +136,45 @@ Promise.prototype.any = function () {
"use strict";
var firstLineError;
try {throw new Error(); } catch (e) {firstLineError = e;}
-var schedule = _dereq_("./schedule.js");
-var Queue = _dereq_("./queue.js");
-var util = _dereq_("./util.js");
+var schedule = _dereq_("./schedule");
+var Queue = _dereq_("./queue");
+var util = _dereq_("./util");
function Async() {
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.isStatic ? schedule(this.drainQueues) : schedule;
+ this._schedule = schedule;
}
+Async.prototype.enableTrampoline = function() {
+ this._trampolineEnabled = true;
+};
+
Async.prototype.disableTrampolineIfNecessary = function() {
if (util.hasDevTools) {
this._trampolineEnabled = false;
}
};
-Async.prototype.enableTrampoline = function() {
- if (!this._trampolineEnabled) {
- this._trampolineEnabled = true;
- this._schedule = function(fn) {
- setTimeout(fn, 0);
- };
- }
+Async.prototype.haveItemsQueued = function () {
+ return this._isTickUsed || this._haveDrainedQueues;
};
-Async.prototype.haveItemsQueued = function () {
- return this._normalQueue.length() > 0;
+
+Async.prototype.fatalError = function(e, isNode) {
+ if (isNode) {
+ process.stderr.write("Fatal " + (e instanceof Error ? e.stack : e));
+ process.exit(2);
+ } else {
+ this.throwLater(e);
+ }
};
Async.prototype.throwLater = function(fn, arg) {
@@ -182,7 +191,7 @@ Async.prototype.throwLater = function(fn, arg) {
fn(arg);
});
} catch (e) {
- throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/m3OTXk\u000a");
+ throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
};
@@ -206,9 +215,6 @@ if (!util.hasDevTools) {
Async.prototype.invoke = AsyncInvoke;
Async.prototype.settlePromises = AsyncSettlePromises;
} else {
- if (schedule.isStatic) {
- schedule = function(fn) { setTimeout(fn, 0); };
- }
Async.prototype.invokeLater = function (fn, receiver, arg) {
if (this._trampolineEnabled) {
AsyncInvokeLater.call(this, fn, receiver, arg);
@@ -263,6 +269,7 @@ Async.prototype._drainQueue = function(queue) {
Async.prototype._drainQueues = function () {
this._drainQueue(this._normalQueue);
this._reset();
+ this._haveDrainedQueues = true;
this._drainQueue(this._lateQueue);
};
@@ -277,12 +284,13 @@ Async.prototype._reset = function () {
this._isTickUsed = false;
};
-module.exports = new Async();
+module.exports = Async;
module.exports.firstLineError = firstLineError;
-},{"./queue.js":28,"./schedule.js":31,"./util.js":38}],3:[function(_dereq_,module,exports){
+},{"./queue":26,"./schedule":29,"./util":36}],3:[function(_dereq_,module,exports){
"use strict";
-module.exports = function(Promise, INTERNAL, tryConvertToPromise) {
+module.exports = function(Promise, INTERNAL, tryConvertToPromise, debug) {
+var calledBind = false;
var rejectThis = function(_, e) {
this._reject(e);
};
@@ -293,7 +301,7 @@ var targetRejected = function(e, context) {
};
var bindingResolved = function(thisArg, context) {
- if (this._isPending()) {
+ if (((this._bitField & 50397184) === 0)) {
this._resolveCallback(context.target);
}
};
@@ -303,11 +311,15 @@ var bindingRejected = function(e, context) {
};
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 = {
@@ -316,9 +328,10 @@ Promise.prototype.bind = function (thisArg) {
target: target,
bindingPromise: maybePromise
};
- target._then(INTERNAL, targetRejected, ret._progress, ret, context);
+ target._then(INTERNAL, targetRejected, undefined, ret, context);
maybePromise._then(
- bindingResolved, bindingRejected, ret._progress, ret, context);
+ bindingResolved, bindingRejected, undefined, ret, context);
+ ret._setOnCancel(maybePromise);
} else {
ret._resolveCallback(target);
}
@@ -327,30 +340,19 @@ Promise.prototype.bind = function (thisArg) {
Promise.prototype._setBoundTo = function (obj) {
if (obj !== undefined) {
- this._bitField = this._bitField | 131072;
+ this._bitField = this._bitField | 2097152;
this._boundTo = obj;
} else {
- this._bitField = this._bitField & (~131072);
+ this._bitField = this._bitField & (~2097152);
}
};
Promise.prototype._isBound = function () {
- return (this._bitField & 131072) === 131072;
+ return (this._bitField & 2097152) === 2097152;
};
Promise.bind = function (thisArg, value) {
- var maybePromise = tryConvertToPromise(thisArg);
- var ret = new Promise(INTERNAL);
-
- ret._setBoundTo(maybePromise);
- if (maybePromise instanceof Promise) {
- maybePromise._then(function() {
- ret._resolveCallback(value);
- }, ret._reject, ret._progress, ret, null);
- } else {
- ret._resolveCallback(value);
- }
- return ret;
+ return Promise.resolve(value).bind(thisArg);
};
};
@@ -363,11 +365,11 @@ function noConflict() {
catch (e) {}
return bluebird;
}
-var bluebird = _dereq_("./promise.js")();
+var bluebird = _dereq_("./promise")();
bluebird.noConflict = noConflict;
module.exports = bluebird;
-},{"./promise.js":23}],5:[function(_dereq_,module,exports){
+},{"./promise":22}],5:[function(_dereq_,module,exports){
"use strict";
var cr = Object.create;
if (cr) {
@@ -377,7 +379,7 @@ if (cr) {
}
module.exports = function(Promise) {
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var canEvaluate = util.canEvaluate;
var isIdentifier = util.isIdentifier;
@@ -453,7 +455,7 @@ function caller(obj) {
return fn.apply(obj, this);
}
Promise.prototype.call = function (methodName) {
- var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];}
+ var args = [].slice.call(arguments, 1);;
if (!true) {
if (canEvaluate) {
var maybeCaller = getMethodCaller(methodName);
@@ -492,269 +494,690 @@ Promise.prototype.get = function (propertyName) {
};
};
-},{"./util.js":38}],6:[function(_dereq_,module,exports){
+},{"./util":36}],6:[function(_dereq_,module,exports){
"use strict";
-module.exports = function(Promise) {
-var errors = _dereq_("./errors.js");
-var async = _dereq_("./async.js");
-var CancellationError = errors.CancellationError;
+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._cancel = function (reason) {
- if (!this.isCancellable()) return this;
- var parent;
- var promiseToReject = this;
- while ((parent = promiseToReject._cancellationParent) !== undefined &&
- parent.isCancellable()) {
- promiseToReject = parent;
+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();
+ child = promise;
+ promise = parent;
+ }
}
- this._unsetCancellable();
- promiseToReject._target()._rejectCallback(reason, false, true);
};
-Promise.prototype.cancel = function (reason) {
- if (!this.isCancellable()) return this;
- if (reason === undefined) reason = new CancellationError();
- async.invokeLater(this._cancel, this, reason);
- return this;
+Promise.prototype._branchHasCancelled = function() {
+ this._branchesRemainingToCancel--;
};
-Promise.prototype.cancellable = function () {
- if (this._cancellable()) return this;
- async.enableTrampoline();
- this._setCancellable();
- this._cancellationParent = undefined;
- return this;
+Promise.prototype._enoughBranchesHaveCancelled = function() {
+ return this._branchesRemainingToCancel === undefined ||
+ this._branchesRemainingToCancel <= 0;
};
-Promise.prototype.uncancellable = function () {
- var ret = this.then();
- ret._unsetCancellable();
- return ret;
+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.fork = function (didFulfill, didReject, didProgress) {
- var ret = this._then(didFulfill, didReject, didProgress,
- undefined, undefined);
-
- ret._setCancellable();
- ret._cancellationParent = undefined;
- return ret;
+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);
};
-},{"./async.js":2,"./errors.js":13}],7:[function(_dereq_,module,exports){
-"use strict";
-module.exports = function() {
-var async = _dereq_("./async.js");
-var util = _dereq_("./util.js");
-var bluebirdFramePattern =
- /[\\\/]bluebird[\\\/]js[\\\/](main|debug|zalgo|instrumented)/;
-var stackFramePattern = null;
-var formatStack = null;
-var indentStackFrames = false;
-var warn;
+Promise.prototype._cancelPromises = function() {
+ if (this._length() > 0) this._settlePromises();
+};
-function CapturedTrace(parent) {
- this._parent = parent;
- var length = this._length = 1 + (parent === undefined ? 0 : parent._length);
- captureStackTrace(this, CapturedTrace);
- if (length > 32) this.uncycle();
-}
-util.inherits(CapturedTrace, Error);
+Promise.prototype._unsetOnCancel = function() {
+ this._onCancelField = undefined;
+};
-CapturedTrace.prototype.uncycle = function() {
- var length = this._length;
- if (length < 2) return;
- var nodes = [];
- var stackToIndex = {};
+Promise.prototype.isCancellable = function() {
+ return this.isPending() && !this.isCancelled();
+};
- for (var i = 0, node = this; node !== undefined; ++i) {
- nodes.push(node);
- node = node._parent;
- }
- length = this._length = i;
- for (var i = length - 1; i >= 0; --i) {
- var stack = nodes[i].stack;
- if (stackToIndex[stack] === undefined) {
- stackToIndex[stack] = i;
+Promise.prototype._doInvokeOnCancel = function(onCancelCallback, internalOnly) {
+ if (util.isArray(onCancelCallback)) {
+ for (var i = 0; i < onCancelCallback.length; ++i) {
+ this._doInvokeOnCancel(onCancelCallback[i], internalOnly);
}
- }
- for (var i = 0; i < length; ++i) {
- var currentStack = nodes[i].stack;
- var index = stackToIndex[currentStack];
- if (index !== undefined && index !== i) {
- if (index > 0) {
- nodes[index - 1]._parent = undefined;
- nodes[index - 1]._length = 1;
- }
- nodes[i]._parent = undefined;
- nodes[i]._length = 1;
- var cycleEdgeNode = i > 0 ? nodes[i - 1] : this;
-
- if (index < length - 1) {
- cycleEdgeNode._parent = nodes[index + 1];
- cycleEdgeNode._parent.uncycle();
- cycleEdgeNode._length =
- cycleEdgeNode._parent._length + 1;
- } else {
- cycleEdgeNode._parent = undefined;
- cycleEdgeNode._length = 1;
- }
- var currentChildLength = cycleEdgeNode._length + 1;
- for (var j = i - 2; j >= 0; --j) {
- nodes[j]._length = currentChildLength;
- currentChildLength++;
+ } 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);
+ }
}
- return;
+ } else {
+ onCancelCallback._resultCancelled(this);
}
}
};
-CapturedTrace.prototype.parent = function() {
- return this._parent;
+Promise.prototype._invokeOnCancel = function() {
+ var onCancelCallback = this._onCancel();
+ this._unsetOnCancel();
+ async.invoke(this._doInvokeOnCancel, this, onCancelCallback);
};
-CapturedTrace.prototype.hasParent = function() {
- return this._parent !== undefined;
+Promise.prototype._invokeInternalOnCancel = function() {
+ if (this.isCancellable()) {
+ this._doInvokeOnCancel(this._onCancel(), true);
+ this._unsetOnCancel();
+ }
};
-CapturedTrace.prototype.attachExtraTrace = function(error) {
- if (error.__stackCleaned__) return;
- this.uncycle();
- var parsed = CapturedTrace.parseStackAndMessage(error);
- var message = parsed.message;
- var stacks = [parsed.stack];
+Promise.prototype._resultCancelled = function() {
+ this.cancel();
+};
- var trace = this;
- while (trace !== undefined) {
- stacks.push(cleanStack(trace.stack.split("\n")));
- trace = trace._parent;
- }
- removeCommonRoots(stacks);
- removeDuplicateOrEmptyJumps(stacks);
- util.notEnumerableProp(error, "stack", reconstructStack(message, stacks));
- util.notEnumerableProp(error, "__stackCleaned__", 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");
- }
- if (i < stacks.length) {
- stacks[i] = stacks[i].join("\n");
- }
- return message + "\n" + stacks.join("\n");
-}
+},{"./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 removeDuplicateOrEmptyJumps(stacks) {
- for (var i = 0; i < stacks.length; ++i) {
- if (stacks[i].length === 0 ||
- ((i + 1 < stacks.length) && stacks[i][0] === stacks[i+1][0])) {
- stacks.splice(i, 1);
- i--;
+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;
+ };
}
-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;
+return catchFilter;
+};
- for (var j = prev.length - 1; j >= 0; --j) {
- if (prev[j] === currentLastLine) {
- commonRootMeetPoint = j;
- break;
- }
- }
+},{"./es5":13,"./util":36}],8:[function(_dereq_,module,exports){
+"use strict";
+module.exports = function(Promise) {
+var longStackTraces = false;
+var contextStack = [];
- for (var j = commonRootMeetPoint; j >= 0; --j) {
- var line = prev[j];
- if (current[currentLastIndex] === line) {
- current.pop();
- currentLastIndex--;
- } else {
- break;
- }
- }
- current = prev;
- }
+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);
+ }
+};
-function cleanStack(stack) {
- var ret = [];
- for (var i = 0; i < stack.length; ++i) {
- var line = stack[i];
- var isTraceLine = stackFramePattern.test(line) ||
- " (No stack trace)" === line;
- var isInternalFrame = isTraceLine && shouldIgnore(line);
- if (isTraceLine && !isInternalFrame) {
- if (indentStackFrames && line.charAt(0) !== " ") {
- line = " " + line;
- }
- ret.push(line);
- }
+Context.prototype._popContext = function () {
+ if (this._trace !== undefined) {
+ var trace = contextStack.pop();
+ var ret = trace._promiseCreated;
+ trace._promiseCreated = null;
+ return ret;
}
- return ret;
+ return null;
+};
+
+function createContext() {
+ if (longStackTraces) return new Context();
}
-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;
- }
- }
- if (i > 0) {
- stack = stack.slice(i);
+function peekContext() {
+ var lastIndex = contextStack.length - 1;
+ if (lastIndex >= 0) {
+ return contextStack[lastIndex];
}
- return stack;
+ return undefined;
}
-
-CapturedTrace.parseStackAndMessage = function(error) {
- var stack = error.stack;
- var message = error.toString();
- stack = typeof stack === "string" && stack.length > 0
- ? stackFramesAsArray(error) : [" (No stack trace)"];
- return {
- message: message,
- stack: cleanStack(stack)
+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;
+};
-CapturedTrace.formatAndLogError = function(error, title) {
- if (typeof console !== "undefined") {
- var message;
- if (typeof error === "object" || typeof error === "function") {
- var stack = error.stack;
- message = title + formatStack(stack, error);
- } else {
- message = title + String(error);
- }
- if (typeof warn === "function") {
- warn(message);
- } else if (typeof console.log === "function" ||
- typeof console.log === "object") {
- console.log(message);
- }
+},{}],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 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);
}
};
-CapturedTrace.unhandledRejection = function (reason) {
- CapturedTrace.formatAndLogError(reason, "^--- With additional stack trace: ");
+Promise.prototype._setUnhandledRejectionIsNotified = function () {
+ this._bitField = this._bitField | 262144;
};
-CapturedTrace.isSupported = function () {
- return typeof captureStackTrace === "function";
+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 : domain.bind(fn))
+ : undefined;
+};
+
+Promise.onUnhandledRejectionHandled = function (fn) {
+ var domain = getDomain();
+ unhandledRejectionHandled =
+ typeof fn === "function" ? (domain === null ? fn : domain.bind(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();
+ }
};
-CapturedTrace.fireRejectionEvent =
-function(name, localHandler, reason, promise) {
+Promise.hasLongStackTraces = function () {
+ return config.longStackTraces && longStackTracesIsSupported();
+};
+
+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;
+ }
+};
+
+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;
+ }
+ }
+ }
+ return ret;
+}
+
+function longStackTracesCaptureStackTrace() {
+ this._trace = new CapturedTrace(this._peekContext());
+}
+
+function longStackTracesAttachExtraTrace(error, ignoreSelf) {
+ if (canAttachTrace(error)) {
+ var trace = this._trace;
+ if (trace !== undefined) {
+ if (ignoreSelf) trace = trace._parent;
+ }
+ if (trace !== undefined) {
+ 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 (returnValue === undefined && promiseCreated !== null &&
+ wForgottenReturn) {
+ if (parent !== undefined && parent._returnedNonUndefined()) return;
+
+ if (name) name = name + " ";
+ var msg = "a promise was created in a " + name +
+ "handler but was not returned from it";
+ promise._warn(msg, true, promiseCreated);
+ }
+}
+
+function deprecated(name, replacement) {
+ var message = name +
+ " is deprecated and will be removed in a future version.";
+ if (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");
+ }
+ 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");
+ }
+ if (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 (stacks[i].length === 0 ||
+ ((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) {
+ current.pop();
+ currentLastIndex--;
+ } else {
+ break;
+ }
+ }
+ 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) {
+ if (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;
+ }
+ }
+ if (i > 0) {
+ stack = stack.slice(i);
+ }
+ return stack;
+}
+
+function parseStackAndMessage(error) {
+ var stack = error.stack;
+ var message = error.toString();
+ stack = typeof stack === "string" && stack.length > 0
+ ? stackFramesAsArray(error) : [" (No stack trace)"];
+ return {
+ message: message,
+ stack: cleanStack(stack)
+ };
+}
+
+function formatAndLogError(error, title, isSoft) {
+ if (typeof console !== "undefined") {
+ var message;
+ if (util.isObject(error)) {
+ var stack = error.stack;
+ message = title + formatStack(stack, error);
+ } else {
+ message = title + String(error);
+ }
+ if (typeof printWarning === "function") {
+ printWarning(message, isSoft);
+ } else if (typeof console.log === "function" ||
+ typeof console.log === "object") {
+ console.log(message);
+ }
+ }
+}
+
+function fireRejectionEvent(name, localHandler, reason, promise) {
var localEventFired = false;
try {
if (typeof localHandler === "function") {
@@ -792,9 +1215,9 @@ function(name, localHandler, reason, promise) {
if (!globalEventFired && !localEventFired && !domEventFired &&
name === "unhandledRejection") {
- CapturedTrace.formatAndLogError(reason, "Unhandled rejection ");
+ formatAndLogError(reason, "Unhandled rejection ");
}
-};
+}
function formatNonError(obj) {
var str;
@@ -803,7 +1226,8 @@ function formatNonError(obj) {
(obj.name || "anonymous") +
"]";
} else {
- str = obj.toString();
+ str = obj && typeof obj.toString === "function"
+ ? obj.toString() : util.toString(obj);
var ruselessToString = /\[object [a-zA-Z0-9$_]+\]/;
if (ruselessToString.test(str)) {
try {
@@ -829,6 +1253,10 @@ function snip(str) {
return str.substr(0, maxChars - 3) + "...";
}
+function longStackTracesIsSupported() {
+ return typeof captureStackTrace === "function";
+}
+
var shouldIgnore = function() { return false; };
var parseLineInfoRegex = /[\/<\(]([^:\/]+):(\d+):(?:\d+)\)?\s*$/;
function parseLineInfo(line) {
@@ -840,8 +1268,9 @@ function parseLineInfo(line) {
};
}
}
-CapturedTrace.setBounds = function(firstLineError, lastLineError) {
- if (!CapturedTrace.isSupported()) return;
+
+function setBounds(firstLineError, lastLineError) {
+ if (!longStackTracesIsSupported()) return;
var firstStackLines = firstLineError.stack.split("\n");
var lastStackLines = lastLineError.stack.split("\n");
var firstIndex = -1;
@@ -880,6 +1309,82 @@ CapturedTrace.setBounds = function(firstLineError, lastLineError) {
}
return false;
};
+}
+
+function CapturedTrace(parent) {
+ this._parent = parent;
+ this._promisesCreated = 0;
+ var length = this._length = 1 + (parent === undefined ? 0 : parent._length);
+ captureStackTrace(this, CapturedTrace);
+ if (length > 32) this.uncycle();
+}
+util.inherits(CapturedTrace, Error);
+Context.CapturedTrace = CapturedTrace;
+
+CapturedTrace.prototype.uncycle = function() {
+ var length = this._length;
+ if (length < 2) return;
+ var nodes = [];
+ var stackToIndex = {};
+
+ for (var i = 0, node = this; node !== undefined; ++i) {
+ nodes.push(node);
+ node = node._parent;
+ }
+ length = this._length = i;
+ for (var i = length - 1; i >= 0; --i) {
+ var stack = nodes[i].stack;
+ if (stackToIndex[stack] === undefined) {
+ stackToIndex[stack] = i;
+ }
+ }
+ for (var i = 0; i < length; ++i) {
+ var currentStack = nodes[i].stack;
+ var index = stackToIndex[currentStack];
+ if (index !== undefined && index !== i) {
+ if (index > 0) {
+ nodes[index - 1]._parent = undefined;
+ nodes[index - 1]._length = 1;
+ }
+ nodes[i]._parent = undefined;
+ nodes[i]._length = 1;
+ var cycleEdgeNode = i > 0 ? nodes[i - 1] : this;
+
+ if (index < length - 1) {
+ cycleEdgeNode._parent = nodes[index + 1];
+ cycleEdgeNode._parent.uncycle();
+ cycleEdgeNode._length =
+ cycleEdgeNode._parent._length + 1;
+ } else {
+ cycleEdgeNode._parent = undefined;
+ cycleEdgeNode._length = 1;
+ }
+ var currentChildLength = cycleEdgeNode._length + 1;
+ for (var j = i - 2; j >= 0; --j) {
+ nodes[j]._length = currentChildLength;
+ currentChildLength++;
+ }
+ return;
+ }
+ }
+};
+
+CapturedTrace.prototype.attachExtraTrace = function(error) {
+ if (error.__stackCleaned__) return;
+ this.uncycle();
+ var parsed = parseStackAndMessage(error);
+ var message = parsed.message;
+ var stacks = [parsed.stack];
+
+ var trace = this;
+ while (trace !== undefined) {
+ stacks.push(cleanStack(trace.stack.split("\n")));
+ trace = trace._parent;
+ }
+ removeCommonRoots(stacks);
+ removeDuplicateOrEmptyJumps(stacks);
+ util.notEnumerableProp(error, "stack", reconstructStack(message, stacks));
+ util.notEnumerableProp(error, "__stackCleaned__", true);
};
var captureStackTrace = (function stackDetection() {
@@ -896,7 +1401,7 @@ var captureStackTrace = (function stackDetection() {
if (typeof Error.stackTraceLimit === "number" &&
typeof Error.captureStackTrace === "function") {
- Error.stackTraceLimit = Error.stackTraceLimit + 6;
+ Error.stackTraceLimit += 6;
stackFramePattern = v8stackFramePattern;
formatStack = v8stackFormatter;
var captureStackTrace = Error.captureStackTrace;
@@ -905,9 +1410,9 @@ var captureStackTrace = (function stackDetection() {
return bluebirdFramePattern.test(line);
};
return function(receiver, ignoreUntil) {
- Error.stackTraceLimit = Error.stackTraceLimit + 6;
+ Error.stackTraceLimit += 6;
captureStackTrace(receiver, ignoreUntil);
- Error.stackTraceLimit = Error.stackTraceLimit - 6;
+ Error.stackTraceLimit -= 6;
};
}
var err = new Error();
@@ -932,10 +1437,10 @@ var captureStackTrace = (function stackDetection() {
stackFramePattern = v8stackFramePattern;
formatStack = v8stackFormatter;
return function captureStackTrace(o) {
- Error.stackTraceLimit = Error.stackTraceLimit + 6;
+ Error.stackTraceLimit += 6;
try { throw new Error(); }
catch(e) { o.stack = e.stack; }
- Error.stackTraceLimit = Error.stackTraceLimit - 6;
+ Error.stackTraceLimit -= 6;
};
}
@@ -966,38 +1471,27 @@ var fireGlobalEvent = (function() {
}
};
} else {
- var customEventWorks = false;
- var anyEventWorks = true;
- try {
- var ev = new self.CustomEvent("test");
- customEventWorks = ev instanceof CustomEvent;
- } catch (e) {}
- if (!customEventWorks) {
- try {
- var event = document.createEvent("CustomEvent");
- event.initCustomEvent("testingtheevent", false, true, {});
- self.dispatchEvent(event);
- } catch (e) {
- anyEventWorks = false;
- }
+ var globalObject = typeof self !== "undefined" ? self :
+ typeof window !== "undefined" ? window :
+ typeof global !== "undefined" ? global :
+ this !== undefined ? this : null;
+
+ if (!globalObject) {
+ return function() {
+ return false;
+ };
}
- if (anyEventWorks) {
- fireDomEvent = function(type, detail) {
- var event;
- if (customEventWorks) {
- event = new self.CustomEvent(type, {
- detail: detail,
- bubbles: false,
- cancelable: true
- });
- } else if (self.dispatchEvent) {
- event = document.createEvent("CustomEvent");
- event.initCustomEvent(type, false, true, detail);
- }
- return event ? !self.dispatchEvent(event) : false;
+ try {
+ var event = document.createEvent("CustomEvent");
+ event.initCustomEvent("testingtheevent", false, true, {});
+ globalObject.dispatchEvent(event);
+ fireDomEvent = function(type, detail) {
+ var event = document.createEvent("CustomEvent");
+ event.initCustomEvent(type, false, true, detail);
+ return !globalObject.dispatchEvent(event);
};
- }
+ } catch (e) {}
var toWindowMethodNameMap = {};
toWindowMethodNameMap["unhandledRejection"] = ("on" +
@@ -1007,12 +1501,12 @@ var fireGlobalEvent = (function() {
return function(name, reason, promise) {
var methodName = toWindowMethodNameMap[name];
- var method = self[methodName];
+ var method = globalObject[methodName];
if (!method) return false;
if (name === "rejectionHandled") {
- method.call(self, promise);
+ method.call(globalObject, promise);
} else {
- method.call(self, reason, promise);
+ method.call(globalObject, reason, promise);
}
return true;
};
@@ -1020,379 +1514,138 @@ var fireGlobalEvent = (function() {
})();
if (typeof console !== "undefined" && typeof console.warn !== "undefined") {
- warn = function (message) {
+ printWarning = function (message) {
console.warn(message);
};
if (util.isNode && process.stderr.isTTY) {
- warn = function(message) {
- process.stderr.write("\u001b[31m" + message + "\u001b[39m\n");
+ printWarning = function(message, isSoft) {
+ var color = isSoft ? "\u001b[33m" : "\u001b[31m";
+ console.warn(color + message + "\u001b[0m\n");
};
} else if (!util.isNode && typeof (new Error().stack) === "string") {
- warn = function(message) {
- console.warn("%c" + message, "color: red");
+ printWarning = function(message, isSoft) {
+ console.warn("%c" + message,
+ isSoft ? "color: darkorange" : "color: red");
};
}
}
-return CapturedTrace;
-};
-
-},{"./async.js":2,"./util.js":38}],8:[function(_dereq_,module,exports){
+var config = {
+ warnings: warnings,
+ longStackTraces: false,
+ cancellation: false
+};
+
+if (longStackTraces) Promise.longStackTraces();
+
+return {
+ longStackTraces: function() {
+ return config.longStackTraces;
+ },
+ warnings: function() {
+ return config.warnings;
+ },
+ cancellation: function() {
+ return config.cancellation;
+ },
+ propagateFromFunction: function() {
+ return propagateFromFunction;
+ },
+ boundValueFunction: function() {
+ return boundValueFunction;
+ },
+ checkForgottenReturns: checkForgottenReturns,
+ setBounds: setBounds,
+ warn: warn,
+ deprecated: deprecated,
+ CapturedTrace: CapturedTrace
+};
+};
+
+},{"./errors":12,"./util":36}],10:[function(_dereq_,module,exports){
"use strict";
-module.exports = function(NEXT_FILTER) {
-var util = _dereq_("./util.js");
-var errors = _dereq_("./errors.js");
-var tryCatch = util.tryCatch;
-var errorObj = util.errorObj;
-var keys = _dereq_("./es5.js").keys;
-var TypeError = errors.TypeError;
-
-function CatchFilter(instances, callback, promise) {
- this._instances = instances;
- this._callback = callback;
- this._promise = promise;
+module.exports = function(Promise) {
+function returner() {
+ return this.value;
}
-
-function safePredicate(predicate, e) {
- var safeObject = {};
- var retfilter = tryCatch(predicate).call(safeObject, e);
-
- if (retfilter === errorObj) return retfilter;
-
- var safeKeys = keys(safeObject);
- if (safeKeys.length) {
- errorObj.e = new TypeError("Catch filter must inherit from Error or be a simple predicate function\u000a\u000a See http://goo.gl/o84o68\u000a");
- return errorObj;
- }
- return retfilter;
+function thrower() {
+ throw this.reason;
}
-CatchFilter.prototype.doFilter = function (e) {
- var cb = this._callback;
- var promise = this._promise;
- var boundTo = promise._boundValue();
- for (var i = 0, len = this._instances.length; i < len; ++i) {
- var item = this._instances[i];
- var itemIsErrorType = item === Error ||
- (item != null && item.prototype instanceof Error);
-
- if (itemIsErrorType && e instanceof item) {
- var ret = tryCatch(cb).call(boundTo, e);
- if (ret === errorObj) {
- NEXT_FILTER.e = ret.e;
- return NEXT_FILTER;
- }
- return ret;
- } else if (typeof item === "function" && !itemIsErrorType) {
- var shouldHandle = safePredicate(item, e);
- if (shouldHandle === errorObj) {
- e = errorObj.e;
- break;
- } else if (shouldHandle) {
- var ret = tryCatch(cb).call(boundTo, e);
- if (ret === errorObj) {
- NEXT_FILTER.e = ret.e;
- return NEXT_FILTER;
- }
- return ret;
- }
- }
- }
- NEXT_FILTER.e = e;
- return NEXT_FILTER;
+Promise.prototype["return"] =
+Promise.prototype.thenReturn = function (value) {
+ if (value instanceof Promise) value.suppressUnhandledRejections();
+ return this._then(
+ returner, undefined, undefined, {value: value}, undefined);
};
-return CatchFilter;
+Promise.prototype["throw"] =
+Promise.prototype.thenThrow = function (reason) {
+ return this._then(
+ thrower, undefined, undefined, {reason: reason}, undefined);
};
-},{"./errors.js":13,"./es5.js":14,"./util.js":38}],9:[function(_dereq_,module,exports){
-"use strict";
-module.exports = function(Promise, CapturedTrace, isDebugging) {
-var contextStack = [];
-function Context() {
- this._trace = new CapturedTrace(peekContext());
-}
-Context.prototype._pushContext = function () {
- if (!isDebugging()) return;
- if (this._trace !== undefined) {
- contextStack.push(this._trace);
+Promise.prototype.catchThrow = function (reason) {
+ if (arguments.length <= 1) {
+ return this._then(
+ undefined, thrower, undefined, {reason: reason}, undefined);
+ } else {
+ var _reason = arguments[1];
+ var handler = function() {throw _reason;};
+ return this.caught(reason, handler);
}
};
-Context.prototype._popContext = function () {
- if (!isDebugging()) return;
- if (this._trace !== undefined) {
- contextStack.pop();
+Promise.prototype.catchReturn = function (value) {
+ if (arguments.length <= 1) {
+ if (value instanceof Promise) value.suppressUnhandledRejections();
+ return this._then(
+ undefined, returner, undefined, {value: value}, undefined);
+ } else {
+ var _value = arguments[1];
+ if (_value instanceof Promise) _value.suppressUnhandledRejections();
+ var handler = function() {return _value;};
+ return this.caught(value, handler);
}
};
-
-function createContext() {
- if (isDebugging()) return new Context();
-}
-
-function peekContext() {
- var lastIndex = contextStack.length - 1;
- if (lastIndex >= 0) {
- return contextStack[lastIndex];
- }
- return undefined;
-}
-
-Promise.prototype._peekContext = peekContext;
-Promise.prototype._pushContext = Context.prototype._pushContext;
-Promise.prototype._popContext = Context.prototype._popContext;
-
-return createContext;
};
-},{}],10:[function(_dereq_,module,exports){
+},{}],11:[function(_dereq_,module,exports){
"use strict";
-module.exports = function(Promise, CapturedTrace) {
-var getDomain = Promise._getDomain;
-var async = _dereq_("./async.js");
-var Warning = _dereq_("./errors.js").Warning;
-var util = _dereq_("./util.js");
-var canAttachTrace = util.canAttachTrace;
-var unhandledRejectionHandled;
-var possiblyUnhandledRejection;
-var debugging = false || (util.isNode &&
- (!!process.env["BLUEBIRD_DEBUG"] ||
- process.env["NODE_ENV"] === "development"));
-
-if (util.isNode && process.env["BLUEBIRD_DEBUG"] == 0) debugging = false;
+module.exports = function(Promise, INTERNAL) {
+var PromiseReduce = Promise.reduce;
+var PromiseAll = Promise.all;
-if (debugging) {
- async.disableTrampolineIfNecessary();
+function promiseAllThis() {
+ return PromiseAll(this);
}
-Promise.prototype._ignoreRejections = function() {
- this._unsetRejectionIsUnhandled();
- this._bitField = this._bitField | 16777216;
-};
-
-Promise.prototype._ensurePossibleRejectionHandled = function () {
- if ((this._bitField & 16777216) !== 0) return;
- this._setRejectionIsUnhandled();
- async.invokeLater(this._notifyUnhandledRejection, this, undefined);
-};
-
-Promise.prototype._notifyUnhandledRejectionIsHandled = function () {
- CapturedTrace.fireRejectionEvent("rejectionHandled",
- unhandledRejectionHandled, undefined, this);
-};
-
-Promise.prototype._notifyUnhandledRejection = function () {
- if (this._isRejectionUnhandled()) {
- var reason = this._getCarriedStackTrace() || this._settledValue;
- this._setUnhandledRejectionIsNotified();
- CapturedTrace.fireRejectionEvent("unhandledRejection",
- possiblyUnhandledRejection, reason, this);
- }
-};
-
-Promise.prototype._setUnhandledRejectionIsNotified = function () {
- this._bitField = this._bitField | 524288;
-};
-
-Promise.prototype._unsetUnhandledRejectionIsNotified = function () {
- this._bitField = this._bitField & (~524288);
-};
-
-Promise.prototype._isUnhandledRejectionNotified = function () {
- return (this._bitField & 524288) > 0;
-};
-
-Promise.prototype._setRejectionIsUnhandled = function () {
- this._bitField = this._bitField | 2097152;
-};
-
-Promise.prototype._unsetRejectionIsUnhandled = function () {
- this._bitField = this._bitField & (~2097152);
- if (this._isUnhandledRejectionNotified()) {
- this._unsetUnhandledRejectionIsNotified();
- this._notifyUnhandledRejectionIsHandled();
- }
-};
-
-Promise.prototype._isRejectionUnhandled = function () {
- return (this._bitField & 2097152) > 0;
-};
-
-Promise.prototype._setCarriedStackTrace = function (capturedTrace) {
- this._bitField = this._bitField | 1048576;
- this._fulfillmentHandler0 = capturedTrace;
-};
-
-Promise.prototype._isCarryingStackTrace = function () {
- return (this._bitField & 1048576) > 0;
-};
-
-Promise.prototype._getCarriedStackTrace = function () {
- return this._isCarryingStackTrace()
- ? this._fulfillmentHandler0
- : undefined;
-};
-
-Promise.prototype._captureStackTrace = function () {
- if (debugging) {
- this._trace = new CapturedTrace(this._peekContext());
- }
- return this;
-};
-
-Promise.prototype._attachExtraTrace = function (error, ignoreSelf) {
- if (debugging && canAttachTrace(error)) {
- var trace = this._trace;
- if (trace !== undefined) {
- if (ignoreSelf) trace = trace._parent;
- }
- if (trace !== undefined) {
- trace.attachExtraTrace(error);
- } else if (!error.__stackCleaned__) {
- var parsed = CapturedTrace.parseStackAndMessage(error);
- util.notEnumerableProp(error, "stack",
- parsed.message + "\n" + parsed.stack.join("\n"));
- util.notEnumerableProp(error, "__stackCleaned__", true);
- }
- }
-};
-
-Promise.prototype._warn = function(message) {
- var warning = new Warning(message);
- var ctx = this._peekContext();
- if (ctx) {
- ctx.attachExtraTrace(warning);
- } else {
- var parsed = CapturedTrace.parseStackAndMessage(warning);
- warning.stack = parsed.message + "\n" + parsed.stack.join("\n");
- }
- CapturedTrace.formatAndLogError(warning, "");
-};
-
-Promise.onPossiblyUnhandledRejection = function (fn) {
- var domain = getDomain();
- possiblyUnhandledRejection =
- typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
- : undefined;
-};
-
-Promise.onUnhandledRejectionHandled = function (fn) {
- var domain = getDomain();
- unhandledRejectionHandled =
- typeof fn === "function" ? (domain === null ? fn : domain.bind(fn))
- : undefined;
-};
-
-Promise.longStackTraces = function () {
- if (async.haveItemsQueued() &&
- debugging === false
- ) {
- throw new Error("cannot enable long stack traces after promises have been created\u000a\u000a See http://goo.gl/DT1qyG\u000a");
- }
- debugging = CapturedTrace.isSupported();
- if (debugging) {
- async.disableTrampolineIfNecessary();
- }
-};
-
-Promise.hasLongStackTraces = function () {
- return debugging && CapturedTrace.isSupported();
-};
-
-if (!CapturedTrace.isSupported()) {
- Promise.longStackTraces = function(){};
- debugging = false;
+function PromiseMapSeries(promises, fn) {
+ return PromiseReduce(promises, fn, INTERNAL, INTERNAL);
}
-return function() {
- return debugging;
-};
-};
-
-},{"./async.js":2,"./errors.js":13,"./util.js":38}],11:[function(_dereq_,module,exports){
-"use strict";
-var util = _dereq_("./util.js");
-var isPrimitive = util.isPrimitive;
-
-module.exports = function(Promise) {
-var returner = function () {
- return this;
-};
-var thrower = function () {
- throw this;
-};
-var returnUndefined = function() {};
-var throwUndefined = function() {
- throw undefined;
-};
-
-var wrapper = function (value, action) {
- if (action === 1) {
- return function () {
- throw value;
- };
- } else if (action === 2) {
- return function () {
- return value;
- };
- }
-};
-
-
-Promise.prototype["return"] =
-Promise.prototype.thenReturn = function (value) {
- if (value === undefined) return this.then(returnUndefined);
-
- if (isPrimitive(value)) {
- return this._then(
- wrapper(value, 2),
- undefined,
- undefined,
- undefined,
- undefined
- );
- } else if (value instanceof Promise) {
- value._ignoreRejections();
- }
- return this._then(returner, undefined, undefined, value, undefined);
-};
-
-Promise.prototype["throw"] =
-Promise.prototype.thenThrow = function (reason) {
- if (reason === undefined) return this.then(throwUndefined);
-
- if (isPrimitive(reason)) {
- return this._then(
- wrapper(reason, 1),
- undefined,
- undefined,
- undefined,
- undefined
- );
- }
- return this._then(thrower, undefined, undefined, reason, undefined);
-};
+Promise.prototype.each = function (fn) {
+ return this.mapSeries(fn)
+ ._then(promiseAllThis, undefined, undefined, this, undefined);
};
-},{"./util.js":38}],12:[function(_dereq_,module,exports){
-"use strict";
-module.exports = function(Promise, INTERNAL) {
-var PromiseReduce = Promise.reduce;
-
-Promise.prototype.each = function (fn) {
- return PromiseReduce(this, fn, null, INTERNAL);
+Promise.prototype.mapSeries = function (fn) {
+ return PromiseReduce(this, fn, INTERNAL, INTERNAL);
};
Promise.each = function (promises, fn) {
- return PromiseReduce(promises, fn, null, INTERNAL);
+ return PromiseMapSeries(promises, fn)
+ ._then(promiseAllThis, undefined, undefined, promises, undefined);
};
+
+Promise.mapSeries = PromiseMapSeries;
};
-},{}],13:[function(_dereq_,module,exports){
+},{}],12:[function(_dereq_,module,exports){
"use strict";
-var es5 = _dereq_("./es5.js");
+var es5 = _dereq_("./es5");
var Objectfreeze = es5.freeze;
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var inherits = util.inherits;
var notEnumerableProp = util.notEnumerableProp;
@@ -1501,7 +1754,7 @@ module.exports = {
Warning: Warning
};
-},{"./es5.js":14,"./util.js":38}],14:[function(_dereq_,module,exports){
+},{"./es5":13,"./util":36}],13:[function(_dereq_,module,exports){
var isES5 = (function(){
"use strict";
return this === undefined;
@@ -1583,7 +1836,7 @@ if (isES5) {
};
}
-},{}],15:[function(_dereq_,module,exports){
+},{}],14:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise, INTERNAL) {
var PromiseMap = Promise.map;
@@ -1597,115 +1850,120 @@ Promise.filter = function (promises, fn, options) {
};
};
-},{}],16:[function(_dereq_,module,exports){
+},{}],15:[function(_dereq_,module,exports){
"use strict";
-module.exports = function(Promise, NEXT_FILTER, tryConvertToPromise) {
-var util = _dereq_("./util.js");
-var isPrimitive = util.isPrimitive;
-var thrower = util.thrower;
+module.exports = function(Promise, tryConvertToPromise) {
+var util = _dereq_("./util");
+var CancellationError = Promise.CancellationError;
+var errorObj = util.errorObj;
-function returnThis() {
- return this;
-}
-function throwThis() {
- throw this;
+function FinallyHandlerCancelReaction(finallyHandler) {
+ this.finallyHandler = finallyHandler;
}
-function return$(r) {
- return function() {
- return r;
- };
-}
-function throw$(r) {
- return function() {
- throw r;
- };
-}
-function promisedFinally(ret, reasonOrValue, isFulfilled) {
- var then;
- if (isPrimitive(reasonOrValue)) {
- then = isFulfilled ? return$(reasonOrValue) : throw$(reasonOrValue);
- } else {
- then = isFulfilled ? returnThis : throwThis;
- }
- return ret._then(then, thrower, undefined, reasonOrValue, undefined);
-}
-
-function finallyHandler(reasonOrValue) {
- var promise = this.promise;
- var handler = this.handler;
-
- var ret = promise._isBound()
- ? handler.call(promise._boundValue())
- : handler();
- if (ret !== undefined) {
- var maybePromise = tryConvertToPromise(ret, promise);
- if (maybePromise instanceof Promise) {
- maybePromise = maybePromise._target();
- return promisedFinally(maybePromise, reasonOrValue,
- promise.isFulfilled());
- }
- }
+FinallyHandlerCancelReaction.prototype._resultCancelled = function() {
+ checkCancel(this.finallyHandler);
+};
- if (promise.isRejected()) {
- NEXT_FILTER.e = reasonOrValue;
- return NEXT_FILTER;
- } else {
- return reasonOrValue;
+function checkCancel(ctx, reason) {
+ if (ctx.cancelPromise != null) {
+ if (arguments.length > 1) {
+ ctx.cancelPromise._reject(reason);
+ } else {
+ ctx.cancelPromise._cancel();
+ }
+ ctx.cancelPromise = null;
+ return true;
}
+ return false;
}
-function tapHandler(value) {
+function succeed() {
+ return finallyHandler.call(this, this.promise._target()._settledValue());
+}
+function fail(reason) {
+ if (checkCancel(this, reason)) return;
+ errorObj.e = reason;
+ return errorObj;
+}
+function finallyHandler(reasonOrValue) {
var promise = this.promise;
var handler = this.handler;
- var ret = promise._isBound()
- ? handler.call(promise._boundValue(), value)
- : handler(value);
-
- if (ret !== undefined) {
- var maybePromise = tryConvertToPromise(ret, promise);
- if (maybePromise instanceof Promise) {
- maybePromise = maybePromise._target();
- return promisedFinally(maybePromise, value, true);
+ if (!this.called) {
+ this.called = true;
+ var ret = this.type === 0
+ ? handler.call(promise._boundValue())
+ : handler.call(promise._boundValue(), reasonOrValue);
+ if (ret !== undefined) {
+ promise._setReturnedNonUndefined();
+ var maybePromise = tryConvertToPromise(ret, promise);
+ if (maybePromise instanceof Promise) {
+ if (this.cancelPromise != null) {
+ if (maybePromise.isCancelled()) {
+ var reason =
+ new CancellationError("late cancellation observer");
+ promise._attachExtraTrace(reason);
+ errorObj.e = reason;
+ return errorObj;
+ } else if (maybePromise.isPending()) {
+ maybePromise._attachCancellationCallback(
+ new FinallyHandlerCancelReaction(this));
+ }
+ }
+ return maybePromise._then(
+ succeed, fail, undefined, this, undefined);
+ }
}
}
- return value;
+
+ if (promise.isRejected()) {
+ checkCancel(this);
+ errorObj.e = reasonOrValue;
+ return errorObj;
+ } else {
+ checkCancel(this);
+ return reasonOrValue;
+ }
}
-Promise.prototype._passThroughHandler = function (handler, isFinally) {
+Promise.prototype._passThrough = function(handler, type, success, fail) {
if (typeof handler !== "function") return this.then();
-
- var promiseAndHandler = {
+ return this._then(success, fail, undefined, {
promise: this,
- handler: handler
- };
-
- return this._then(
- isFinally ? finallyHandler : tapHandler,
- isFinally ? finallyHandler : undefined, undefined,
- promiseAndHandler, undefined);
+ handler: handler,
+ called: false,
+ cancelPromise: null,
+ type: type
+ }, undefined);
};
Promise.prototype.lastly =
Promise.prototype["finally"] = function (handler) {
- return this._passThroughHandler(handler, true);
+ return this._passThrough(handler,
+ 0,
+ finallyHandler,
+ finallyHandler);
};
Promise.prototype.tap = function (handler) {
- return this._passThroughHandler(handler, false);
+ return this._passThrough(handler, 1, finallyHandler);
};
+
+return finallyHandler;
};
-},{"./util.js":38}],17:[function(_dereq_,module,exports){
+},{"./util":36}],16:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise,
apiRejection,
INTERNAL,
- tryConvertToPromise) {
-var errors = _dereq_("./errors.js");
+ tryConvertToPromise,
+ Proxyable,
+ debug) {
+var errors = _dereq_("./errors");
var TypeError = errors.TypeError;
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var errorObj = util.errorObj;
var tryCatch = util.tryCatch;
var yieldHandlers = [];
@@ -1730,6 +1988,7 @@ function promiseFromYieldHandler(value, yieldHandlers, traceParent) {
function PromiseSpawn(generatorFunction, receiver, yieldHandler, stack) {
var promise = this._promise = new Promise(INTERNAL);
promise._captureStackTrace();
+ promise._setOnCancel(this);
this._stack = stack;
this._generatorFunction = generatorFunction;
this._receiver = receiver;
@@ -1737,7 +1996,75 @@ function PromiseSpawn(generatorFunction, receiver, yieldHandler, stack) {
this._yieldHandlers = typeof yieldHandler === "function"
? [yieldHandler].concat(yieldHandlers)
: yieldHandlers;
+ this._yieldedPromise = null;
}
+util.inherits(PromiseSpawn, Proxyable);
+
+PromiseSpawn.prototype._isResolved = function() {
+ return this._promise === null;
+};
+
+PromiseSpawn.prototype._cleanup = function() {
+ this._promise = this._generator = null;
+};
+
+PromiseSpawn.prototype._promiseCancelled = function() {
+ if (this._isResolved()) return;
+ var implementsReturn = typeof this._generator["return"] !== "undefined";
+
+ var result;
+ if (!implementsReturn) {
+ var reason = new Promise.CancellationError(
+ "generator .return() sentinel");
+ Promise.coroutine.returnSentinel = reason;
+ this._promise._attachExtraTrace(reason);
+ this._promise._pushContext();
+ result = tryCatch(this._generator["throw"]).call(this._generator,
+ reason);
+ this._promise._popContext();
+ if (result === errorObj && result.e === reason) {
+ result = null;
+ }
+ } else {
+ this._promise._pushContext();
+ result = tryCatch(this._generator["return"]).call(this._generator,
+ undefined);
+ this._promise._popContext();
+ }
+ var promise = this._promise;
+ this._cleanup();
+ if (result === errorObj) {
+ promise._rejectCallback(result.e, false);
+ } else {
+ promise.cancel();
+ }
+};
+
+PromiseSpawn.prototype._promiseFulfilled = function(value) {
+ this._yieldedPromise = null;
+ this._promise._pushContext();
+ var result = tryCatch(this._generator.next).call(this._generator, value);
+ this._promise._popContext();
+ this._continue(result);
+};
+
+PromiseSpawn.prototype._promiseRejected = function(reason) {
+ this._yieldedPromise = null;
+ this._promise._attachExtraTrace(reason);
+ this._promise._pushContext();
+ var result = tryCatch(this._generator["throw"])
+ .call(this._generator, reason);
+ this._promise._popContext();
+ this._continue(result);
+};
+
+PromiseSpawn.prototype._resultCancelled = function() {
+ if (this._yieldedPromise instanceof Promise) {
+ var promise = this._yieldedPromise;
+ this._yieldedPromise = null;
+ promise.cancel();
+ }
+};
PromiseSpawn.prototype.promise = function () {
return this._promise;
@@ -1747,17 +2074,20 @@ PromiseSpawn.prototype._run = function () {
this._generator = this._generatorFunction.call(this._receiver);
this._receiver =
this._generatorFunction = undefined;
- this._next(undefined);
+ this._promiseFulfilled(undefined);
};
PromiseSpawn.prototype._continue = function (result) {
+ var promise = this._promise;
if (result === errorObj) {
- return this._promise._rejectCallback(result.e, false, true);
+ this._cleanup();
+ return promise._rejectCallback(result.e, false);
}
var value = result.value;
if (result.done === true) {
- this._promise._resolveCallback(value);
+ this._cleanup();
+ return promise._resolveCallback(value);
} else {
var maybePromise = tryConvertToPromise(value, this._promise);
if (!(maybePromise instanceof Promise)) {
@@ -1766,9 +2096,9 @@ PromiseSpawn.prototype._continue = function (result) {
this._yieldHandlers,
this._promise);
if (maybePromise === null) {
- this._throw(
+ this._promiseRejected(
new TypeError(
- "A value %s was yielded that could not be treated as a promise\u000a\u000a See http://goo.gl/4Y4pDk\u000a\u000a".replace("%s", value) +
+ "A value %s was yielded that could not be treated as a promise\u000a\u000a See http://goo.gl/MqrFmX\u000a\u000a".replace("%s", value) +
"From coroutine:\u000a" +
this._stack.split("\n").slice(1, -7).join("\n")
)
@@ -1776,35 +2106,25 @@ PromiseSpawn.prototype._continue = function (result) {
return;
}
}
- maybePromise._then(
- this._next,
- this._throw,
- undefined,
- this,
- null
- );
+ maybePromise = maybePromise._target();
+ var bitField = maybePromise._bitField;
+ ;
+ if (((bitField & 50397184) === 0)) {
+ this._yieldedPromise = maybePromise;
+ maybePromise._proxy(this, null);
+ } else if (((bitField & 33554432) !== 0)) {
+ this._promiseFulfilled(maybePromise._value());
+ } else if (((bitField & 16777216) !== 0)) {
+ this._promiseRejected(maybePromise._reason());
+ } else {
+ this._promiseCancelled();
+ }
}
};
-PromiseSpawn.prototype._throw = function (reason) {
- this._promise._attachExtraTrace(reason);
- this._promise._pushContext();
- var result = tryCatch(this._generator["throw"])
- .call(this._generator, reason);
- this._promise._popContext();
- this._continue(result);
-};
-
-PromiseSpawn.prototype._next = function (value) {
- this._promise._pushContext();
- var result = tryCatch(this._generator.next).call(this._generator, value);
- this._promise._popContext();
- this._continue(result);
-};
-
Promise.coroutine = function (generatorFunction, options) {
if (typeof generatorFunction !== "function") {
- throw new TypeError("generatorFunction must be a function\u000a\u000a See http://goo.gl/6Vqhm0\u000a");
+ throw new TypeError("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
var yieldHandler = Object(options).yieldHandler;
var PromiseSpawn$ = PromiseSpawn;
@@ -1813,20 +2133,24 @@ Promise.coroutine = function (generatorFunction, options) {
var generator = generatorFunction.apply(this, arguments);
var spawn = new PromiseSpawn$(undefined, undefined, yieldHandler,
stack);
+ var ret = spawn.promise();
spawn._generator = generator;
- spawn._next(undefined);
- return spawn.promise();
+ spawn._promiseFulfilled(undefined);
+ return ret;
};
};
Promise.coroutine.addYieldHandler = function(fn) {
- if (typeof fn !== "function") throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
+ if (typeof fn !== "function") {
+ throw new TypeError("expecting a function but got " + util.classString(fn));
+ }
yieldHandlers.push(fn);
};
Promise.spawn = function (generatorFunction) {
+ debug.deprecated("Promise.spawn()", "Promise.coroutine()");
if (typeof generatorFunction !== "function") {
- return apiRejection("generatorFunction must be a function\u000a\u000a See http://goo.gl/6Vqhm0\u000a");
+ return apiRejection("generatorFunction must be a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
var spawn = new PromiseSpawn(generatorFunction, this);
var ret = spawn.promise();
@@ -1835,11 +2159,11 @@ Promise.spawn = function (generatorFunction) {
};
};
-},{"./errors.js":13,"./util.js":38}],18:[function(_dereq_,module,exports){
+},{"./errors":12,"./util":36}],17:[function(_dereq_,module,exports){
"use strict";
module.exports =
function(Promise, PromiseArray, tryConvertToPromise, INTERNAL) {
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var canEvaluate = util.canEvaluate;
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
@@ -1855,54 +2179,85 @@ if (canEvaluate) {
".replace(/Index/g, i));
};
- var caller = function(count) {
- var values = [];
- for (var i = 1; i <= count; ++i) values.push("holder.p" + i);
- return new Function("holder", " \n\
+ var promiseSetter = function(i) {
+ return new Function("promise", "holder", " \n\
'use strict'; \n\
- var callback = holder.fn; \n\
- return callback(values); \n\
- ".replace(/values/g, values.join(", ")));
- };
- var thenCallbacks = [];
- var callers = [undefined];
- for (var i = 1; i <= 5; ++i) {
- thenCallbacks.push(thenCallback(i));
- callers.push(caller(i));
- }
-
- var Holder = function(total, fn) {
- this.p1 = this.p2 = this.p3 = this.p4 = this.p5 = null;
- this.fn = fn;
- this.total = total;
- this.now = 0;
+ holder.pIndex = promise; \n\
+ ".replace(/Index/g, i));
};
- Holder.prototype.callers = callers;
- Holder.prototype.checkFulfillment = function(promise) {
- var now = this.now;
- now++;
- var total = this.total;
- if (now >= total) {
- var handler = this.callers[total];
- promise._pushContext();
- var ret = tryCatch(handler)(this);
- promise._popContext();
- if (ret === errorObj) {
- promise._rejectCallback(ret.e, false, true);
- } else {
- promise._resolveCallback(ret);
- }
- } else {
- this.now = now;
+ var generateHolderClass = function(total) {
+ var props = new Array(total);
+ for (var i = 0; i < props.length; ++i) {
+ props[i] = "this.p" + (i+1);
}
+ var assignment = props.join(" = ") + " = null;";
+ var cancellationCode= "var promise;\n" + props.map(function(prop) {
+ return " \n\
+ promise = " + prop + "; \n\
+ if (promise instanceof Promise) { \n\
+ promise.cancel(); \n\
+ } \n\
+ ";
+ }).join("\n");
+ var passedArguments = props.join(", ");
+ var name = "Holder$" + total;
+
+
+ var code = "return function(tryCatch, errorObj, Promise) { \n\
+ 'use strict'; \n\
+ function [TheName](fn) { \n\
+ [TheProperties] \n\
+ this.fn = fn; \n\
+ this.now = 0; \n\
+ } \n\
+ [TheName].prototype.checkFulfillment = function(promise) { \n\
+ var now = ++this.now; \n\
+ if (now === [TheTotal]) { \n\
+ promise._pushContext(); \n\
+ var callback = this.fn; \n\
+ var ret = tryCatch(callback)([ThePassedArguments]); \n\
+ promise._popContext(); \n\
+ if (ret === errorObj) { \n\
+ promise._rejectCallback(ret.e, false); \n\
+ } else { \n\
+ promise._resolveCallback(ret); \n\
+ } \n\
+ } \n\
+ }; \n\
+ \n\
+ [TheName].prototype._resultCancelled = function() { \n\
+ [CancellationCode] \n\
+ }; \n\
+ \n\
+ return [TheName]; \n\
+ }(tryCatch, errorObj, Promise); \n\
+ ";
+
+ code = code.replace(/\[TheName\]/g, name)
+ .replace(/\[TheTotal\]/g, total)
+ .replace(/\[ThePassedArguments\]/g, passedArguments)
+ .replace(/\[TheProperties\]/g, assignment)
+ .replace(/\[CancellationCode\]/g, cancellationCode);
+
+ return new Function("tryCatch", "errorObj", "Promise", code)
+ (tryCatch, errorObj, Promise);
};
- var reject = function (reason) {
+ var holderClasses = [];
+ var thenCallbacks = [];
+ var promiseSetters = [];
+
+ for (var i = 0; i < 8; ++i) {
+ holderClasses.push(generateHolderClass(i + 1));
+ thenCallbacks.push(thenCallback(i + 1));
+ promiseSetters.push(promiseSetter(i + 1));
+ }
+
+ reject = function (reason) {
this._reject(reason);
};
-}
-}
+}}
Promise.join = function () {
var last = arguments.length - 1;
@@ -1910,33 +2265,44 @@ Promise.join = function () {
if (last > 0 && typeof arguments[last] === "function") {
fn = arguments[last];
if (!true) {
- if (last < 6 && canEvaluate) {
+ if (last <= 8 && canEvaluate) {
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
- var holder = new Holder(last, fn);
+ var HolderClass = holderClasses[last - 1];
+ var holder = new HolderClass(fn);
var callbacks = thenCallbacks;
+
for (var i = 0; i < last; ++i) {
var maybePromise = tryConvertToPromise(arguments[i], ret);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
- if (maybePromise._isPending()) {
+ var bitField = maybePromise._bitField;
+ ;
+ if (((bitField & 50397184) === 0)) {
maybePromise._then(callbacks[i], reject,
undefined, ret, holder);
- } else if (maybePromise._isFulfilled()) {
+ promiseSetters[i](maybePromise, holder);
+ } else if (((bitField & 33554432) !== 0)) {
callbacks[i].call(ret,
maybePromise._value(), holder);
- } else {
+ } else if (((bitField & 16777216) !== 0)) {
ret._reject(maybePromise._reason());
+ } else {
+ ret._cancel();
}
} else {
callbacks[i].call(ret, maybePromise, holder);
}
}
+ if (!ret._isFateSealed()) {
+ ret._setAsyncGuaranteed();
+ ret._setOnCancel(holder);
+ }
return ret;
}
}
}
- var $_len = arguments.length;var args = new Array($_len); for(var $_i = 0; $_i < $_len; ++$_i) {args[$_i] = arguments[$_i];}
+ var args = [].slice.call(arguments);;
if (fn) args.pop();
var ret = new PromiseArray(args).promise();
return fn !== undefined ? ret.spread(fn) : ret;
@@ -1944,19 +2310,18 @@ Promise.join = function () {
};
-},{"./util.js":38}],19:[function(_dereq_,module,exports){
+},{"./util":36}],18:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise,
PromiseArray,
apiRejection,
tryConvertToPromise,
- INTERNAL) {
+ INTERNAL,
+ debug) {
var getDomain = Promise._getDomain;
-var async = _dereq_("./async.js");
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
-var PENDING = {};
var EMPTY_ARRAY = [];
function MappingPromiseArray(promises, fn, limit, _filter) {
@@ -1970,10 +2335,9 @@ function MappingPromiseArray(promises, fn, limit, _filter) {
this._limit = limit;
this._inFlight = 0;
this._queue = limit >= 1 ? [] : EMPTY_ARRAY;
- async.invoke(init, this, undefined);
+ this._init$(undefined, -2);
}
util.inherits(MappingPromiseArray, PromiseArray);
-function init() {this._init$(undefined, -2);}
MappingPromiseArray.prototype._init = function () {};
@@ -1982,39 +2346,58 @@ MappingPromiseArray.prototype._promiseFulfilled = function (value, index) {
var length = this.length();
var preservedValues = this._preservedValues;
var limit = this._limit;
- if (values[index] === PENDING) {
+
+ if (index < 0) {
+ index = (index * -1) - 1;
values[index] = value;
if (limit >= 1) {
this._inFlight--;
this._drainQueue();
- if (this._isResolved()) return;
+ if (this._isResolved()) return true;
}
} else {
if (limit >= 1 && this._inFlight >= limit) {
values[index] = value;
this._queue.push(index);
- return;
+ return false;
}
if (preservedValues !== null) preservedValues[index] = value;
+ var promise = this._promise;
var callback = this._callback;
- var receiver = this._promise._boundValue();
- this._promise._pushContext();
+ var receiver = promise._boundValue();
+ promise._pushContext();
var ret = tryCatch(callback).call(receiver, value, index, length);
- this._promise._popContext();
- if (ret === errorObj) return this._reject(ret.e);
+ var promiseCreated = promise._popContext();
+ debug.checkForgottenReturns(
+ ret,
+ promiseCreated,
+ preservedValues !== null ? "Promise.filter" : "Promise.map",
+ promise
+ );
+ if (ret === errorObj) {
+ this._reject(ret.e);
+ return true;
+ }
var maybePromise = tryConvertToPromise(ret, this._promise);
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
- if (maybePromise._isPending()) {
+ var bitField = maybePromise._bitField;
+ ;
+ if (((bitField & 50397184) === 0)) {
if (limit >= 1) this._inFlight++;
- values[index] = PENDING;
- return maybePromise._proxyPromiseArray(this, index);
- } else if (maybePromise._isFulfilled()) {
+ values[index] = maybePromise;
+ maybePromise._proxy(this, (index + 1) * -1);
+ return false;
+ } else if (((bitField & 33554432) !== 0)) {
ret = maybePromise._value();
+ } else if (((bitField & 16777216) !== 0)) {
+ this._reject(maybePromise._reason());
+ return true;
} else {
- return this._reject(maybePromise._reason());
+ this._cancel();
+ return true;
}
}
values[index] = ret;
@@ -2026,8 +2409,9 @@ MappingPromiseArray.prototype._promiseFulfilled = function (value, index) {
} else {
this._resolve(values);
}
-
+ return true;
}
+ return false;
};
MappingPromiseArray.prototype._drainQueue = function () {
@@ -2057,79 +2441,143 @@ MappingPromiseArray.prototype.preservedValues = function () {
};
function map(promises, fn, options, _filter) {
+ if (typeof fn !== "function") {
+ return apiRejection("expecting a function but got " + util.classString(fn));
+ }
var limit = typeof options === "object" && options !== null
? options.concurrency
: 0;
limit = typeof limit === "number" &&
isFinite(limit) && limit >= 1 ? limit : 0;
- return new MappingPromiseArray(promises, fn, limit, _filter);
+ return new MappingPromiseArray(promises, fn, limit, _filter).promise();
}
Promise.prototype.map = function (fn, options) {
- if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
-
- return map(this, fn, options, null).promise();
+ return map(this, fn, options, null);
};
Promise.map = function (promises, fn, options, _filter) {
- if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
- return map(promises, fn, options, _filter).promise();
+ return map(promises, fn, options, _filter);
};
};
-},{"./async.js":2,"./util.js":38}],20:[function(_dereq_,module,exports){
+},{"./util":36}],19:[function(_dereq_,module,exports){
"use strict";
module.exports =
-function(Promise, INTERNAL, tryConvertToPromise, apiRejection) {
-var util = _dereq_("./util.js");
+function(Promise, INTERNAL, tryConvertToPromise, apiRejection, debug) {
+var util = _dereq_("./util");
var tryCatch = util.tryCatch;
Promise.method = function (fn) {
if (typeof fn !== "function") {
- throw new Promise.TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
+ throw new Promise.TypeError("expecting a function but got " + util.classString(fn));
}
return function () {
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
ret._pushContext();
var value = tryCatch(fn).apply(this, arguments);
- ret._popContext();
+ var promiseCreated = ret._popContext();
+ debug.checkForgottenReturns(
+ value, promiseCreated, "Promise.method", ret);
ret._resolveFromSyncValue(value);
return ret;
};
};
-Promise.attempt = Promise["try"] = function (fn, args, ctx) {
+Promise.attempt = Promise["try"] = function (fn) {
if (typeof fn !== "function") {
- return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
+ return apiRejection("expecting a function but got " + util.classString(fn));
}
var ret = new Promise(INTERNAL);
ret._captureStackTrace();
ret._pushContext();
- var value = util.isArray(args)
- ? tryCatch(fn).apply(ctx, args)
- : tryCatch(fn).call(ctx, args);
- ret._popContext();
+ var value;
+ if (arguments.length > 1) {
+ debug.deprecated("calling Promise.try with more than 1 argument");
+ var arg = arguments[1];
+ var ctx = arguments[2];
+ value = util.isArray(arg) ? tryCatch(fn).apply(ctx, arg)
+ : tryCatch(fn).call(ctx, arg);
+ } else {
+ value = tryCatch(fn)();
+ }
+ var promiseCreated = ret._popContext();
+ debug.checkForgottenReturns(
+ value, promiseCreated, "Promise.try", ret);
ret._resolveFromSyncValue(value);
return ret;
};
Promise.prototype._resolveFromSyncValue = function (value) {
if (value === util.errorObj) {
- this._rejectCallback(value.e, false, true);
+ this._rejectCallback(value.e, false);
} else {
this._resolveCallback(value, true);
}
};
};
-},{"./util.js":38}],21:[function(_dereq_,module,exports){
+},{"./util":36}],20:[function(_dereq_,module,exports){
+"use strict";
+var util = _dereq_("./util");
+var maybeWrapAsError = util.maybeWrapAsError;
+var errors = _dereq_("./errors");
+var OperationalError = errors.OperationalError;
+var es5 = _dereq_("./es5");
+
+function isUntypedError(obj) {
+ return obj instanceof Error &&
+ es5.getPrototypeOf(obj) === Error.prototype;
+}
+
+var rErrorKey = /^(?:name|message|stack|cause)$/;
+function wrapAsOperationalError(obj) {
+ var ret;
+ if (isUntypedError(obj)) {
+ ret = new OperationalError(obj);
+ ret.name = obj.name;
+ ret.message = obj.message;
+ ret.stack = obj.stack;
+ var keys = es5.keys(obj);
+ for (var i = 0; i < keys.length; ++i) {
+ var key = keys[i];
+ if (!rErrorKey.test(key)) {
+ ret[key] = obj[key];
+ }
+ }
+ return ret;
+ }
+ util.markAsOriginatingFromRejection(obj);
+ return obj;
+}
+
+function nodebackForPromise(promise, multiArgs) {
+ return function(err, value) {
+ if (promise === null) return;
+ if (err) {
+ var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
+ promise._attachExtraTrace(wrapped);
+ promise._reject(wrapped);
+ } else if (!multiArgs) {
+ promise._fulfill(value);
+ } else {
+ var args = [].slice.call(arguments, 1);;
+ promise._fulfill(args);
+ }
+ promise = null;
+ };
+}
+
+module.exports = nodebackForPromise;
+
+},{"./errors":12,"./es5":13,"./util":36}],21:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise) {
-var util = _dereq_("./util.js");
-var async = _dereq_("./async.js");
+var util = _dereq_("./util");
+var async = Promise._async;
var tryCatch = util.tryCatch;
var errorObj = util.errorObj;
@@ -2156,8 +2604,7 @@ function successAdapter(val, nodeback) {
function errorAdapter(reason, nodeback) {
var promise = this;
if (!reason) {
- var target = promise._target();
- var newReason = target._getCarriedStackTrace();
+ var newReason = new Error(reason + "");
newReason.cause = reason;
reason = newReason;
}
@@ -2167,8 +2614,8 @@ function errorAdapter(reason, nodeback) {
}
}
-Promise.prototype.asCallback =
-Promise.prototype.nodeify = function (nodeback, options) {
+Promise.prototype.asCallback = Promise.prototype.nodeify = function (nodeback,
+ options) {
if (typeof nodeback == "function") {
var adapter = successAdapter;
if (options !== undefined && Object(options).spread) {
@@ -2186,98 +2633,21 @@ Promise.prototype.nodeify = function (nodeback, options) {
};
};
-},{"./async.js":2,"./util.js":38}],22:[function(_dereq_,module,exports){
-"use strict";
-module.exports = function(Promise, PromiseArray) {
-var util = _dereq_("./util.js");
-var async = _dereq_("./async.js");
-var tryCatch = util.tryCatch;
-var errorObj = util.errorObj;
-
-Promise.prototype.progressed = function (handler) {
- return this._then(undefined, undefined, handler, undefined, undefined);
-};
-
-Promise.prototype._progress = function (progressValue) {
- if (this._isFollowingOrFulfilledOrRejected()) return;
- this._target()._progressUnchecked(progressValue);
-
-};
-
-Promise.prototype._progressHandlerAt = function (index) {
- return index === 0
- ? this._progressHandler0
- : this[(index << 2) + index - 5 + 2];
-};
-
-Promise.prototype._doProgressWith = function (progression) {
- var progressValue = progression.value;
- var handler = progression.handler;
- var promise = progression.promise;
- var receiver = progression.receiver;
-
- var ret = tryCatch(handler).call(receiver, progressValue);
- if (ret === errorObj) {
- if (ret.e != null &&
- ret.e.name !== "StopProgressPropagation") {
- var trace = util.canAttachTrace(ret.e)
- ? ret.e : new Error(util.toString(ret.e));
- promise._attachExtraTrace(trace);
- promise._progress(ret.e);
- }
- } else if (ret instanceof Promise) {
- ret._then(promise._progress, null, null, promise, undefined);
- } else {
- promise._progress(ret);
- }
-};
-
-
-Promise.prototype._progressUnchecked = function (progressValue) {
- var len = this._length();
- var progress = this._progress;
- for (var i = 0; i < len; i++) {
- var handler = this._progressHandlerAt(i);
- var promise = this._promiseAt(i);
- if (!(promise instanceof Promise)) {
- var receiver = this._receiverAt(i);
- if (typeof handler === "function") {
- handler.call(receiver, progressValue, promise);
- } else if (receiver instanceof PromiseArray &&
- !receiver._isResolved()) {
- receiver._promiseProgressed(progressValue, promise);
- }
- continue;
- }
-
- if (typeof handler === "function") {
- async.invoke(this._doProgressWith, this, {
- handler: handler,
- promise: promise,
- receiver: this._receiverAt(i),
- value: progressValue
- });
- } else {
- async.invoke(progress, promise, progressValue);
- }
- }
-};
-};
-
-},{"./async.js":2,"./util.js":38}],23:[function(_dereq_,module,exports){
+},{"./util":36}],22:[function(_dereq_,module,exports){
"use strict";
module.exports = function() {
var makeSelfResolutionError = function () {
- return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/LhFpo0\u000a");
+ return new TypeError("circular promise resolution chain\u000a\u000a See http://goo.gl/MqrFmX\u000a");
};
-var reflect = function() {
+var reflectHandler = function() {
return new Promise.PromiseInspection(this._target());
};
var apiRejection = function(msg) {
return Promise.reject(new TypeError(msg));
};
-
-var util = _dereq_("./util.js");
+function Proxyable() {}
+var UNDEFINED_BINDING = {};
+var util = _dereq_("./util");
var getDomain;
if (util.isNode) {
@@ -2293,48 +2663,55 @@ if (util.isNode) {
}
util.notEnumerableProp(Promise, "_getDomain", getDomain);
-var UNDEFINED_BINDING = {};
-var async = _dereq_("./async.js");
-var errors = _dereq_("./errors.js");
+var es5 = _dereq_("./es5");
+var Async = _dereq_("./async");
+var async = new Async();
+es5.defineProperty(Promise, "_async", {value: async});
+var errors = _dereq_("./errors");
var TypeError = Promise.TypeError = errors.TypeError;
Promise.RangeError = errors.RangeError;
-Promise.CancellationError = errors.CancellationError;
+var CancellationError = Promise.CancellationError = errors.CancellationError;
Promise.TimeoutError = errors.TimeoutError;
Promise.OperationalError = errors.OperationalError;
Promise.RejectionError = errors.OperationalError;
Promise.AggregateError = errors.AggregateError;
var INTERNAL = function(){};
var APPLY = {};
-var NEXT_FILTER = {e: null};
-var tryConvertToPromise = _dereq_("./thenables.js")(Promise, INTERNAL);
+var NEXT_FILTER = {};
+var tryConvertToPromise = _dereq_("./thenables")(Promise, INTERNAL);
var PromiseArray =
- _dereq_("./promise_array.js")(Promise, INTERNAL,
- tryConvertToPromise, apiRejection);
-var CapturedTrace = _dereq_("./captured_trace.js")();
-var isDebugging = _dereq_("./debuggability.js")(Promise, CapturedTrace);
+ _dereq_("./promise_array")(Promise, INTERNAL,
+ tryConvertToPromise, apiRejection, Proxyable);
+var Context = _dereq_("./context")(Promise);
/*jshint unused:false*/
-var createContext =
- _dereq_("./context.js")(Promise, CapturedTrace, isDebugging);
-var CatchFilter = _dereq_("./catch_filter.js")(NEXT_FILTER);
-var PromiseResolver = _dereq_("./promise_resolver.js");
-var nodebackForPromise = PromiseResolver._nodebackForPromise;
+var createContext = Context.create;
+var debug = _dereq_("./debuggability")(Promise, Context);
+var CapturedTrace = debug.CapturedTrace;
+var finallyHandler = _dereq_("./finally")(Promise, tryConvertToPromise);
+var catchFilter = _dereq_("./catch_filter")(NEXT_FILTER);
+var nodebackForPromise = _dereq_("./nodeback");
var errorObj = util.errorObj;
var tryCatch = util.tryCatch;
-function Promise(resolver) {
- if (typeof resolver !== "function") {
- throw new TypeError("the promise constructor requires a resolver function\u000a\u000a See http://goo.gl/EC22Yn\u000a");
+function check(self, executor) {
+ if (typeof executor !== "function") {
+ throw new TypeError("expecting a function but got " + util.classString(executor));
}
- if (this.constructor !== Promise) {
- throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/KsIlge\u000a");
+ if (self.constructor !== Promise) {
+ throw new TypeError("the promise constructor cannot be invoked directly\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
+}
+
+function Promise(executor) {
this._bitField = 0;
this._fulfillmentHandler0 = undefined;
this._rejectionHandler0 = undefined;
- this._progressHandler0 = undefined;
this._promise0 = undefined;
this._receiver0 = undefined;
- this._settledValue = undefined;
- if (resolver !== INTERNAL) this._resolveFromResolver(resolver);
+ if (executor !== INTERNAL) {
+ check(this, executor);
+ this._resolveFromExecutor(executor);
+ }
+ this._promiseCreated();
}
Promise.prototype.toString = function () {
@@ -2348,28 +2725,26 @@ Promise.prototype.caught = Promise.prototype["catch"] = function (fn) {
j = 0, i;
for (i = 0; i < len - 1; ++i) {
var item = arguments[i];
- if (typeof item === "function") {
+ if (util.isObject(item)) {
catchInstances[j++] = item;
} else {
- return Promise.reject(
- new TypeError("Catch filter must inherit from Error or be a simple predicate function\u000a\u000a See http://goo.gl/o84o68\u000a"));
+ return apiRejection("expecting an object but got " + util.classString(item));
}
}
catchInstances.length = j;
fn = arguments[i];
- var catchFilter = new CatchFilter(catchInstances, fn, this);
- return this._then(undefined, catchFilter.doFilter, undefined,
- catchFilter, undefined);
+ return this.then(undefined, catchFilter(catchInstances, fn, this));
}
- return this._then(undefined, fn, undefined, undefined, undefined);
+ return this.then(undefined, fn);
};
Promise.prototype.reflect = function () {
- return this._then(reflect, reflect, undefined, this, undefined);
+ return this._then(reflectHandler,
+ reflectHandler, undefined, this, undefined);
};
-Promise.prototype.then = function (didFulfill, didReject, didProgress) {
- if (isDebugging() && arguments.length > 0 &&
+Promise.prototype.then = function (didFulfill, didReject) {
+ if (debug.warnings() && arguments.length > 0 &&
typeof didFulfill !== "function" &&
typeof didReject !== "function") {
var msg = ".then() only accepts functions but was passed: " +
@@ -2379,23 +2754,20 @@ Promise.prototype.then = function (didFulfill, didReject, didProgress) {
}
this._warn(msg);
}
- return this._then(didFulfill, didReject, didProgress,
- undefined, undefined);
+ return this._then(didFulfill, didReject, undefined, undefined, undefined);
};
-Promise.prototype.done = function (didFulfill, didReject, didProgress) {
- var promise = this._then(didFulfill, didReject, didProgress,
- undefined, undefined);
+Promise.prototype.done = function (didFulfill, didReject) {
+ var promise =
+ this._then(didFulfill, didReject, undefined, undefined, undefined);
promise._setIsFinal();
};
-Promise.prototype.spread = function (didFulfill, didReject) {
- return this.all()._then(didFulfill, didReject, undefined, APPLY, undefined);
-};
-
-Promise.prototype.isCancellable = function () {
- return !this.isResolved() &&
- this._cancellable();
+Promise.prototype.spread = function (fn) {
+ if (typeof fn !== "function") {
+ return apiRejection("expecting a function but got " + util.classString(fn));
+ }
+ return this.all()._then(fn, undefined, undefined, APPLY, undefined);
};
Promise.prototype.toJSON = function () {
@@ -2416,6 +2788,9 @@ Promise.prototype.toJSON = function () {
};
Promise.prototype.all = function () {
+ if (arguments.length > 0) {
+ this._warn(".all() was passed arguments but it does not take any");
+ }
return new PromiseArray(this).promise();
};
@@ -2427,12 +2802,15 @@ Promise.is = function (val) {
return val instanceof Promise;
};
-Promise.fromNode = function(fn) {
+Promise.fromNode = Promise.fromCallback = function(fn) {
var ret = new Promise(INTERNAL);
- var result = tryCatch(fn)(nodebackForPromise(ret));
+ var multiArgs = arguments.length > 1 ? !!Object(arguments[1]).multiArgs
+ : false;
+ var result = tryCatch(fn)(nodebackForPromise(ret, multiArgs));
if (result === errorObj) {
- ret._rejectCallback(result.e, true, true);
+ ret._rejectCallback(result.e, true);
}
+ if (!ret._isFateSealed()) ret._setAsyncGuaranteed();
return ret;
};
@@ -2440,17 +2818,13 @@ Promise.all = function (promises) {
return new PromiseArray(promises).promise();
};
-Promise.defer = Promise.pending = function () {
- var promise = new Promise(INTERNAL);
- return new PromiseResolver(promise);
-};
-
Promise.cast = function (obj) {
var ret = tryConvertToPromise(obj);
if (!(ret instanceof Promise)) {
- var val = ret;
ret = new Promise(INTERNAL);
- ret._fulfillUnchecked(val);
+ ret._captureStackTrace();
+ ret._setFulfilled();
+ ret._rejectionHandler0 = obj;
}
return ret;
};
@@ -2465,7 +2839,9 @@ Promise.reject = Promise.rejected = function (reason) {
};
Promise.setScheduler = function(fn) {
- if (typeof fn !== "function") throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
+ if (typeof fn !== "function") {
+ throw new TypeError("expecting a function but got " + util.classString(fn));
+ }
var prev = async._schedule;
async._schedule = fn;
return prev;
@@ -2474,110 +2850,110 @@ Promise.setScheduler = function(fn) {
Promise.prototype._then = function (
didFulfill,
didReject,
- didProgress,
- receiver,
+ _, receiver,
internalData
) {
var haveInternalData = internalData !== undefined;
- var ret = haveInternalData ? internalData : new Promise(INTERNAL);
+ var promise = haveInternalData ? internalData : new Promise(INTERNAL);
+ var target = this._target();
+ var bitField = target._bitField;
if (!haveInternalData) {
- ret._propagateFrom(this, 4 | 1);
- ret._captureStackTrace();
- }
-
- var target = this._target();
- if (target !== this) {
- if (receiver === undefined) receiver = this._boundTo;
- if (!haveInternalData) ret._setIsMigrated();
+ promise._propagateFrom(this, 3);
+ promise._captureStackTrace();
+ if (receiver === undefined &&
+ ((this._bitField & 2097152) !== 0)) {
+ if (!((bitField & 50397184) === 0)) {
+ receiver = this._boundValue();
+ } else {
+ receiver = target === this ? undefined : this._boundTo;
+ }
+ }
}
- var callbackIndex = target._addCallbacks(didFulfill,
- didReject,
- didProgress,
- ret,
- receiver,
- getDomain());
+ var domain = getDomain();
+ if (!((bitField & 50397184) === 0)) {
+ var handler, value, settler = target._settlePromiseCtx;
+ if (((bitField & 33554432) !== 0)) {
+ value = target._rejectionHandler0;
+ handler = didFulfill;
+ } else if (((bitField & 16777216) !== 0)) {
+ value = target._fulfillmentHandler0;
+ handler = didReject;
+ target._unsetRejectionIsUnhandled();
+ } else {
+ settler = target._settlePromiseLateCancellationObserver;
+ value = new CancellationError("late cancellation observer");
+ target._attachExtraTrace(value);
+ handler = didReject;
+ }
- if (target._isResolved() && !target._isSettlePromisesQueued()) {
- async.invoke(
- target._settlePromiseAtPostResolution, target, callbackIndex);
+ async.invoke(settler, target, {
+ handler: domain === null ? handler
+ : (typeof handler === "function" && domain.bind(handler)),
+ promise: promise,
+ receiver: receiver,
+ value: value
+ });
+ } else {
+ target._addCallbacks(didFulfill, didReject, promise, receiver, domain);
}
- return ret;
-};
-
-Promise.prototype._settlePromiseAtPostResolution = function (index) {
- if (this._isRejectionUnhandled()) this._unsetRejectionIsUnhandled();
- this._settlePromiseAt(index);
+ return promise;
};
Promise.prototype._length = function () {
- return this._bitField & 131071;
+ return this._bitField & 65535;
};
-Promise.prototype._isFollowingOrFulfilledOrRejected = function () {
- return (this._bitField & 939524096) > 0;
+Promise.prototype._isFateSealed = function () {
+ return (this._bitField & 117506048) !== 0;
};
Promise.prototype._isFollowing = function () {
- return (this._bitField & 536870912) === 536870912;
+ return (this._bitField & 67108864) === 67108864;
};
Promise.prototype._setLength = function (len) {
- this._bitField = (this._bitField & -131072) |
- (len & 131071);
+ this._bitField = (this._bitField & -65536) |
+ (len & 65535);
};
Promise.prototype._setFulfilled = function () {
- this._bitField = this._bitField | 268435456;
-};
-
-Promise.prototype._setRejected = function () {
- this._bitField = this._bitField | 134217728;
-};
-
-Promise.prototype._setFollowing = function () {
- this._bitField = this._bitField | 536870912;
-};
-
-Promise.prototype._setIsFinal = function () {
this._bitField = this._bitField | 33554432;
};
-Promise.prototype._isFinal = function () {
- return (this._bitField & 33554432) > 0;
-};
-
-Promise.prototype._cancellable = function () {
- return (this._bitField & 67108864) > 0;
+Promise.prototype._setRejected = function () {
+ this._bitField = this._bitField | 16777216;
};
-Promise.prototype._setCancellable = function () {
+Promise.prototype._setFollowing = function () {
this._bitField = this._bitField | 67108864;
};
-Promise.prototype._unsetCancellable = function () {
- this._bitField = this._bitField & (~67108864);
+Promise.prototype._setIsFinal = function () {
+ this._bitField = this._bitField | 4194304;
};
-Promise.prototype._setIsMigrated = function () {
- this._bitField = this._bitField | 4194304;
+Promise.prototype._isFinal = function () {
+ return (this._bitField & 4194304) > 0;
};
-Promise.prototype._unsetIsMigrated = function () {
- this._bitField = this._bitField & (~4194304);
+Promise.prototype._unsetCancelled = function() {
+ this._bitField = this._bitField & (~65536);
};
-Promise.prototype._isMigrated = function () {
- return (this._bitField & 4194304) > 0;
+Promise.prototype._setCancelled = function() {
+ this._bitField = this._bitField | 65536;
+};
+
+Promise.prototype._setAsyncGuaranteed = function() {
+ this._bitField = this._bitField | 134217728;
};
Promise.prototype._receiverAt = function (index) {
- var ret = index === 0
- ? this._receiver0
- : this[
- index * 5 - 5 + 4];
+ var ret = index === 0 ? this._receiver0 : this[
+ index * 4 - 4 + 3];
if (ret === UNDEFINED_BINDING) {
return undefined;
} else if (ret === undefined && this._isBound()) {
@@ -2587,67 +2963,59 @@ Promise.prototype._receiverAt = function (index) {
};
Promise.prototype._promiseAt = function (index) {
- return index === 0
- ? this._promise0
- : this[index * 5 - 5 + 3];
+ return this[
+ index * 4 - 4 + 2];
};
Promise.prototype._fulfillmentHandlerAt = function (index) {
- return index === 0
- ? this._fulfillmentHandler0
- : this[index * 5 - 5 + 0];
+ return this[
+ index * 4 - 4 + 0];
};
Promise.prototype._rejectionHandlerAt = function (index) {
- return index === 0
- ? this._rejectionHandler0
- : this[index * 5 - 5 + 1];
+ return this[
+ index * 4 - 4 + 1];
};
-Promise.prototype._boundValue = function() {
- var ret = this._boundTo;
- if (ret !== undefined) {
- if (ret instanceof Promise) {
- if (ret.isFulfilled()) {
- return ret.value();
- } else {
- return undefined;
- }
- }
- }
- return ret;
+Promise.prototype._boundValue = function() {};
+
+Promise.prototype._migrateCallback0 = function (follower) {
+ var bitField = follower._bitField;
+ var fulfill = follower._fulfillmentHandler0;
+ var reject = follower._rejectionHandler0;
+ var promise = follower._promise0;
+ var receiver = follower._receiverAt(0);
+ if (receiver === undefined) receiver = UNDEFINED_BINDING;
+ this._addCallbacks(fulfill, reject, promise, receiver, null);
};
-Promise.prototype._migrateCallbacks = function (follower, index) {
+Promise.prototype._migrateCallbackAt = function (follower, index) {
var fulfill = follower._fulfillmentHandlerAt(index);
var reject = follower._rejectionHandlerAt(index);
- var progress = follower._progressHandlerAt(index);
var promise = follower._promiseAt(index);
var receiver = follower._receiverAt(index);
- if (promise instanceof Promise) promise._setIsMigrated();
if (receiver === undefined) receiver = UNDEFINED_BINDING;
- this._addCallbacks(fulfill, reject, progress, promise, receiver, null);
+ this._addCallbacks(fulfill, reject, promise, receiver, null);
};
Promise.prototype._addCallbacks = function (
fulfill,
reject,
- progress,
promise,
receiver,
domain
) {
var index = this._length();
- if (index >= 131071 - 5) {
+ if (index >= 65535 - 4) {
index = 0;
this._setLength(0);
}
if (index === 0) {
this._promise0 = promise;
- if (receiver !== undefined) this._receiver0 = receiver;
- if (typeof fulfill === "function" && !this._isCarryingStackTrace()) {
+ this._receiver0 = receiver;
+ if (typeof fulfill === "function") {
this._fulfillmentHandler0 =
domain === null ? fulfill : domain.bind(fulfill);
}
@@ -2655,14 +3023,10 @@ Promise.prototype._addCallbacks = function (
this._rejectionHandler0 =
domain === null ? reject : domain.bind(reject);
}
- if (typeof progress === "function") {
- this._progressHandler0 =
- domain === null ? progress : domain.bind(progress);
- }
} else {
- var base = index * 5 - 5;
- this[base + 3] = promise;
- this[base + 4] = receiver;
+ var base = index * 4 - 4;
+ this[base + 2] = promise;
+ this[base + 3] = receiver;
if (typeof fulfill === "function") {
this[base + 0] =
domain === null ? fulfill : domain.bind(fulfill);
@@ -2671,114 +3035,106 @@ Promise.prototype._addCallbacks = function (
this[base + 1] =
domain === null ? reject : domain.bind(reject);
}
- if (typeof progress === "function") {
- this[base + 2] =
- domain === null ? progress : domain.bind(progress);
- }
}
this._setLength(index + 1);
return index;
};
-Promise.prototype._setProxyHandlers = function (receiver, promiseSlotValue) {
- var index = this._length();
-
- if (index >= 131071 - 5) {
- index = 0;
- this._setLength(0);
- }
- if (index === 0) {
- this._promise0 = promiseSlotValue;
- this._receiver0 = receiver;
- } else {
- var base = index * 5 - 5;
- this[base + 3] = promiseSlotValue;
- this[base + 4] = receiver;
- }
- this._setLength(index + 1);
-};
-
-Promise.prototype._proxyPromiseArray = function (promiseArray, index) {
- this._setProxyHandlers(promiseArray, index);
+Promise.prototype._proxy = function (proxyable, arg) {
+ this._addCallbacks(undefined, undefined, arg, proxyable, null);
};
Promise.prototype._resolveCallback = function(value, shouldBind) {
- if (this._isFollowingOrFulfilledOrRejected()) return;
+ if (((this._bitField & 117506048) !== 0)) return;
if (value === this)
- return this._rejectCallback(makeSelfResolutionError(), false, true);
+ return this._rejectCallback(makeSelfResolutionError(), false);
var maybePromise = tryConvertToPromise(value, this);
if (!(maybePromise instanceof Promise)) return this._fulfill(value);
- var propagationFlags = 1 | (shouldBind ? 4 : 0);
- this._propagateFrom(maybePromise, propagationFlags);
+ if (shouldBind) this._propagateFrom(maybePromise, 2);
+
var promise = maybePromise._target();
- if (promise._isPending()) {
+ var bitField = promise._bitField;
+ if (((bitField & 50397184) === 0)) {
var len = this._length();
- for (var i = 0; i < len; ++i) {
- promise._migrateCallbacks(this, i);
+ if (len > 0) promise._migrateCallback0(this);
+ for (var i = 1; i < len; ++i) {
+ promise._migrateCallbackAt(this, i);
}
this._setFollowing();
this._setLength(0);
this._setFollowee(promise);
- } else if (promise._isFulfilled()) {
- this._fulfillUnchecked(promise._value());
+ } else if (((bitField & 33554432) !== 0)) {
+ this._fulfill(promise._value());
+ } else if (((bitField & 16777216) !== 0)) {
+ this._reject(promise._reason());
} else {
- this._rejectUnchecked(promise._reason(),
- promise._getCarriedStackTrace());
+ var reason = new CancellationError("late cancellation observer");
+ promise._attachExtraTrace(reason);
+ this._reject(reason);
}
};
Promise.prototype._rejectCallback =
-function(reason, synchronous, shouldNotMarkOriginatingFromRejection) {
- if (!shouldNotMarkOriginatingFromRejection) {
- util.markAsOriginatingFromRejection(reason);
- }
+function(reason, synchronous, ignoreNonErrorWarnings) {
var trace = util.ensureErrorObject(reason);
var hasStack = trace === reason;
+ if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
+ var message = "a promise was rejected with a non-error: " +
+ util.classString(reason);
+ this._warn(message, true);
+ }
this._attachExtraTrace(trace, synchronous ? hasStack : false);
- this._reject(reason, hasStack ? undefined : trace);
+ this._reject(reason);
};
-Promise.prototype._resolveFromResolver = function (resolver) {
+Promise.prototype._resolveFromExecutor = function (executor) {
var promise = this;
this._captureStackTrace();
this._pushContext();
var synchronous = true;
- var r = tryCatch(resolver)(function(value) {
- if (promise === null) return;
+ var r = this._execute(executor, function(value) {
promise._resolveCallback(value);
- promise = null;
}, function (reason) {
- if (promise === null) return;
promise._rejectCallback(reason, synchronous);
- promise = null;
});
synchronous = false;
this._popContext();
- if (r !== undefined && r === errorObj && promise !== null) {
- promise._rejectCallback(r.e, true, true);
- promise = null;
+ if (r !== undefined) {
+ promise._rejectCallback(r, true);
}
};
Promise.prototype._settlePromiseFromHandler = function (
handler, receiver, value, promise
) {
- if (promise._isRejected()) return;
+ var bitField = promise._bitField;
+ if (((bitField & 65536) !== 0)) return;
promise._pushContext();
var x;
- if (receiver === APPLY && !this._isRejected()) {
- x = tryCatch(handler).apply(this._boundValue(), value);
+ if (receiver === APPLY) {
+ if (!value || typeof value.length !== "number") {
+ x = errorObj;
+ x.e = new TypeError("cannot .spread() a non-array: " +
+ util.classString(value));
+ } else {
+ x = tryCatch(handler).apply(this._boundValue(), value);
+ }
} else {
x = tryCatch(handler).call(receiver, value);
}
- promise._popContext();
+ var promiseCreated = promise._popContext();
+ bitField = promise._bitField;
+ if (((bitField & 65536) !== 0)) return;
- if (x === errorObj || x === promise || x === NEXT_FILTER) {
+ if (x === NEXT_FILTER) {
+ promise._reject(value);
+ } else if (x === errorObj || x === promise) {
var err = x === promise ? makeSelfResolutionError() : x.e;
- promise._rejectCallback(err, false, true);
+ promise._rejectCallback(err, false);
} else {
+ debug.checkForgottenReturns(x, promiseCreated, "", promise, this);
promise._resolveCallback(x);
}
};
@@ -2797,201 +3153,217 @@ Promise.prototype._setFollowee = function(promise) {
this._rejectionHandler0 = promise;
};
-Promise.prototype._cleanValues = function () {
- if (this._cancellable()) {
- this._cancellationParent = undefined;
- }
-};
-
-Promise.prototype._propagateFrom = function (parent, flags) {
- if ((flags & 1) > 0 && parent._cancellable()) {
- this._setCancellable();
- this._cancellationParent = parent;
- }
- if ((flags & 4) > 0 && parent._isBound()) {
- this._setBoundTo(parent._boundTo);
- }
-};
-
-Promise.prototype._fulfill = function (value) {
- if (this._isFollowingOrFulfilledOrRejected()) return;
- this._fulfillUnchecked(value);
-};
-
-Promise.prototype._reject = function (reason, carriedStackTrace) {
- if (this._isFollowingOrFulfilledOrRejected()) return;
- this._rejectUnchecked(reason, carriedStackTrace);
-};
-
-Promise.prototype._settlePromiseAt = function (index) {
- var promise = this._promiseAt(index);
+Promise.prototype._settlePromise = function(promise, handler, receiver, value) {
var isPromise = promise instanceof Promise;
-
- if (isPromise && promise._isMigrated()) {
- promise._unsetIsMigrated();
- return async.invoke(this._settlePromiseAt, this, index);
- }
- var handler = this._isFulfilled()
- ? this._fulfillmentHandlerAt(index)
- : this._rejectionHandlerAt(index);
-
- var carriedStackTrace =
- this._isCarryingStackTrace() ? this._getCarriedStackTrace() : undefined;
- var value = this._settledValue;
- var receiver = this._receiverAt(index);
- this._clearCallbackDataAtIndex(index);
-
- if (typeof handler === "function") {
+ var bitField = this._bitField;
+ var asyncGuaranteed = ((bitField & 134217728) !== 0);
+ if (((bitField & 65536) !== 0)) {
+ if (isPromise) promise._invokeInternalOnCancel();
+
+ if (handler === finallyHandler) {
+ receiver.cancelPromise = promise;
+ if (tryCatch(handler).call(receiver, value) === errorObj) {
+ promise._reject(errorObj.e);
+ }
+ } else if (handler === reflectHandler) {
+ promise._fulfill(reflectHandler.call(receiver));
+ } else if (receiver instanceof Proxyable) {
+ receiver._promiseCancelled(promise);
+ } else if (isPromise || promise instanceof PromiseArray) {
+ promise._cancel();
+ } else {
+ receiver.cancel();
+ }
+ } else if (typeof handler === "function") {
if (!isPromise) {
handler.call(receiver, value, promise);
} else {
+ if (asyncGuaranteed) promise._setAsyncGuaranteed();
this._settlePromiseFromHandler(handler, receiver, value, promise);
}
- } else if (receiver instanceof PromiseArray) {
+ } else if (receiver instanceof Proxyable) {
if (!receiver._isResolved()) {
- if (this._isFulfilled()) {
+ if (((bitField & 33554432) !== 0)) {
receiver._promiseFulfilled(value, promise);
- }
- else {
+ } else {
receiver._promiseRejected(value, promise);
}
}
} else if (isPromise) {
- if (this._isFulfilled()) {
+ if (asyncGuaranteed) promise._setAsyncGuaranteed();
+ if (((bitField & 33554432) !== 0)) {
promise._fulfill(value);
} else {
- promise._reject(value, carriedStackTrace);
+ promise._reject(value);
}
}
-
- if (index >= 4 && (index & 31) === 4)
- async.invokeLater(this._setLength, this, 0);
};
-Promise.prototype._clearCallbackDataAtIndex = function(index) {
- if (index === 0) {
- if (!this._isCarryingStackTrace()) {
- this._fulfillmentHandler0 = undefined;
+Promise.prototype._settlePromiseLateCancellationObserver = function(ctx) {
+ var handler = ctx.handler;
+ var promise = ctx.promise;
+ var receiver = ctx.receiver;
+ var value = ctx.value;
+ if (typeof handler === "function") {
+ if (!(promise instanceof Promise)) {
+ handler.call(receiver, value, promise);
+ } else {
+ this._settlePromiseFromHandler(handler, receiver, value, promise);
}
- this._rejectionHandler0 =
- this._progressHandler0 =
- this._receiver0 =
- this._promise0 = undefined;
- } else {
- var base = index * 5 - 5;
- this[base + 3] =
- this[base + 4] =
- this[base + 0] =
- this[base + 1] =
- this[base + 2] = undefined;
+ } else if (promise instanceof Promise) {
+ promise._reject(value);
}
};
-Promise.prototype._isSettlePromisesQueued = function () {
- return (this._bitField &
- -1073741824) === -1073741824;
+Promise.prototype._settlePromiseCtx = function(ctx) {
+ this._settlePromise(ctx.promise, ctx.handler, ctx.receiver, ctx.value);
};
-Promise.prototype._setSettlePromisesQueued = function () {
- this._bitField = this._bitField | -1073741824;
-};
-
-Promise.prototype._unsetSettlePromisesQueued = function () {
- this._bitField = this._bitField & (~-1073741824);
+Promise.prototype._settlePromise0 = function(handler, value, bitField) {
+ var promise = this._promise0;
+ var receiver = this._receiverAt(0);
+ this._promise0 = undefined;
+ this._receiver0 = undefined;
+ this._settlePromise(promise, handler, receiver, value);
};
-Promise.prototype._queueSettlePromises = function() {
- async.settlePromises(this);
- this._setSettlePromisesQueued();
+Promise.prototype._clearCallbackDataAtIndex = function(index) {
+ var base = index * 4 - 4;
+ this[base + 2] =
+ this[base + 3] =
+ this[base + 0] =
+ this[base + 1] = undefined;
};
-Promise.prototype._fulfillUnchecked = function (value) {
+Promise.prototype._fulfill = function (value) {
+ var bitField = this._bitField;
+ if (((bitField & 117506048) >>> 16)) return;
if (value === this) {
var err = makeSelfResolutionError();
this._attachExtraTrace(err);
- return this._rejectUnchecked(err, undefined);
+ return this._reject(err);
}
this._setFulfilled();
- this._settledValue = value;
- this._cleanValues();
+ this._rejectionHandler0 = value;
- if (this._length() > 0) {
- this._queueSettlePromises();
+ if ((bitField & 65535) > 0) {
+ if (((bitField & 134217728) !== 0)) {
+ this._settlePromises();
+ } else {
+ async.settlePromises(this);
+ }
}
};
-Promise.prototype._rejectUncheckedCheckError = function (reason) {
- var trace = util.ensureErrorObject(reason);
- this._rejectUnchecked(reason, trace === reason ? undefined : trace);
-};
-
-Promise.prototype._rejectUnchecked = function (reason, trace) {
- if (reason === this) {
- var err = makeSelfResolutionError();
- this._attachExtraTrace(err);
- return this._rejectUnchecked(err);
- }
+Promise.prototype._reject = function (reason) {
+ var bitField = this._bitField;
+ if (((bitField & 117506048) >>> 16)) return;
this._setRejected();
- this._settledValue = reason;
- this._cleanValues();
+ this._fulfillmentHandler0 = reason;
if (this._isFinal()) {
- async.throwLater(function(e) {
- if ("stack" in e) {
- async.invokeFirst(
- CapturedTrace.unhandledRejection, undefined, e);
- }
- throw e;
- }, trace === undefined ? reason : trace);
- return;
+ return async.fatalError(reason, util.isNode);
}
- if (trace !== undefined && trace !== reason) {
- this._setCarriedStackTrace(trace);
- }
-
- if (this._length() > 0) {
- this._queueSettlePromises();
+ if ((bitField & 65535) > 0) {
+ if (((bitField & 134217728) !== 0)) {
+ this._settlePromises();
+ } else {
+ async.settlePromises(this);
+ }
} else {
this._ensurePossibleRejectionHandled();
}
};
+Promise.prototype._fulfillPromises = function (len, value) {
+ for (var i = 1; i < len; i++) {
+ var handler = this._fulfillmentHandlerAt(i);
+ var promise = this._promiseAt(i);
+ var receiver = this._receiverAt(i);
+ this._clearCallbackDataAtIndex(i);
+ this._settlePromise(promise, handler, receiver, value);
+ }
+};
+
+Promise.prototype._rejectPromises = function (len, reason) {
+ for (var i = 1; i < len; i++) {
+ var handler = this._rejectionHandlerAt(i);
+ var promise = this._promiseAt(i);
+ var receiver = this._receiverAt(i);
+ this._clearCallbackDataAtIndex(i);
+ this._settlePromise(promise, handler, receiver, reason);
+ }
+};
+
Promise.prototype._settlePromises = function () {
- this._unsetSettlePromisesQueued();
- var len = this._length();
- for (var i = 0; i < len; i++) {
- this._settlePromiseAt(i);
+ var bitField = this._bitField;
+ var len = (bitField & 65535);
+
+ if (len > 0) {
+ if (((bitField & 16842752) !== 0)) {
+ var reason = this._fulfillmentHandler0;
+ this._settlePromise0(this._rejectionHandler0, reason, bitField);
+ this._rejectPromises(len, reason);
+ } else {
+ var value = this._rejectionHandler0;
+ this._settlePromise0(this._fulfillmentHandler0, value, bitField);
+ this._fulfillPromises(len, value);
+ }
+ this._setLength(0);
+ }
+ this._clearCancellationData();
+};
+
+Promise.prototype._settledValue = function() {
+ var bitField = this._bitField;
+ if (((bitField & 33554432) !== 0)) {
+ return this._rejectionHandler0;
+ } else if (((bitField & 16777216) !== 0)) {
+ return this._fulfillmentHandler0;
}
};
+function deferResolve(v) {this.promise._resolveCallback(v);}
+function deferReject(v) {this.promise._rejectCallback(v, false);}
+
+Promise.defer = Promise.pending = function() {
+ debug.deprecated("Promise.defer", "new Promise");
+ var promise = new Promise(INTERNAL);
+ return {
+ promise: promise,
+ resolve: deferResolve,
+ reject: deferReject
+ };
+};
+
util.notEnumerableProp(Promise,
"_makeSelfResolutionError",
makeSelfResolutionError);
-_dereq_("./progress.js")(Promise, PromiseArray);
-_dereq_("./method.js")(Promise, INTERNAL, tryConvertToPromise, apiRejection);
-_dereq_("./bind.js")(Promise, INTERNAL, tryConvertToPromise);
-_dereq_("./finally.js")(Promise, NEXT_FILTER, tryConvertToPromise);
-_dereq_("./direct_resolve.js")(Promise);
-_dereq_("./synchronous_inspection.js")(Promise);
-_dereq_("./join.js")(Promise, PromiseArray, tryConvertToPromise, INTERNAL);
+_dereq_("./method")(Promise, INTERNAL, tryConvertToPromise, apiRejection,
+ debug);
+_dereq_("./bind")(Promise, INTERNAL, tryConvertToPromise, debug);
+_dereq_("./cancel")(Promise, PromiseArray, apiRejection, debug);
+_dereq_("./direct_resolve")(Promise);
+_dereq_("./synchronous_inspection")(Promise);
+_dereq_("./join")(
+ Promise, PromiseArray, tryConvertToPromise, INTERNAL, debug);
Promise.Promise = Promise;
-_dereq_('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
-_dereq_('./cancel.js')(Promise);
-_dereq_('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext);
-_dereq_('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise);
+_dereq_('./map.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
+_dereq_('./using.js')(Promise, apiRejection, tryConvertToPromise, createContext, INTERNAL, debug);
+_dereq_('./timers.js')(Promise, INTERNAL);
+_dereq_('./generators.js')(Promise, apiRejection, INTERNAL, tryConvertToPromise, Proxyable, debug);
_dereq_('./nodeify.js')(Promise);
_dereq_('./call_get.js')(Promise);
_dereq_('./props.js')(Promise, PromiseArray, tryConvertToPromise, apiRejection);
_dereq_('./race.js')(Promise, INTERNAL, tryConvertToPromise, apiRejection);
-_dereq_('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL);
-_dereq_('./settle.js')(Promise, PromiseArray);
+_dereq_('./reduce.js')(Promise, PromiseArray, apiRejection, tryConvertToPromise, INTERNAL, debug);
+_dereq_('./settle.js')(Promise, PromiseArray, debug);
_dereq_('./some.js')(Promise, PromiseArray, apiRejection);
_dereq_('./promisify.js')(Promise, INTERNAL);
_dereq_('./any.js')(Promise);
_dereq_('./each.js')(Promise, INTERNAL);
-_dereq_('./timers.js')(Promise, INTERNAL);
_dereq_('./filter.js')(Promise, INTERNAL);
util.toFastProperties(Promise);
@@ -3000,10 +3372,8 @@ _dereq_('./filter.js')(Promise, INTERNAL);
var p = new Promise(INTERNAL);
p._fulfillmentHandler0 = value;
p._rejectionHandler0 = value;
- p._progressHandler0 = value;
p._promise0 = value;
p._receiver0 = value;
- p._settledValue = value;
}
// Complete slack tracking, opt out of field-type tracking and
// stabilize map
@@ -3015,16 +3385,16 @@ _dereq_('./filter.js')(Promise, INTERNAL);
fillTypes(undefined);
fillTypes(false);
fillTypes(new Promise(INTERNAL));
- CapturedTrace.setBounds(async.firstLineError, util.lastLineError);
+ debug.setBounds(Async.firstLineError, util.lastLineError);
return Promise;
};
-},{"./any.js":1,"./async.js":2,"./bind.js":3,"./call_get.js":5,"./cancel.js":6,"./captured_trace.js":7,"./catch_filter.js":8,"./context.js":9,"./debuggability.js":10,"./direct_resolve.js":11,"./each.js":12,"./errors.js":13,"./filter.js":15,"./finally.js":16,"./generators.js":17,"./join.js":18,"./map.js":19,"./method.js":20,"./nodeify.js":21,"./progress.js":22,"./promise_array.js":24,"./promise_resolver.js":25,"./promisify.js":26,"./props.js":27,"./race.js":29,"./reduce.js":30,"./settle.js":32,"./some.js":33,"./synchronous_inspection.js":34,"./thenables.js":35,"./timers.js":36,"./using.js":37,"./util.js":38}],24:[function(_dereq_,module,exports){
+},{"./any.js":1,"./async":2,"./bind":3,"./call_get.js":5,"./cancel":6,"./catch_filter":7,"./context":8,"./debuggability":9,"./direct_resolve":10,"./each.js":11,"./errors":12,"./es5":13,"./filter.js":14,"./finally":15,"./generators.js":16,"./join":17,"./map.js":18,"./method":19,"./nodeback":20,"./nodeify.js":21,"./promise_array":23,"./promisify.js":24,"./props.js":25,"./race.js":27,"./reduce.js":28,"./settle.js":30,"./some.js":31,"./synchronous_inspection":32,"./thenables":33,"./timers.js":34,"./using.js":35,"./util":36}],23:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise, INTERNAL, tryConvertToPromise,
- apiRejection) {
-var util = _dereq_("./util.js");
+ apiRejection, Proxyable) {
+var util = _dereq_("./util");
var isArray = util.isArray;
function toResolutionValue(val) {
@@ -3036,16 +3406,17 @@ function toResolutionValue(val) {
function PromiseArray(values) {
var promise = this._promise = new Promise(INTERNAL);
- var parent;
if (values instanceof Promise) {
- parent = values;
- promise._propagateFrom(parent, 1 | 4);
+ promise._propagateFrom(values, 3);
}
+ promise._setOnCancel(this);
this._values = values;
this._length = 0;
this._totalResolved = 0;
this._init(undefined, -2);
}
+util.inherits(PromiseArray, Proxyable);
+
PromiseArray.prototype.length = function () {
return this._length;
};
@@ -3058,29 +3429,32 @@ PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
var values = tryConvertToPromise(this._values, this._promise);
if (values instanceof Promise) {
values = values._target();
+ var bitField = values._bitField;
+ ;
this._values = values;
- if (values._isFulfilled()) {
- values = values._value();
- if (!isArray(values)) {
- var err = new Promise.TypeError("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a");
- this.__hardReject__(err);
- return;
- }
- } else if (values._isPending()) {
- values._then(
+
+ if (((bitField & 50397184) === 0)) {
+ this._promise._setAsyncGuaranteed();
+ return values._then(
init,
this._reject,
undefined,
this,
resolveValueIfEmpty
);
- return;
+ } else if (((bitField & 33554432) !== 0)) {
+ values = values._value();
+ } else if (((bitField & 16777216) !== 0)) {
+ return this._reject(values._reason());
} else {
- this._reject(values._reason());
- return;
+ return this._cancel();
}
- } else if (!isArray(values)) {
- this._promise._reject(apiRejection("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a")._reason());
+ }
+ values = util.asArray(values);
+ if (values === null) {
+ var err = apiRejection(
+ "expecting an array or an iterable object but got " + util.classString(values)).reason();
+ this._promise._rejectCallback(err, false);
return;
}
@@ -3093,28 +3467,46 @@ PromiseArray.prototype._init = function init(_, resolveValueIfEmpty) {
}
return;
}
+ this._iterate(values);
+};
+
+PromiseArray.prototype._iterate = function(values) {
var len = this.getActualLength(values.length);
this._length = len;
this._values = this.shouldCopyValues() ? new Array(len) : this._values;
- var promise = this._promise;
+ var result = this._promise;
+ var isResolved = false;
+ var bitField = null;
for (var i = 0; i < len; ++i) {
- var isResolved = this._isResolved();
- var maybePromise = tryConvertToPromise(values[i], promise);
+ var maybePromise = tryConvertToPromise(values[i], result);
+
if (maybePromise instanceof Promise) {
maybePromise = maybePromise._target();
- if (isResolved) {
- maybePromise._ignoreRejections();
- } else if (maybePromise._isPending()) {
- maybePromise._proxyPromiseArray(this, i);
- } else if (maybePromise._isFulfilled()) {
- this._promiseFulfilled(maybePromise._value(), i);
+ bitField = maybePromise._bitField;
+ } else {
+ bitField = null;
+ }
+
+ if (isResolved) {
+ if (bitField !== null) {
+ maybePromise.suppressUnhandledRejections();
+ }
+ } else if (bitField !== null) {
+ if (((bitField & 50397184) === 0)) {
+ maybePromise._proxy(this, i);
+ this._values[i] = maybePromise;
+ } else if (((bitField & 33554432) !== 0)) {
+ isResolved = this._promiseFulfilled(maybePromise._value(), i);
+ } else if (((bitField & 16777216) !== 0)) {
+ isResolved = this._promiseRejected(maybePromise._reason(), i);
} else {
- this._promiseRejected(maybePromise._reason(), i);
+ isResolved = this._promiseCancelled(i);
}
- } else if (!isResolved) {
- this._promiseFulfilled(maybePromise, i);
+ } else {
+ isResolved = this._promiseFulfilled(maybePromise, i);
}
}
+ if (!isResolved) result._setAsyncGuaranteed();
};
PromiseArray.prototype._isResolved = function () {
@@ -3126,176 +3518,70 @@ PromiseArray.prototype._resolve = function (value) {
this._promise._fulfill(value);
};
-PromiseArray.prototype.__hardReject__ =
-PromiseArray.prototype._reject = function (reason) {
+PromiseArray.prototype._cancel = function() {
+ if (this._isResolved() || !this._promise.isCancellable()) return;
this._values = null;
- this._promise._rejectCallback(reason, false, true);
+ this._promise._cancel();
};
-PromiseArray.prototype._promiseProgressed = function (progressValue, index) {
- this._promise._progress({
- index: index,
- value: progressValue
- });
+PromiseArray.prototype._reject = function (reason) {
+ this._values = null;
+ this._promise._rejectCallback(reason, false);
};
-
PromiseArray.prototype._promiseFulfilled = function (value, index) {
this._values[index] = value;
var totalResolved = ++this._totalResolved;
if (totalResolved >= this._length) {
this._resolve(this._values);
+ return true;
}
+ return false;
};
-PromiseArray.prototype._promiseRejected = function (reason, index) {
- this._totalResolved++;
- this._reject(reason);
-};
-
-PromiseArray.prototype.shouldCopyValues = function () {
+PromiseArray.prototype._promiseCancelled = function() {
+ this._cancel();
return true;
};
-PromiseArray.prototype.getActualLength = function (len) {
- return len;
-};
-
-return PromiseArray;
+PromiseArray.prototype._promiseRejected = function (reason) {
+ this._totalResolved++;
+ this._reject(reason);
+ return true;
};
-},{"./util.js":38}],25:[function(_dereq_,module,exports){
-"use strict";
-var util = _dereq_("./util.js");
-var maybeWrapAsError = util.maybeWrapAsError;
-var errors = _dereq_("./errors.js");
-var TimeoutError = errors.TimeoutError;
-var OperationalError = errors.OperationalError;
-var haveGetters = util.haveGetters;
-var es5 = _dereq_("./es5.js");
-
-function isUntypedError(obj) {
- return obj instanceof Error &&
- es5.getPrototypeOf(obj) === Error.prototype;
-}
-
-var rErrorKey = /^(?:name|message|stack|cause)$/;
-function wrapAsOperationalError(obj) {
- var ret;
- if (isUntypedError(obj)) {
- ret = new OperationalError(obj);
- ret.name = obj.name;
- ret.message = obj.message;
- ret.stack = obj.stack;
- var keys = es5.keys(obj);
- for (var i = 0; i < keys.length; ++i) {
- var key = keys[i];
- if (!rErrorKey.test(key)) {
- ret[key] = obj[key];
+PromiseArray.prototype._resultCancelled = function() {
+ if (this._isResolved()) return;
+ var values = this._values;
+ this._cancel();
+ if (values instanceof Promise) {
+ values.cancel();
+ } else {
+ for (var i = 0; i < values.length; ++i) {
+ if (values[i] instanceof Promise) {
+ values[i].cancel();
}
}
- return ret;
- }
- util.markAsOriginatingFromRejection(obj);
- return obj;
-}
-
-function nodebackForPromise(promise) {
- return function(err, value) {
- if (promise === null) return;
-
- if (err) {
- var wrapped = wrapAsOperationalError(maybeWrapAsError(err));
- promise._attachExtraTrace(wrapped);
- promise._reject(wrapped);
- } else if (arguments.length > 2) {
- var $_len = arguments.length;var args = new Array($_len - 1); for(var $_i = 1; $_i < $_len; ++$_i) {args[$_i - 1] = arguments[$_i];}
- promise._fulfill(args);
- } else {
- promise._fulfill(value);
- }
-
- promise = null;
- };
-}
-
-
-var PromiseResolver;
-if (!haveGetters) {
- PromiseResolver = function (promise) {
- this.promise = promise;
- this.asCallback = nodebackForPromise(promise);
- this.callback = this.asCallback;
- };
-}
-else {
- PromiseResolver = function (promise) {
- this.promise = promise;
- };
-}
-if (haveGetters) {
- var prop = {
- get: function() {
- return nodebackForPromise(this.promise);
- }
- };
- es5.defineProperty(PromiseResolver.prototype, "asCallback", prop);
- es5.defineProperty(PromiseResolver.prototype, "callback", prop);
-}
-
-PromiseResolver._nodebackForPromise = nodebackForPromise;
-
-PromiseResolver.prototype.toString = function () {
- return "[object PromiseResolver]";
-};
-
-PromiseResolver.prototype.resolve =
-PromiseResolver.prototype.fulfill = function (value) {
- if (!(this instanceof PromiseResolver)) {
- throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
- }
- this.promise._resolveCallback(value);
-};
-
-PromiseResolver.prototype.reject = function (reason) {
- if (!(this instanceof PromiseResolver)) {
- throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
- }
- this.promise._rejectCallback(reason);
-};
-
-PromiseResolver.prototype.progress = function (value) {
- if (!(this instanceof PromiseResolver)) {
- throw new TypeError("Illegal invocation, resolver resolve/reject must be called within a resolver context. Consider using the promise constructor instead.\u000a\u000a See http://goo.gl/sdkXL9\u000a");
}
- this.promise._progress(value);
-};
-
-PromiseResolver.prototype.cancel = function (err) {
- this.promise.cancel(err);
};
-PromiseResolver.prototype.timeout = function () {
- this.reject(new TimeoutError("timeout"));
+PromiseArray.prototype.shouldCopyValues = function () {
+ return true;
};
-PromiseResolver.prototype.isResolved = function () {
- return this.promise.isResolved();
+PromiseArray.prototype.getActualLength = function (len) {
+ return len;
};
-PromiseResolver.prototype.toJSON = function () {
- return this.promise.toJSON();
+return PromiseArray;
};
-module.exports = PromiseResolver;
-
-},{"./errors.js":13,"./es5.js":14,"./util.js":38}],26:[function(_dereq_,module,exports){
+},{"./util":36}],24:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise, INTERNAL) {
var THIS = {};
-var util = _dereq_("./util.js");
-var nodebackForPromise = _dereq_("./promise_resolver.js")
- ._nodebackForPromise;
+var util = _dereq_("./util");
+var nodebackForPromise = _dereq_("./nodeback");
var withAppended = util.withAppended;
var maybeWrapAsError = util.maybeWrapAsError;
var canEvaluate = util.canEvaluate;
@@ -3344,7 +3630,7 @@ function checkValid(ret, suffix, suffixRegexp) {
var keyWithoutAsyncSuffix = key.replace(suffixRegexp, "");
for (var j = 0; j < ret.length; j += 2) {
if (ret[j] === keyWithoutAsyncSuffix) {
- throw new TypeError("Cannot promisify an API that has normal methods with '%s'-suffix\u000a\u000a See http://goo.gl/iWrZbw\u000a"
+ throw new TypeError("Cannot promisify an API that has normal methods with '%s'-suffix\u000a\u000a See http://goo.gl/MqrFmX\u000a"
.replace("%s", suffix));
}
}
@@ -3406,7 +3692,7 @@ var parameterCount = function(fn) {
};
makeNodePromisifiedEval =
-function(callback, receiver, originalName, fn) {
+function(callback, receiver, originalName, fn, _, multiArgs) {
var newParameterCount = Math.max(0, parameterCount(fn) - 1);
var argumentOrder = switchCaseArgumentOrder(newParameterCount);
var shouldProxyThis = typeof callback === "string" || receiver === THIS;
@@ -3451,23 +3737,13 @@ function(callback, receiver, originalName, fn) {
var getFunctionCode = typeof callback === "string"
? ("this != null ? this['"+callback+"'] : fn")
: "fn";
-
- return new Function("Promise",
- "fn",
- "receiver",
- "withAppended",
- "maybeWrapAsError",
- "nodebackForPromise",
- "tryCatch",
- "errorObj",
- "notEnumerableProp",
- "INTERNAL","'use strict'; \n\
+ var body = "'use strict'; \n\
var ret = function (Parameters) { \n\
'use strict'; \n\
var len = arguments.length; \n\
var promise = new Promise(INTERNAL); \n\
promise._captureStackTrace(); \n\
- var nodeback = nodebackForPromise(promise); \n\
+ var nodeback = nodebackForPromise(promise, " + multiArgs + "); \n\
var ret; \n\
var callback = tryCatch([GetFunctionCode]); \n\
switch(len) { \n\
@@ -3476,29 +3752,39 @@ function(callback, receiver, originalName, fn) {
if (ret === errorObj) { \n\
promise._rejectCallback(maybeWrapAsError(ret.e), true, true);\n\
} \n\
+ if (!promise._isFateSealed()) promise._setAsyncGuaranteed(); \n\
return promise; \n\
}; \n\
notEnumerableProp(ret, '__isPromisified__', true); \n\
return ret; \n\
- "
- .replace("Parameters", parameterDeclaration(newParameterCount))
- .replace("[CodeForSwitchCase]", generateArgumentSwitchCase())
- .replace("[GetFunctionCode]", getFunctionCode))(
- Promise,
- fn,
- receiver,
- withAppended,
- maybeWrapAsError,
- nodebackForPromise,
- util.tryCatch,
- util.errorObj,
- util.notEnumerableProp,
- INTERNAL
- );
+ ".replace("[CodeForSwitchCase]", generateArgumentSwitchCase())
+ .replace("[GetFunctionCode]", getFunctionCode);
+ body = body.replace("Parameters", parameterDeclaration(newParameterCount));
+ return new Function("Promise",
+ "fn",
+ "receiver",
+ "withAppended",
+ "maybeWrapAsError",
+ "nodebackForPromise",
+ "tryCatch",
+ "errorObj",
+ "notEnumerableProp",
+ "INTERNAL",
+ body)(
+ Promise,
+ fn,
+ receiver,
+ withAppended,
+ maybeWrapAsError,
+ nodebackForPromise,
+ util.tryCatch,
+ util.errorObj,
+ util.notEnumerableProp,
+ INTERNAL);
};
}
-function makeNodePromisifiedClosure(callback, receiver, _, fn) {
+function makeNodePromisifiedClosure(callback, receiver, _, fn, __, multiArgs) {
var defaultThis = (function() {return this;})();
var method = callback;
if (typeof method === "string") {
@@ -3511,12 +3797,13 @@ function makeNodePromisifiedClosure(callback, receiver, _, fn) {
promise._captureStackTrace();
var cb = typeof method === "string" && this !== defaultThis
? this[method] : callback;
- var fn = nodebackForPromise(promise);
+ var fn = nodebackForPromise(promise, multiArgs);
try {
cb.apply(_receiver, withAppended(arguments, fn));
} catch(e) {
promise._rejectCallback(maybeWrapAsError(e), true, true);
}
+ if (!promise._isFateSealed()) promise._setAsyncGuaranteed();
return promise;
}
util.notEnumerableProp(promisified, "__isPromisified__", true);
@@ -3527,7 +3814,7 @@ var makeNodePromisified = canEvaluate
? makeNodePromisifiedEval
: makeNodePromisifiedClosure;
-function promisifyAll(obj, suffix, filter, promisifier) {
+function promisifyAll(obj, suffix, filter, promisifier, multiArgs) {
var suffixRegexp = new RegExp(escapeIdentRegex(suffix) + "$");
var methods =
promisifiableMethods(obj, suffix, suffixRegexp, filter);
@@ -3538,10 +3825,11 @@ function promisifyAll(obj, suffix, filter, promisifier) {
var promisifiedKey = key + suffix;
if (promisifier === makeNodePromisified) {
obj[promisifiedKey] =
- makeNodePromisified(key, THIS, key, fn, suffix);
+ makeNodePromisified(key, THIS, key, fn, suffix, multiArgs);
} else {
var promisified = promisifier(fn, function() {
- return makeNodePromisified(key, THIS, key, fn, suffix);
+ return makeNodePromisified(key, THIS, key,
+ fn, suffix, multiArgs);
});
util.notEnumerableProp(promisified, "__isPromisified__", true);
obj[promisifiedKey] = promisified;
@@ -3551,27 +3839,32 @@ function promisifyAll(obj, suffix, filter, promisifier) {
return obj;
}
-function promisify(callback, receiver) {
- return makeNodePromisified(callback, receiver, undefined, callback);
+function promisify(callback, receiver, multiArgs) {
+ return makeNodePromisified(callback, receiver, undefined,
+ callback, null, multiArgs);
}
-Promise.promisify = function (fn, receiver) {
+Promise.promisify = function (fn, options) {
if (typeof fn !== "function") {
- throw new TypeError("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
+ throw new TypeError("expecting a function but got " + util.classString(fn));
}
if (isPromisified(fn)) {
return fn;
}
- var ret = promisify(fn, arguments.length < 2 ? THIS : receiver);
+ options = Object(options);
+ var receiver = options.context === undefined ? THIS : options.context;
+ var multiArgs = !!options.multiArgs;
+ var ret = promisify(fn, receiver, multiArgs);
util.copyDescriptors(fn, ret, propsFilter);
return ret;
};
Promise.promisifyAll = function (target, options) {
if (typeof target !== "function" && typeof target !== "object") {
- throw new TypeError("the target of promisifyAll must be an object or a function\u000a\u000a See http://goo.gl/9ITlV0\u000a");
+ throw new TypeError("the target of promisifyAll must be an object or a function\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
options = Object(options);
+ var multiArgs = !!options.multiArgs;
var suffix = options.suffix;
if (typeof suffix !== "string") suffix = defaultSuffix;
var filter = options.filter;
@@ -3580,7 +3873,7 @@ Promise.promisifyAll = function (target, options) {
if (typeof promisifier !== "function") promisifier = makeNodePromisified;
if (!util.isIdentifier(suffix)) {
- throw new RangeError("suffix must be a valid identifier\u000a\u000a See http://goo.gl/8FZo5V\u000a");
+ throw new RangeError("suffix must be a valid identifier\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
var keys = util.inheritedDataKeys(target);
@@ -3588,59 +3881,99 @@ Promise.promisifyAll = function (target, options) {
var value = target[keys[i]];
if (keys[i] !== "constructor" &&
util.isClass(value)) {
- promisifyAll(value.prototype, suffix, filter, promisifier);
- promisifyAll(value, suffix, filter, promisifier);
+ promisifyAll(value.prototype, suffix, filter, promisifier,
+ multiArgs);
+ promisifyAll(value, suffix, filter, promisifier, multiArgs);
}
}
- return promisifyAll(target, suffix, filter, promisifier);
+ return promisifyAll(target, suffix, filter, promisifier, multiArgs);
};
};
-},{"./errors":13,"./promise_resolver.js":25,"./util.js":38}],27:[function(_dereq_,module,exports){
+},{"./errors":12,"./nodeback":20,"./util":36}],25:[function(_dereq_,module,exports){
"use strict";
module.exports = function(
Promise, PromiseArray, tryConvertToPromise, apiRejection) {
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var isObject = util.isObject;
-var es5 = _dereq_("./es5.js");
+var es5 = _dereq_("./es5");
+var Es6Map;
+if (typeof Map === "function") Es6Map = Map;
+
+var mapToEntries = (function() {
+ var index = 0;
+ var size = 0;
+
+ function extractEntry(value, key) {
+ this[index] = value;
+ this[index + size] = key;
+ index++;
+ }
+
+ return function mapToEntries(map) {
+ size = map.size;
+ index = 0;
+ var ret = new Array(map.size * 2);
+ map.forEach(extractEntry, ret);
+ return ret;
+ };
+})();
+
+var entriesToMap = function(entries) {
+ var ret = new Es6Map();
+ var length = entries.length / 2 | 0;
+ for (var i = 0; i < length; ++i) {
+ var key = entries[length + i];
+ var value = entries[i];
+ ret.set(key, value);
+ }
+ return ret;
+};
function PropertiesPromiseArray(obj) {
- var keys = es5.keys(obj);
- var len = keys.length;
- var values = new Array(len * 2);
- for (var i = 0; i < len; ++i) {
- var key = keys[i];
- values[i] = obj[key];
- values[i + len] = key;
+ var isMap = false;
+ var entries;
+ if (Es6Map !== undefined && obj instanceof Es6Map) {
+ entries = mapToEntries(obj);
+ isMap = true;
+ } else {
+ var keys = es5.keys(obj);
+ var len = keys.length;
+ entries = new Array(len * 2);
+ for (var i = 0; i < len; ++i) {
+ var key = keys[i];
+ entries[i] = obj[key];
+ entries[i + len] = key;
+ }
}
- this.constructor$(values);
+ this.constructor$(entries);
+ this._isMap = isMap;
+ this._init$(undefined, -3);
}
util.inherits(PropertiesPromiseArray, PromiseArray);
-PropertiesPromiseArray.prototype._init = function () {
- this._init$(undefined, -3) ;
-};
+PropertiesPromiseArray.prototype._init = function () {};
PropertiesPromiseArray.prototype._promiseFulfilled = function (value, index) {
this._values[index] = value;
var totalResolved = ++this._totalResolved;
if (totalResolved >= this._length) {
- var val = {};
- var keyOffset = this.length();
- for (var i = 0, len = this.length(); i < len; ++i) {
- val[this._values[i + keyOffset]] = this._values[i];
+ var val;
+ if (this._isMap) {
+ val = entriesToMap(this._values);
+ } else {
+ val = {};
+ var keyOffset = this.length();
+ for (var i = 0, len = this.length(); i < len; ++i) {
+ val[this._values[i + keyOffset]] = this._values[i];
+ }
}
this._resolve(val);
+ return true;
}
-};
-
-PropertiesPromiseArray.prototype._promiseProgressed = function (value, index) {
- this._promise._progress({
- key: this._values[index + this.length()],
- value: value
- });
+ return false;
};
PropertiesPromiseArray.prototype.shouldCopyValues = function () {
@@ -3656,7 +3989,7 @@ function props(promises) {
var castValue = tryConvertToPromise(promises);
if (!isObject(castValue)) {
- return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/OsFKC8\u000a");
+ return apiRejection("cannot await properties of a non-object\u000a\u000a See http://goo.gl/MqrFmX\u000a");
} else if (castValue instanceof Promise) {
ret = castValue._then(
Promise.props, undefined, undefined, undefined, undefined);
@@ -3665,7 +3998,7 @@ function props(promises) {
}
if (castValue instanceof Promise) {
- ret._propagateFrom(castValue, 4);
+ ret._propagateFrom(castValue, 2);
}
return ret;
}
@@ -3679,7 +4012,7 @@ Promise.props = function (promises) {
};
};
-},{"./es5.js":14,"./util.js":38}],28:[function(_dereq_,module,exports){
+},{"./es5":13,"./util":36}],26:[function(_dereq_,module,exports){
"use strict";
function arrayMove(src, srcIndex, dst, dstIndex, len) {
for (var j = 0; j < len; ++j) {
@@ -3771,11 +4104,11 @@ Queue.prototype._resizeTo = function (capacity) {
module.exports = Queue;
-},{}],29:[function(_dereq_,module,exports){
+},{}],27:[function(_dereq_,module,exports){
"use strict";
module.exports = function(
Promise, INTERNAL, tryConvertToPromise, apiRejection) {
-var isArray = _dereq_("./util.js").isArray;
+var util = _dereq_("./util");
var raceLater = function (promise) {
return promise.then(function(array) {
@@ -3788,13 +4121,15 @@ function race(promises, parent) {
if (maybePromise instanceof Promise) {
return raceLater(maybePromise);
- } else if (!isArray(promises)) {
- return apiRejection("expecting an array, a promise or a thenable\u000a\u000a See http://goo.gl/s8MMhc\u000a");
+ } else {
+ promises = util.asArray(promises);
+ if (promises === null)
+ return apiRejection("expecting an array or an iterable object but got " + util.classString(promises));
}
var ret = new Promise(INTERNAL);
if (parent !== undefined) {
- ret._propagateFrom(parent, 4 | 1);
+ ret._propagateFrom(parent, 3);
}
var fulfill = ret._fulfill;
var reject = ret._reject;
@@ -3820,147 +4155,108 @@ Promise.prototype.race = function () {
};
-},{"./util.js":38}],30:[function(_dereq_,module,exports){
+},{"./util":36}],28:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise,
PromiseArray,
apiRejection,
tryConvertToPromise,
- INTERNAL) {
+ INTERNAL,
+ debug) {
var getDomain = Promise._getDomain;
-var async = _dereq_("./async.js");
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var tryCatch = util.tryCatch;
-var errorObj = util.errorObj;
-function ReductionPromiseArray(promises, fn, accum, _each) {
+
+function ReductionPromiseArray(promises, fn, initialValue, _each) {
this.constructor$(promises);
- this._promise._captureStackTrace();
- this._preservedValues = _each === INTERNAL ? [] : null;
- this._zerothIsAccum = (accum === undefined);
- this._gotAccum = false;
- this._reducingIndex = (this._zerothIsAccum ? 1 : 0);
- this._valuesPhase = undefined;
- var maybePromise = tryConvertToPromise(accum, this._promise);
- var rejected = false;
- var isPromise = maybePromise instanceof Promise;
- if (isPromise) {
- maybePromise = maybePromise._target();
- if (maybePromise._isPending()) {
- maybePromise._proxyPromiseArray(this, -1);
- } else if (maybePromise._isFulfilled()) {
- accum = maybePromise._value();
- this._gotAccum = true;
- } else {
- this._reject(maybePromise._reason());
- rejected = true;
- }
- }
- if (!(isPromise || this._zerothIsAccum)) this._gotAccum = true;
var domain = getDomain();
- this._callback = domain === null ? fn : domain.bind(fn);
- this._accum = accum;
- if (!rejected) async.invoke(init, this, undefined);
-}
-function init() {
+ this._fn = domain === null ? fn : domain.bind(fn);
+ if (initialValue !== undefined) {
+ initialValue = Promise.resolve(initialValue);
+ initialValue._attachCancellationCallback(this);
+ }
+ this._initialValue = initialValue;
+ this._currentCancellable = null;
+ this._eachValues = _each === INTERNAL ? [] : undefined;
+ this._promise._captureStackTrace();
this._init$(undefined, -5);
}
util.inherits(ReductionPromiseArray, PromiseArray);
-ReductionPromiseArray.prototype._init = function () {};
-
-ReductionPromiseArray.prototype._resolveEmptyArray = function () {
- if (this._gotAccum || this._zerothIsAccum) {
- this._resolve(this._preservedValues !== null
- ? [] : this._accum);
+ReductionPromiseArray.prototype._gotAccum = function(accum) {
+ if (this._eachValues !== undefined && accum !== INTERNAL) {
+ this._eachValues.push(accum);
}
};
-ReductionPromiseArray.prototype._promiseFulfilled = function (value, index) {
- var values = this._values;
- values[index] = value;
- var length = this.length();
- var preservedValues = this._preservedValues;
- var isEach = preservedValues !== null;
- var gotAccum = this._gotAccum;
- var valuesPhase = this._valuesPhase;
- var valuesPhaseIndex;
- if (!valuesPhase) {
- valuesPhase = this._valuesPhase = new Array(length);
- for (valuesPhaseIndex=0; valuesPhaseIndex<length; ++valuesPhaseIndex) {
- valuesPhase[valuesPhaseIndex] = 0;
- }
+ReductionPromiseArray.prototype._eachComplete = function(value) {
+ this._eachValues.push(value);
+ return this._eachValues;
+};
+
+ReductionPromiseArray.prototype._init = function() {};
+
+ReductionPromiseArray.prototype._resolveEmptyArray = function() {
+ this._resolve(this._eachValues !== undefined ? this._eachValues
+ : this._initialValue);
+};
+
+ReductionPromiseArray.prototype.shouldCopyValues = function () {
+ return false;
+};
+
+ReductionPromiseArray.prototype._resolve = function(value) {
+ this._promise._resolveCallback(value);
+ this._values = null;
+};
+
+ReductionPromiseArray.prototype._resultCancelled = function(sender) {
+ if (sender === this._initialValue) return this._cancel();
+ if (this._isResolved()) return;
+ this._resultCancelled$();
+ if (this._currentCancellable instanceof Promise) {
+ this._currentCancellable.cancel();
}
- valuesPhaseIndex = valuesPhase[index];
+ if (this._initialValue instanceof Promise) {
+ this._initialValue.cancel();
+ }
+};
- if (index === 0 && this._zerothIsAccum) {
- this._accum = value;
- this._gotAccum = gotAccum = true;
- valuesPhase[index] = ((valuesPhaseIndex === 0)
- ? 1 : 2);
- } else if (index === -1) {
- this._accum = value;
- this._gotAccum = gotAccum = true;
+ReductionPromiseArray.prototype._iterate = function (values) {
+ this._values = values;
+ var value;
+ var i;
+ var length = values.length;
+ if (this._initialValue !== undefined) {
+ value = this._initialValue;
+ i = 0;
} else {
- if (valuesPhaseIndex === 0) {
- valuesPhase[index] = 1;
- } else {
- valuesPhase[index] = 2;
- this._accum = value;
- }
+ value = Promise.resolve(values[0]);
+ i = 1;
}
- if (!gotAccum) return;
-
- var callback = this._callback;
- var receiver = this._promise._boundValue();
- var ret;
-
- for (var i = this._reducingIndex; i < length; ++i) {
- valuesPhaseIndex = valuesPhase[i];
- if (valuesPhaseIndex === 2) {
- this._reducingIndex = i + 1;
- continue;
- }
- if (valuesPhaseIndex !== 1) return;
- value = values[i];
- this._promise._pushContext();
- if (isEach) {
- preservedValues.push(value);
- ret = tryCatch(callback).call(receiver, value, i, length);
- }
- else {
- ret = tryCatch(callback)
- .call(receiver, this._accum, value, i, length);
- }
- this._promise._popContext();
- if (ret === errorObj) return this._reject(ret.e);
+ this._currentCancellable = value;
- var maybePromise = tryConvertToPromise(ret, this._promise);
- if (maybePromise instanceof Promise) {
- maybePromise = maybePromise._target();
- if (maybePromise._isPending()) {
- valuesPhase[i] = 4;
- return maybePromise._proxyPromiseArray(this, i);
- } else if (maybePromise._isFulfilled()) {
- ret = maybePromise._value();
- } else {
- return this._reject(maybePromise._reason());
- }
+ if (!value.isRejected()) {
+ for (; i < length; ++i) {
+ var ctx = {
+ accum: null,
+ value: values[i],
+ index: i,
+ length: length,
+ array: this
+ };
+ value = value._then(gotAccum, undefined, undefined, ctx, undefined);
}
-
- this._reducingIndex = i + 1;
- this._accum = ret;
}
- this._resolve(isEach ? preservedValues : this._accum);
+ if (this._eachValues !== undefined) {
+ value = value
+ ._then(this._eachComplete, undefined, undefined, this, undefined);
+ }
+ value._then(completed, completed, undefined, value, this);
};
-function reduce(promises, fn, initialValue, _each) {
- if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
- var array = new ReductionPromiseArray(promises, fn, initialValue, _each);
- return array.promise();
-}
-
Promise.prototype.reduce = function (fn, initialValue) {
return reduce(this, fn, initialValue, null);
};
@@ -3968,14 +4264,67 @@ Promise.prototype.reduce = function (fn, initialValue) {
Promise.reduce = function (promises, fn, initialValue, _each) {
return reduce(promises, fn, initialValue, _each);
};
+
+function completed(valueOrReason, array) {
+ if (this.isFulfilled()) {
+ array._resolve(valueOrReason);
+ } else {
+ array._reject(valueOrReason);
+ }
+}
+
+function reduce(promises, fn, initialValue, _each) {
+ if (typeof fn !== "function") {
+ return apiRejection("expecting a function but got " + util.classString(fn));
+ }
+ var array = new ReductionPromiseArray(promises, fn, initialValue, _each);
+ return array.promise();
+}
+
+function gotAccum(accum) {
+ this.accum = accum;
+ this.array._gotAccum(accum);
+ var value = tryConvertToPromise(this.value, this.array._promise);
+ if (value instanceof Promise) {
+ this.array._currentCancellable = value;
+ return value._then(gotValue, undefined, undefined, this, undefined);
+ } else {
+ return gotValue.call(this, value);
+ }
+}
+
+function gotValue(value) {
+ var array = this.array;
+ var promise = array._promise;
+ var fn = tryCatch(array._fn);
+ promise._pushContext();
+ var ret;
+ if (array._eachValues !== undefined) {
+ ret = fn.call(promise._boundValue(), value, this.index, this.length);
+ } else {
+ ret = fn.call(promise._boundValue(),
+ this.accum, value, this.index, this.length);
+ }
+ if (ret instanceof Promise) {
+ array._currentCancellable = ret;
+ }
+ var promiseCreated = promise._popContext();
+ debug.checkForgottenReturns(
+ ret,
+ promiseCreated,
+ array._eachValues !== undefined ? "Promise.each" : "Promise.reduce",
+ promise
+ );
+ return ret;
+}
};
-},{"./async.js":2,"./util.js":38}],31:[function(_dereq_,module,exports){
+},{"./util":36}],29:[function(_dereq_,module,exports){
"use strict";
-var schedule;
var util = _dereq_("./util");
+var schedule;
var noAsyncScheduler = function() {
- throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/m3OTXk\u000a");
+ throw new Error("No async scheduler available\u000a\u000a See http://goo.gl/MqrFmX\u000a");
};
if (util.isNode && typeof MutationObserver === "undefined") {
var GlobalSetImmediate = global.setImmediate;
@@ -3987,13 +4336,32 @@ if (util.isNode && typeof MutationObserver === "undefined") {
!(typeof window !== "undefined" &&
window.navigator &&
window.navigator.standalone)) {
- schedule = function(fn) {
+ schedule = (function() {
var div = document.createElement("div");
- var observer = new MutationObserver(fn);
- observer.observe(div, {attributes: true});
- return function() { div.classList.toggle("foo"); };
- };
- schedule.isStatic = true;
+ var opts = {attributes: true};
+ var toggleScheduled = false;
+ var div2 = document.createElement("div");
+ var o2 = new MutationObserver(function() {
+ div.classList.toggle("foo");
+ toggleScheduled = false;
+ });
+ o2.observe(div2, opts);
+
+ var scheduleToggle = function() {
+ if (toggleScheduled) return;
+ toggleScheduled = true;
+ div2.classList.toggle("foo");
+ };
+
+ return function schedule(fn) {
+ var o = new MutationObserver(function() {
+ o.disconnect();
+ fn();
+ });
+ o.observe(div, opts);
+ scheduleToggle();
+ };
+ })();
} else if (typeof setImmediate !== "undefined") {
schedule = function (fn) {
setImmediate(fn);
@@ -4007,12 +4375,12 @@ if (util.isNode && typeof MutationObserver === "undefined") {
}
module.exports = schedule;
-},{"./util":38}],32:[function(_dereq_,module,exports){
+},{"./util":36}],30:[function(_dereq_,module,exports){
"use strict";
module.exports =
- function(Promise, PromiseArray) {
+ function(Promise, PromiseArray, debug) {
var PromiseInspection = Promise.PromiseInspection;
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
function SettledPromiseArray(values) {
this.constructor$(values);
@@ -4024,39 +4392,43 @@ SettledPromiseArray.prototype._promiseResolved = function (index, inspection) {
var totalResolved = ++this._totalResolved;
if (totalResolved >= this._length) {
this._resolve(this._values);
+ return true;
}
+ return false;
};
SettledPromiseArray.prototype._promiseFulfilled = function (value, index) {
var ret = new PromiseInspection();
- ret._bitField = 268435456;
- ret._settledValue = value;
- this._promiseResolved(index, ret);
+ ret._bitField = 33554432;
+ ret._settledValueField = value;
+ return this._promiseResolved(index, ret);
};
SettledPromiseArray.prototype._promiseRejected = function (reason, index) {
var ret = new PromiseInspection();
- ret._bitField = 134217728;
- ret._settledValue = reason;
- this._promiseResolved(index, ret);
+ ret._bitField = 16777216;
+ ret._settledValueField = reason;
+ return this._promiseResolved(index, ret);
};
Promise.settle = function (promises) {
+ debug.deprecated(".settle()", ".reflect()");
return new SettledPromiseArray(promises).promise();
};
Promise.prototype.settle = function () {
- return new SettledPromiseArray(this).promise();
+ return Promise.settle(this);
};
};
-},{"./util.js":38}],33:[function(_dereq_,module,exports){
+},{"./util":36}],31:[function(_dereq_,module,exports){
"use strict";
module.exports =
function(Promise, PromiseArray, apiRejection) {
-var util = _dereq_("./util.js");
-var RangeError = _dereq_("./errors.js").RangeError;
-var AggregateError = _dereq_("./errors.js").AggregateError;
+var util = _dereq_("./util");
+var RangeError = _dereq_("./errors").RangeError;
+var AggregateError = _dereq_("./errors").AggregateError;
var isArray = util.isArray;
+var CANCELLATION = {};
function SomePromiseArray(values) {
@@ -4110,18 +4482,40 @@ SomePromiseArray.prototype._promiseFulfilled = function (value) {
} else {
this._resolve(this._values);
}
+ return true;
}
+ return false;
};
SomePromiseArray.prototype._promiseRejected = function (reason) {
this._addRejected(reason);
+ return this._checkOutcome();
+};
+
+SomePromiseArray.prototype._promiseCancelled = function () {
+ if (this._values instanceof Promise || this._values == null) {
+ return this._cancel();
+ }
+ this._addRejected(CANCELLATION);
+ return this._checkOutcome();
+};
+
+SomePromiseArray.prototype._checkOutcome = function() {
if (this.howMany() > this._canPossiblyFulfill()) {
var e = new AggregateError();
for (var i = this.length(); i < this._values.length; ++i) {
- e.push(this._values[i]);
+ if (this._values[i] !== CANCELLATION) {
+ e.push(this._values[i]);
+ }
}
- this._reject(e);
+ if (e.length > 0) {
+ this._reject(e);
+ } else {
+ this._cancel();
+ }
+ return true;
}
+ return false;
};
SomePromiseArray.prototype._fulfilled = function () {
@@ -4156,7 +4550,7 @@ SomePromiseArray.prototype._resolveEmptyArray = function () {
function some(promises, howMany) {
if ((howMany | 0) !== howMany || howMany < 0) {
- return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/1wAmHx\u000a");
+ return apiRejection("expecting a positive integer\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
var ret = new SomePromiseArray(promises);
var promise = ret.promise();
@@ -4176,142 +4570,151 @@ Promise.prototype.some = function (howMany) {
Promise._SomePromiseArray = SomePromiseArray;
};
-},{"./errors.js":13,"./util.js":38}],34:[function(_dereq_,module,exports){
+},{"./errors":12,"./util":36}],32:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise) {
function PromiseInspection(promise) {
if (promise !== undefined) {
promise = promise._target();
this._bitField = promise._bitField;
- this._settledValue = promise._settledValue;
+ this._settledValueField = promise._isFateSealed()
+ ? promise._settledValue() : undefined;
}
else {
this._bitField = 0;
- this._settledValue = undefined;
+ this._settledValueField = undefined;
}
}
-PromiseInspection.prototype.value = function () {
+PromiseInspection.prototype._settledValue = function() {
+ return this._settledValueField;
+};
+
+var value = PromiseInspection.prototype.value = function () {
if (!this.isFulfilled()) {
- throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/hc1DLj\u000a");
+ throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
- return this._settledValue;
+ return this._settledValue();
};
-PromiseInspection.prototype.error =
+var reason = PromiseInspection.prototype.error =
PromiseInspection.prototype.reason = function () {
if (!this.isRejected()) {
- throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/hPuiwB\u000a");
+ throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/MqrFmX\u000a");
}
- return this._settledValue;
+ return this._settledValue();
};
-PromiseInspection.prototype.isFulfilled =
-Promise.prototype._isFulfilled = function () {
- return (this._bitField & 268435456) > 0;
+var isFulfilled = PromiseInspection.prototype.isFulfilled = function() {
+ return (this._bitField & 33554432) !== 0;
};
-PromiseInspection.prototype.isRejected =
-Promise.prototype._isRejected = function () {
- return (this._bitField & 134217728) > 0;
+var isRejected = PromiseInspection.prototype.isRejected = function () {
+ return (this._bitField & 16777216) !== 0;
};
-PromiseInspection.prototype.isPending =
-Promise.prototype._isPending = function () {
- return (this._bitField & 402653184) === 0;
+var isPending = PromiseInspection.prototype.isPending = function () {
+ return (this._bitField & 50397184) === 0;
};
-PromiseInspection.prototype.isResolved =
-Promise.prototype._isResolved = function () {
- return (this._bitField & 402653184) > 0;
+var isResolved = PromiseInspection.prototype.isResolved = function () {
+ return (this._bitField & 50331648) !== 0;
};
-Promise.prototype.isPending = function() {
- return this._target()._isPending();
+PromiseInspection.prototype.isCancelled =
+Promise.prototype._isCancelled = function() {
+ return (this._bitField & 65536) === 65536;
};
-Promise.prototype.isRejected = function() {
- return this._target()._isRejected();
+Promise.prototype.isCancelled = function() {
+ return this._target()._isCancelled();
};
-Promise.prototype.isFulfilled = function() {
- return this._target()._isFulfilled();
+Promise.prototype.isPending = function() {
+ return isPending.call(this._target());
};
-Promise.prototype.isResolved = function() {
- return this._target()._isResolved();
+Promise.prototype.isRejected = function() {
+ return isRejected.call(this._target());
};
-Promise.prototype._value = function() {
- return this._settledValue;
+Promise.prototype.isFulfilled = function() {
+ return isFulfilled.call(this._target());
};
-Promise.prototype._reason = function() {
- this._unsetRejectionIsUnhandled();
- return this._settledValue;
+Promise.prototype.isResolved = function() {
+ return isResolved.call(this._target());
};
Promise.prototype.value = function() {
- var target = this._target();
- if (!target.isFulfilled()) {
- throw new TypeError("cannot get fulfillment value of a non-fulfilled promise\u000a\u000a See http://goo.gl/hc1DLj\u000a");
- }
- return target._settledValue;
+ return value.call(this._target());
};
Promise.prototype.reason = function() {
var target = this._target();
- if (!target.isRejected()) {
- throw new TypeError("cannot get rejection reason of a non-rejected promise\u000a\u000a See http://goo.gl/hPuiwB\u000a");
- }
target._unsetRejectionIsUnhandled();
- return target._settledValue;
+ return reason.call(target);
+};
+
+Promise.prototype._value = function() {
+ return this._settledValue();
};
+Promise.prototype._reason = function() {
+ this._unsetRejectionIsUnhandled();
+ return this._settledValue();
+};
Promise.PromiseInspection = PromiseInspection;
};
-},{}],35:[function(_dereq_,module,exports){
+},{}],33:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise, INTERNAL) {
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var errorObj = util.errorObj;
var isObject = util.isObject;
function tryConvertToPromise(obj, context) {
if (isObject(obj)) {
- if (obj instanceof Promise) {
- return obj;
- }
- else if (isAnyBluebirdPromise(obj)) {
- var ret = new Promise(INTERNAL);
- obj._then(
- ret._fulfillUnchecked,
- ret._rejectUncheckedCheckError,
- ret._progressUnchecked,
- ret,
- null
- );
- return ret;
- }
- var then = util.tryCatch(getThen)(obj);
+ if (obj instanceof Promise) return obj;
+ var then = getThen(obj);
if (then === errorObj) {
if (context) context._pushContext();
var ret = Promise.reject(then.e);
if (context) context._popContext();
return ret;
} else if (typeof then === "function") {
+ if (isAnyBluebirdPromise(obj)) {
+ var ret = new Promise(INTERNAL);
+ obj._then(
+ ret._fulfill,
+ ret._reject,
+ undefined,
+ ret,
+ null
+ );
+ return ret;
+ }
return doThenable(obj, then, context);
}
}
return obj;
}
-function getThen(obj) {
+function doGetThen(obj) {
return obj.then;
}
+function getThen(obj) {
+ try {
+ return doGetThen(obj);
+ } catch (e) {
+ errorObj.e = e;
+ return errorObj;
+ }
+}
+
var hasProp = {}.hasOwnProperty;
function isAnyBluebirdPromise(obj) {
return hasProp.call(obj, "_promise0");
@@ -4324,78 +4727,71 @@ function doThenable(x, then, context) {
promise._captureStackTrace();
if (context) context._popContext();
var synchronous = true;
- var result = util.tryCatch(then).call(x,
- resolveFromThenable,
- rejectFromThenable,
- progressFromThenable);
+ var result = util.tryCatch(then).call(x, resolve, reject);
synchronous = false;
+
if (promise && result === errorObj) {
promise._rejectCallback(result.e, true, true);
promise = null;
}
- function resolveFromThenable(value) {
+ function resolve(value) {
if (!promise) return;
promise._resolveCallback(value);
promise = null;
}
- function rejectFromThenable(reason) {
+ function reject(reason) {
if (!promise) return;
promise._rejectCallback(reason, synchronous, true);
promise = null;
}
-
- function progressFromThenable(value) {
- if (!promise) return;
- if (typeof promise._progress === "function") {
- promise._progress(value);
- }
- }
return ret;
}
return tryConvertToPromise;
};
-},{"./util.js":38}],36:[function(_dereq_,module,exports){
+},{"./util":36}],34:[function(_dereq_,module,exports){
"use strict";
module.exports = function(Promise, INTERNAL) {
-var util = _dereq_("./util.js");
+var util = _dereq_("./util");
var TimeoutError = Promise.TimeoutError;
-var afterTimeout = function (promise, message) {
+var afterTimeout = function (promise, message, parent) {
if (!promise.isPending()) return;
-
var err;
- if(!util.isPrimitive(message) && (message instanceof Error)) {
- err = message;
- } else {
- if (typeof message !== "string") {
- message = "operation timed out";
+ if (typeof message !== "string") {
+ if (message instanceof Error) {
+ err = message;
+ } else {
+ err = new TimeoutError("operation timed out");
}
+ } else {
err = new TimeoutError(message);
}
util.markAsOriginatingFromRejection(err);
promise._attachExtraTrace(err);
- promise._cancel(err);
+ promise._reject(err);
+ parent.cancel();
};
var afterValue = function(value) { return delay(+this).thenReturn(value); };
-var delay = Promise.delay = function (value, ms) {
- if (ms === undefined) {
- ms = value;
- value = undefined;
- var ret = new Promise(INTERNAL);
- setTimeout(function() { ret._fulfill(); }, ms);
- return ret;
+var delay = Promise.delay = function (ms, value) {
+ var ret;
+ if (value !== undefined) {
+ ret = Promise.resolve(value)
+ ._then(afterValue, null, null, ms, undefined);
+ } else {
+ ret = new Promise(INTERNAL);
+ setTimeout(function() { ret._fulfill(); }, +ms);
}
- ms = +ms;
- return Promise.resolve(value)._then(afterValue, null, null, ms, undefined);
+ ret._setAsyncGuaranteed();
+ return ret;
};
Promise.prototype.delay = function (ms) {
- return delay(this, ms);
+ return delay(ms, this);
};
function successClear(value) {
@@ -4414,35 +4810,25 @@ function failureClear(reason) {
Promise.prototype.timeout = function (ms, message) {
ms = +ms;
- var ret = this.then().cancellable();
- ret._cancellationParent = this;
+ var parent = this.then();
+ var ret = parent.then();
var handle = setTimeout(function timeoutTimeout() {
- afterTimeout(ret, message);
+ afterTimeout(ret, message, parent);
}, ms);
return ret._then(successClear, failureClear, undefined, handle, undefined);
};
};
-},{"./util.js":38}],37:[function(_dereq_,module,exports){
+},{"./util":36}],35:[function(_dereq_,module,exports){
"use strict";
module.exports = function (Promise, apiRejection, tryConvertToPromise,
- createContext) {
- var TypeError = _dereq_("./errors.js").TypeError;
- var inherits = _dereq_("./util.js").inherits;
- var PromiseInspection = Promise.PromiseInspection;
-
- function inspectionMapper(inspections) {
- var len = inspections.length;
- for (var i = 0; i < len; ++i) {
- var inspection = inspections[i];
- if (inspection.isRejected()) {
- return Promise.reject(inspection.error());
- }
- inspections[i] = inspection._settledValue;
- }
- return inspections;
- }
+ createContext, INTERNAL, debug) {
+ var util = _dereq_("./util");
+ var TypeError = _dereq_("./errors").TypeError;
+ var inherits = _dereq_("./util").inherits;
+ var errorObj = util.errorObj;
+ var tryCatch = util.tryCatch;
function thrower(e) {
setTimeout(function(){throw e;}, 0);
@@ -4461,9 +4847,9 @@ module.exports = function (Promise, apiRejection, tryConvertToPromise,
function dispose(resources, inspection) {
var i = 0;
var len = resources.length;
- var ret = Promise.defer();
+ var ret = new Promise(INTERNAL);
function iterator() {
- if (i >= len) return ret.resolve();
+ if (i >= len) return ret._fulfill();
var maybePromise = castPreservingDisposable(resources[i++]);
if (maybePromise instanceof Promise &&
maybePromise._isDisposable()) {
@@ -4482,21 +4868,7 @@ module.exports = function (Promise, apiRejection, tryConvertToPromise,
iterator();
}
iterator();
- return ret.promise;
- }
-
- function disposerSuccess(value) {
- var inspection = new PromiseInspection();
- inspection._settledValue = value;
- inspection._bitField = 268435456;
- return dispose(this, inspection).thenReturn(value);
- }
-
- function disposerFail(reason) {
- var inspection = new PromiseInspection();
- inspection._settledValue = reason;
- inspection._bitField = 134217728;
- return dispose(this, inspection).thenThrow(reason);
+ return ret;
}
function Disposer(data, promise, context) {
@@ -4556,13 +4928,30 @@ module.exports = function (Promise, apiRejection, tryConvertToPromise,
return value;
}
+ function ResourceList(length) {
+ this.length = length;
+ this.promise = null;
+ this[length-1] = null;
+ }
+
+ ResourceList.prototype._resultCancelled = function() {
+ var len = this.length;
+ for (var i = 0; i < len; ++i) {
+ var item = this[i];
+ if (item instanceof Promise) {
+ item.cancel();
+ }
+ }
+ };
+
Promise.using = function () {
var len = arguments.length;
if (len < 2) return apiRejection(
"you must pass at least 2 arguments to Promise.using");
var fn = arguments[len - 1];
- if (typeof fn !== "function") return apiRejection("fn must be a function\u000a\u000a See http://goo.gl/916lJJ\u000a");
-
+ if (typeof fn !== "function") {
+ return apiRejection("expecting a function but got " + util.classString(fn));
+ }
var input;
var spreadArgs = true;
if (len === 2 && Array.isArray(arguments[0])) {
@@ -4573,7 +4962,7 @@ module.exports = function (Promise, apiRejection, tryConvertToPromise,
input = arguments;
len--;
}
- var resources = new Array(len);
+ var resources = new ResourceList(len);
for (var i = 0; i < len; ++i) {
var resource = input[i];
if (Disposer.isDisposer(resource)) {
@@ -4593,32 +4982,51 @@ module.exports = function (Promise, apiRejection, tryConvertToPromise,
resources[i] = resource;
}
- var promise = Promise.settle(resources)
- .then(inspectionMapper)
- .then(function(vals) {
- promise._pushContext();
- var ret;
- try {
- ret = spreadArgs
- ? fn.apply(undefined, vals) : fn.call(undefined, vals);
- } finally {
- promise._popContext();
+ var reflectedResources = new Array(resources.length);
+ for (var i = 0; i < reflectedResources.length; ++i) {
+ reflectedResources[i] = Promise.resolve(resources[i]).reflect();
+ }
+
+ var resultPromise = Promise.all(reflectedResources)
+ .then(function(inspections) {
+ for (var i = 0; i < inspections.length; ++i) {
+ var inspection = inspections[i];
+ if (inspection.isRejected()) {
+ errorObj.e = inspection.error();
+ return errorObj;
+ } else if (!inspection.isFulfilled()) {
+ resultPromise.cancel();
+ return;
+ }
+ inspections[i] = inspection.value();
}
+ promise._pushContext();
+
+ fn = tryCatch(fn);
+ var ret = spreadArgs
+ ? fn.apply(undefined, inspections) : fn(inspections);
+ var promiseCreated = promise._popContext();
+ debug.checkForgottenReturns(
+ ret, promiseCreated, "Promise.using", promise);
return ret;
- })
- ._then(
- disposerSuccess, disposerFail, undefined, resources, undefined);
+ });
+
+ var promise = resultPromise.lastly(function() {
+ var inspection = new Promise.PromiseInspection(resultPromise);
+ return dispose(resources, inspection);
+ });
resources.promise = promise;
+ promise._setOnCancel(resources);
return promise;
};
Promise.prototype._setDisposable = function (disposer) {
- this._bitField = this._bitField | 262144;
+ this._bitField = this._bitField | 131072;
this._disposer = disposer;
};
Promise.prototype._isDisposable = function () {
- return (this._bitField & 262144) > 0;
+ return (this._bitField & 131072) > 0;
};
Promise.prototype._getDisposer = function () {
@@ -4626,7 +5034,7 @@ module.exports = function (Promise, apiRejection, tryConvertToPromise,
};
Promise.prototype._unsetDisposable = function () {
- this._bitField = this._bitField & (~262144);
+ this._bitField = this._bitField & (~131072);
this._disposer = undefined;
};
@@ -4639,25 +5047,10 @@ module.exports = function (Promise, apiRejection, tryConvertToPromise,
};
-},{"./errors.js":13,"./util.js":38}],38:[function(_dereq_,module,exports){
+},{"./errors":12,"./util":36}],36:[function(_dereq_,module,exports){
"use strict";
-var es5 = _dereq_("./es5.js");
+var es5 = _dereq_("./es5");
var canEvaluate = typeof navigator == "undefined";
-var haveGetters = (function(){
- try {
- var o = {};
- es5.defineProperty(o, "f", {
- get: function () {
- return 3;
- }
- });
- return o.f === 3;
- }
- catch (e) {
- return false;
- }
-
-})();
var errorObj = {e: {}};
var tryCatchTarget;
@@ -4703,7 +5096,8 @@ function isPrimitive(val) {
}
function isObject(value) {
- return !isPrimitive(value);
+ return typeof value === "function" ||
+ typeof value === "object" && value !== null;
}
function maybeWrapAsError(maybeError) {
@@ -4844,10 +5238,10 @@ function isClass(fn) {
function toFastProperties(obj) {
/*jshint -W027,-W055,-W031*/
- function f() {}
- f.prototype = obj;
+ function FakeConstructor() {}
+ FakeConstructor.prototype = obj;
var l = 8;
- while (l--) new f();
+ while (l--) new FakeConstructor();
return obj;
eval(obj);
}
@@ -4889,7 +5283,7 @@ function originatesFromRejection(e) {
function canAttachTrace(obj) {
// Added by Zotero
return obj && obj.stack && es5.propertyIsWritable(obj, "stack");
-
+
return obj instanceof Error && es5.propertyIsWritable(obj, "stack");
}
@@ -4924,6 +5318,43 @@ function copyDescriptors(from, to, filter) {
}
}
+var asArray = function(v) {
+ if (es5.isArray(v)) {
+ return v;
+ }
+ return null;
+};
+
+if (typeof Symbol !== "undefined" && Symbol.iterator) {
+ var ArrayFrom = typeof Array.from === "function" ? function(v) {
+ return Array.from(v);
+ } : function(v) {
+ var ret = [];
+ var it = v[Symbol.iterator]();
+ var itResult;
+ while (!((itResult = it.next()).done)) {
+ ret.push(itResult.value);
+ }
+ return ret;
+ };
+
+ asArray = function(v) {
+ if (es5.isArray(v)) {
+ return v;
+ } else if (v != null && typeof v[Symbol.iterator] === "function") {
+ return ArrayFrom(v);
+ }
+ return null;
+ };
+}
+
+var isNode = typeof process !== "undefined" &&
+ classString(process).toLowerCase() === "[object process]";
+
+function env(key, def) {
+ return isNode ? process.env[key] : def;
+}
+
var ret = {
isClass: isClass,
isIdentifier: isIdentifier,
@@ -4931,7 +5362,7 @@ var ret = {
getDataPropertyOrDefault: getDataPropertyOrDefault,
thrower: thrower,
isArray: es5.isArray,
- haveGetters: haveGetters,
+ asArray: asArray,
notEnumerableProp: notEnumerableProp,
isPrimitive: isPrimitive,
isObject: isObject,
@@ -4952,8 +5383,8 @@ var ret = {
copyDescriptors: copyDescriptors,
hasDevTools: typeof chrome !== "undefined" && chrome &&
typeof chrome.loadTimes === "function",
- isNode: typeof process !== "undefined" &&
- classString(process).toLowerCase() === "[object process]"
+ isNode: isNode,
+ env: env
};
ret.isRecentNode = ret.isNode && (function() {
var version = process.versions.node.split(".").map(Number);
@@ -4965,5 +5396,5 @@ if (ret.isNode) ret.toFastProperties(process);
try {throw new Error(); } catch (e) {ret.lastLineError = e;}
module.exports = ret;
-},{"./es5.js":14}]},{},[4])(4)
+},{"./es5":13}]},{},[4])(4)
}); ;if (typeof window !== 'undefined' && window !== null) { window.P = window.Promise; } else if (typeof self !== 'undefined' && self !== null) { self.P = self.Promise; }
\ No newline at end of file