Comparing version 3.2.1 to 3.2.2
@@ -181,13 +181,13 @@ !function(e){"object"==typeof exports?module.exports=e():"function"==typeof define&&define.amd?define(e):"undefined"!=typeof window?window.Promise=e():"undefined"!=typeof global?global.Promise=e():"undefined"!=typeof self&&(self.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 require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
var timer = require('../timer'); | ||
var async = require('../async'); | ||
var logError = (function() { | ||
if(typeof console !== 'undefined') { | ||
if(typeof console.error !== 'undefined') { | ||
return function(e) { | ||
console.error(e); | ||
}; | ||
} | ||
return function unhandledRejection(Promise) { | ||
var logError = (function() { | ||
if(typeof console !== 'undefined') { | ||
if(typeof console.error !== 'undefined') { | ||
return function(e) { | ||
console.error(e); | ||
}; | ||
} | ||
if(typeof console.log !== 'undefined') { | ||
return function(e) { | ||
@@ -197,34 +197,12 @@ console.log(e); | ||
} | ||
} | ||
return noop; | ||
}()); | ||
return noop; | ||
}()); | ||
return function unhandledRejection(Promise, enqueue) { | ||
var unhandledRejections = []; | ||
if(typeof enqueue !== 'function') { | ||
enqueue = function(f) { | ||
timer.set(f, 0); | ||
}; | ||
} | ||
function reportUnhandledRejections() { | ||
unhandledRejections.forEach(function (r) { | ||
if(!r.handled) { | ||
logError('Potentially unhandled rejection ' + formatError(r.value)); | ||
} | ||
}); | ||
unhandledRejections = []; | ||
} | ||
Promise.onPotentiallyUnhandledRejection = function(rejection) { | ||
if(unhandledRejections.length === 0) { | ||
enqueue(reportUnhandledRejections); | ||
} | ||
unhandledRejections.push(rejection); | ||
logError('Potentially unhandled rejection ' + formatError(rejection.value)); | ||
}; | ||
Promise.onFatalRejection = function(rejection) { | ||
enqueue(function() { | ||
async(function() { | ||
throw rejection.value; | ||
@@ -244,3 +222,3 @@ }); | ||
if(s === '[object Object]' && typeof JSON !== 'undefined') { | ||
s = JSON.stringify(e); | ||
s = tryStringify(e, s); | ||
} | ||
@@ -250,3 +228,11 @@ } | ||
return e instanceof Error ? s : s + ' (WARNING: non-Error used)'; | ||
} | ||
function tryStringify(e, defaultValue) { | ||
try { | ||
return JSON.stringify(e); | ||
} catch(e) { | ||
// Ignore. Cannot JSON.stringify e, stick with String(e) | ||
return defaultValue; | ||
} | ||
} | ||
@@ -259,3 +245,3 @@ | ||
},{"../timer":8}],6:[function(require,module,exports){ | ||
},{"../async":4}],6:[function(require,module,exports){ | ||
/** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
@@ -285,5 +271,6 @@ /** @author Brian Cavalier */ | ||
*/ | ||
function Promise(resolver) { | ||
this._handler = arguments.length === 0 | ||
? foreverPendingHandler : init(resolver); | ||
function Promise(resolver, handler) { | ||
this._handler = resolver === Handler ? handler : init(resolver); | ||
// this._handler = arguments.length === 0 | ||
// ? foreverPendingHandler : init(resolver); | ||
} | ||
@@ -349,3 +336,3 @@ | ||
return x instanceof Promise ? x | ||
: promiseFromHandler(new AsyncHandler(getHandlerUnchecked(x))); | ||
: new Promise(Handler, new AsyncHandler(getHandler(x))); | ||
} | ||
@@ -359,3 +346,3 @@ | ||
function reject(x) { | ||
return promiseFromHandler(new AsyncHandler(new RejectedHandler(x))); | ||
return new Promise(Handler, new AsyncHandler(new RejectedHandler(x))); | ||
} | ||
@@ -377,20 +364,5 @@ | ||
function defer() { | ||
return promiseFromHandler(new DeferredHandler()); | ||
return new Promise(Handler, new DeferredHandler()); | ||
} | ||
/** | ||
* Create a new promise with the supplied handler | ||
* @private | ||
* @param {object} handler | ||
* @returns {Promise} | ||
*/ | ||
function promiseFromHandler(handler) { | ||
return configurePromise(handler, new Promise()); | ||
} | ||
function configurePromise(handler, p) { | ||
p._handler = handler; | ||
return p; | ||
} | ||
// Transformation and flow control | ||
@@ -413,3 +385,3 @@ | ||
// Short circuit: value will not change, simply share handler | ||
return promiseFromHandler(parent); | ||
return new Promise(Handler, parent); | ||
} | ||
@@ -451,3 +423,3 @@ | ||
Promise.prototype._bindContext = function(thisArg) { | ||
return promiseFromHandler(new BoundHandler(this._handler, thisArg)); | ||
return new Promise(Handler, new BoundHandler(this._handler, thisArg)); | ||
}; | ||
@@ -461,6 +433,5 @@ | ||
Promise.prototype._beget = function() { | ||
var p = new this.constructor(); | ||
var parent = this._handler; | ||
var child = new DeferredHandler(parent.receiver, parent.join().context); | ||
return configurePromise(child, p); | ||
return new this.constructor(Handler, child); | ||
}; | ||
@@ -478,7 +449,7 @@ | ||
var handler = getHandlerUnchecked(x); | ||
handler.context = this._handler.context; | ||
handler.chain(handler, void 0, function() { | ||
this._fatal(this.context); | ||
}); | ||
var handler = getHandler(x); | ||
var context = this._handler.context; | ||
handler.catchError(function() { | ||
this._fatal(context); | ||
}, handler); | ||
}; | ||
@@ -501,8 +472,7 @@ | ||
var resolver = new DeferredHandler(); | ||
var len = promises.length >>> 0; | ||
var pending = len; | ||
var results = []; | ||
var pending = promises.length >>> 0; | ||
var results = new Array(pending); | ||
var i, h, x; | ||
for (i = 0; i < len; ++i) { | ||
for (i = 0; i < promises.length; ++i) { | ||
x = promises[i]; | ||
@@ -526,3 +496,3 @@ | ||
} else { | ||
h.chain(resolver, void 0, resolver.reject); | ||
h.catchError(resolver.reject, resolver); | ||
break; | ||
@@ -541,6 +511,5 @@ } | ||
return promiseFromHandler(resolver); | ||
return new Promise(Handler, resolver); | ||
function resolveOne(resolver, results, handler, i) { | ||
handler.chain(resolver, function(x) { | ||
handler.map(function(x) { | ||
results[i] = x; | ||
@@ -550,3 +519,3 @@ if(--pending === 0) { | ||
} | ||
}, resolver.reject, resolver.notify); | ||
}, resolver); | ||
} | ||
@@ -584,3 +553,3 @@ } | ||
} | ||
return promiseFromHandler(h); | ||
return new Promise(Handler, h); | ||
} | ||
@@ -591,18 +560,2 @@ | ||
/** | ||
* Get an appropriate handler for x, checking for untrusted thenables | ||
* and promise graph cycles. | ||
* @private | ||
* @param {*} x | ||
* @param {object?} h optional handler to check for cycles | ||
* @returns {object} handler | ||
*/ | ||
function getHandler(x, h) { | ||
if(x instanceof Promise) { | ||
var xh = x._handler.join(); | ||
return h === xh ? promiseCycleHandler() : xh; | ||
} | ||
return maybeThenable(x) ? getHandlerUntrusted(x) : new FulfilledHandler(x); | ||
} | ||
/** | ||
* Get an appropriate handler for x, without checking for cycles | ||
@@ -613,3 +566,3 @@ * @private | ||
*/ | ||
function getHandlerUnchecked(x) { | ||
function getHandler(x) { | ||
if(x instanceof Promise) { | ||
@@ -638,15 +591,2 @@ return x._handler.join(); | ||
/** | ||
* Recursively collapse handler chain to find the handler | ||
* nearest to the fully resolved value. | ||
* @param {Handler} h | ||
* @returns {*} | ||
*/ | ||
function join(h) { | ||
while(h.handler !== void 0) { | ||
h = h.handler; | ||
} | ||
return h; | ||
} | ||
/** | ||
* Handler for a promise that is pending forever | ||
@@ -665,4 +605,4 @@ * @private | ||
= Handler.prototype._fatal | ||
= Handler.prototype._removeTrace | ||
= Handler.prototype._reportTrace | ||
= Handler.prototype._unreport | ||
= Handler.prototype._report | ||
= noop; | ||
@@ -672,3 +612,14 @@ | ||
Handler.prototype.join = function() { return join(this); }; | ||
/** | ||
* Recursively collapse handler chain to find the handler | ||
* nearest to the fully resolved value. | ||
* @returns {object} handler nearest the fully resolved value | ||
*/ | ||
Handler.prototype.join = function() { | ||
var h = this; | ||
while(h.handler !== void 0) { | ||
h = h.handler; | ||
} | ||
return h; | ||
}; | ||
@@ -687,8 +638,16 @@ Handler.prototype.chain = function(to, fulfilled, rejected, progress) { | ||
Handler.prototype.map = function(f, to) { | ||
this.chain(to, f, to.reject, to.notify); | ||
}; | ||
Handler.prototype.catchError = function(f, to) { | ||
this.chain(to, to.resolve, f, to.notify); | ||
}; | ||
Handler.prototype.fold = function(to, f, z) { | ||
join(this).chain(to, function(x) { | ||
getHandler(z).chain(this, function(z) { | ||
this.join().map(function(x) { | ||
getHandler(z).map(function(z) { | ||
this.resolve(tryCatchReject2(f, z, x, this.receiver)); | ||
}, this.reject, this.notify); | ||
}, to.reject, to.notify); | ||
}, this); | ||
}, to); | ||
}; | ||
@@ -704,3 +663,3 @@ | ||
this.consumers = []; | ||
this.consumers = void 0; | ||
this.receiver = receiver; | ||
@@ -720,3 +679,3 @@ this.handler = void 0; | ||
if(!this.resolved) { | ||
this._resolve(getHandler(x, this)); | ||
this.become(getHandler(x)); | ||
} | ||
@@ -727,3 +686,3 @@ }; | ||
if(!this.resolved) { | ||
this._resolve(new RejectedHandler(x)); | ||
this.become(new RejectedHandler(x)); | ||
} | ||
@@ -734,3 +693,10 @@ }; | ||
if (this.resolved) { | ||
return this.handler = join(this.handler); | ||
var h = this; | ||
while(h.handler !== void 0) { | ||
h = h.handler; | ||
if(h === this) { | ||
return this.handler = new Cycle(); | ||
} | ||
} | ||
return h; | ||
} else { | ||
@@ -743,3 +709,3 @@ return this; | ||
var q = this.consumers; | ||
var handler = this.handler.join(); | ||
var handler = this.join(); | ||
this.consumers = void 0; | ||
@@ -752,9 +718,11 @@ | ||
DeferredHandler.prototype._resolve = function(handler) { | ||
DeferredHandler.prototype.become = function(handler) { | ||
this.resolved = true; | ||
this.handler = handler; | ||
tasks.enqueue(this); | ||
if(this.consumers !== void 0) { | ||
tasks.enqueue(this); | ||
} | ||
if(this.context !== void 0) { | ||
handler._reportTrace(this.context); | ||
handler._report(this.context); | ||
} | ||
@@ -767,3 +735,7 @@ }; | ||
} else { | ||
this.consumers.push(continuation); | ||
if(this.consumers === void 0) { | ||
this.consumers = [continuation]; | ||
} else { | ||
this.consumers.push(continuation); | ||
} | ||
} | ||
@@ -774,12 +746,12 @@ }; | ||
if(!this.resolved) { | ||
tasks.enqueue(new ProgressTask(this.consumers, x)); | ||
tasks.enqueue(new ProgressTask(this, x)); | ||
} | ||
}; | ||
DeferredHandler.prototype._reportTrace = function(context) { | ||
this.resolved && this.handler.join()._reportTrace(context); | ||
DeferredHandler.prototype._report = function(context) { | ||
this.resolved && this.handler.join()._report(context); | ||
}; | ||
DeferredHandler.prototype._removeTrace = function() { | ||
this.resolved && this.handler.join()._removeTrace(); | ||
DeferredHandler.prototype._unreport = function() { | ||
this.resolved && this.handler.join()._unreport(); | ||
}; | ||
@@ -809,8 +781,8 @@ | ||
DelegateHandler.prototype._reportTrace = function(context) { | ||
this.join()._reportTrace(context); | ||
DelegateHandler.prototype._report = function(context) { | ||
this.join()._report(context); | ||
}; | ||
DelegateHandler.prototype._removeTrace = function() { | ||
this.join()._removeTrace(); | ||
DelegateHandler.prototype._unreport = function() { | ||
this.join()._unreport(); | ||
}; | ||
@@ -942,6 +914,6 @@ | ||
this.value = x; | ||
this.state = -1; | ||
this.state = -1; // -1: rejected, -2: rejected and reported | ||
this.handled = false; | ||
this._reportTrace(); | ||
this._report(); | ||
} | ||
@@ -959,3 +931,3 @@ | ||
if (typeof cont.rejected === 'function') { | ||
this._removeTrace(); | ||
this._unreport(); | ||
Promise.enterContext(this); | ||
@@ -965,3 +937,3 @@ x = tryCatchReject(cont.rejected, this.value, cont.receiver); | ||
} else { | ||
x = promiseFromHandler(this); | ||
x = new Promise(Handler, this); | ||
} | ||
@@ -973,9 +945,9 @@ | ||
RejectedHandler.prototype._reportTrace = function(context) { | ||
Promise.onPotentiallyUnhandledRejection(this, context); | ||
RejectedHandler.prototype._report = function(context) { | ||
tasks.afterQueue(reportUnhandled, this, context); | ||
}; | ||
RejectedHandler.prototype._removeTrace = function() { | ||
RejectedHandler.prototype._unreport = function() { | ||
this.handled = true; | ||
Promise.onPotentiallyUnhandledRejectionHandled(this); | ||
tasks.afterQueue(reportHandled, this); | ||
}; | ||
@@ -987,2 +959,15 @@ | ||
function reportUnhandled(rejection, context) { | ||
if(!rejection.handled) { | ||
rejection.state = -2; | ||
Promise.onPotentiallyUnhandledRejection(rejection, context); | ||
} | ||
} | ||
function reportHandled(rejection) { | ||
if(rejection.state === -2) { | ||
Promise.onPotentiallyUnhandledRejectionHandled(rejection); | ||
} | ||
} | ||
// Unhandled rejection hooks | ||
@@ -1003,8 +988,10 @@ // By default, everything is a noop | ||
var foreverPendingHandler = new Handler(); | ||
var foreverPendingPromise = promiseFromHandler(foreverPendingHandler); | ||
var foreverPendingPromise = new Promise(Handler, foreverPendingHandler); | ||
function promiseCycleHandler() { | ||
return new RejectedHandler(new TypeError('Promise cycle')); | ||
function Cycle() { | ||
RejectedHandler.call(this, new TypeError('Promise cycle')); | ||
} | ||
inherit(RejectedHandler, Cycle); | ||
// Snapshot states | ||
@@ -1042,4 +1029,4 @@ | ||
*/ | ||
function ProgressTask(q, value) { | ||
this.q = q; | ||
function ProgressTask(handler, value) { | ||
this.handler = handler; | ||
this.value = value; | ||
@@ -1049,3 +1036,6 @@ } | ||
ProgressTask.prototype.run = function() { | ||
var q = this.q; | ||
var q = this.handler.consumers; | ||
if(q === void 0) { | ||
return; | ||
} | ||
// First progress handler is at index 1 | ||
@@ -1140,6 +1130,8 @@ for (var i = 0; i < q.length; ++i) { | ||
this._handlerQueue = new Queue(15); | ||
this._afterQueue = new Queue(5); | ||
this._running = false; | ||
var self = this; | ||
this.drainQueue = function() { | ||
self._drainQueue(); | ||
this.drain = function() { | ||
self._drain(); | ||
}; | ||
@@ -1154,8 +1146,19 @@ } | ||
Scheduler.prototype.enqueue = function(task) { | ||
if(this._handlerQueue.length === 0) { | ||
this._enqueue(this.drainQueue); | ||
this._handlerQueue.push(task); | ||
if(!this._running) { | ||
this._running = true; | ||
this._enqueue(this.drain); | ||
} | ||
this._handlerQueue.push(task); | ||
}; | ||
Scheduler.prototype.afterQueue = function(f, x, y) { | ||
this._afterQueue.push(f); | ||
this._afterQueue.push(x); | ||
this._afterQueue.push(y); | ||
if(!this._running) { | ||
this._running = true; | ||
this._enqueue(this.drain); | ||
} | ||
}; | ||
/** | ||
@@ -1166,3 +1169,3 @@ * Drain the handler queue entirely, being careful to allow the | ||
*/ | ||
Scheduler.prototype._drainQueue = function() { | ||
Scheduler.prototype._drain = function() { | ||
var q = this._handlerQueue; | ||
@@ -1172,2 +1175,9 @@ while(q.length > 0) { | ||
} | ||
q = this._afterQueue; | ||
while(q.length > 0) { | ||
q.shift()(q.shift(), q.shift()); | ||
} | ||
this._running = false; | ||
}; | ||
@@ -1180,34 +1190,5 @@ | ||
},{"./Queue":3}],8:[function(require,module,exports){ | ||
/** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
/** @author Brian Cavalier */ | ||
/** @author John Hann */ | ||
(function(define) { 'use strict'; | ||
define(function(require) { | ||
/*global setTimeout,clearTimeout*/ | ||
var cjsRequire, vertx, setTimer, clearTimer; | ||
cjsRequire = require; | ||
try { | ||
vertx = cjsRequire('vertx'); | ||
setTimer = function (f, ms) { return vertx.setTimer(ms, f); }; | ||
clearTimer = vertx.cancelTimer; | ||
} catch (e) { | ||
setTimer = function(f, ms) { return setTimeout(f, ms); }; | ||
clearTimer = function(t) { return clearTimeout(t); }; | ||
} | ||
return { | ||
set: setTimer, | ||
clear: clearTimer | ||
}; | ||
}); | ||
}(typeof define === 'function' && define.amd ? define : function(factory) { module.exports = factory(require); })); | ||
},{}]},{},[1]) | ||
},{"./Queue":3}]},{},[1]) | ||
(1) | ||
}); | ||
; |
@@ -12,3 +12,2 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var origCatch = Promise.prototype['catch']; | ||
var nil = Promise.nil; | ||
@@ -27,3 +26,3 @@ /** | ||
h.when({ resolve: this._maybeFatal, notify: noop, context: this, | ||
receiver: h.receiver, arg: nil, fulfilled: onResult, rejected: onError, | ||
receiver: h.receiver, fulfilled: onResult, rejected: onError, | ||
progress: void 0 }); | ||
@@ -30,0 +29,0 @@ }; |
@@ -22,5 +22,5 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
this._handler.chain(h, function delay(x) { | ||
timer.set(function() { h.resolve(x); }, ms); | ||
}, h.reject, h.notify); | ||
this._handler.map(function delay(x) { | ||
timer.set(function() { h.resolve(x); }, ms); | ||
}, h); | ||
@@ -27,0 +27,0 @@ return p; |
@@ -8,13 +8,13 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
var timer = require('../timer'); | ||
var async = require('../async'); | ||
var logError = (function() { | ||
if(typeof console !== 'undefined') { | ||
if(typeof console.error !== 'undefined') { | ||
return function(e) { | ||
console.error(e); | ||
}; | ||
} | ||
return function unhandledRejection(Promise) { | ||
var logError = (function() { | ||
if(typeof console !== 'undefined') { | ||
if(typeof console.error !== 'undefined') { | ||
return function(e) { | ||
console.error(e); | ||
}; | ||
} | ||
if(typeof console.log !== 'undefined') { | ||
return function(e) { | ||
@@ -24,34 +24,12 @@ console.log(e); | ||
} | ||
} | ||
return noop; | ||
}()); | ||
return noop; | ||
}()); | ||
return function unhandledRejection(Promise, enqueue) { | ||
var unhandledRejections = []; | ||
if(typeof enqueue !== 'function') { | ||
enqueue = function(f) { | ||
timer.set(f, 0); | ||
}; | ||
} | ||
function reportUnhandledRejections() { | ||
unhandledRejections.forEach(function (r) { | ||
if(!r.handled) { | ||
logError('Potentially unhandled rejection ' + formatError(r.value)); | ||
} | ||
}); | ||
unhandledRejections = []; | ||
} | ||
Promise.onPotentiallyUnhandledRejection = function(rejection) { | ||
if(unhandledRejections.length === 0) { | ||
enqueue(reportUnhandledRejections); | ||
} | ||
unhandledRejections.push(rejection); | ||
logError('Potentially unhandled rejection ' + formatError(rejection.value)); | ||
}; | ||
Promise.onFatalRejection = function(rejection) { | ||
enqueue(function() { | ||
async(function() { | ||
throw rejection.value; | ||
@@ -58,0 +36,0 @@ }); |
@@ -25,5 +25,6 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
*/ | ||
function Promise(resolver) { | ||
this._handler = arguments.length === 0 | ||
? foreverPendingHandler : init(resolver); | ||
function Promise(resolver, handler) { | ||
this._handler = resolver === Handler ? handler : init(resolver); | ||
// this._handler = arguments.length === 0 | ||
// ? foreverPendingHandler : init(resolver); | ||
} | ||
@@ -89,3 +90,3 @@ | ||
return x instanceof Promise ? x | ||
: promiseFromHandler(new AsyncHandler(getHandlerUnchecked(x))); | ||
: new Promise(Handler, new AsyncHandler(getHandler(x))); | ||
} | ||
@@ -99,3 +100,3 @@ | ||
function reject(x) { | ||
return promiseFromHandler(new AsyncHandler(new RejectedHandler(x))); | ||
return new Promise(Handler, new AsyncHandler(new RejectedHandler(x))); | ||
} | ||
@@ -117,20 +118,5 @@ | ||
function defer() { | ||
return promiseFromHandler(new DeferredHandler()); | ||
return new Promise(Handler, new DeferredHandler()); | ||
} | ||
/** | ||
* Create a new promise with the supplied handler | ||
* @private | ||
* @param {object} handler | ||
* @returns {Promise} | ||
*/ | ||
function promiseFromHandler(handler) { | ||
return configurePromise(handler, new Promise()); | ||
} | ||
function configurePromise(handler, p) { | ||
p._handler = handler; | ||
return p; | ||
} | ||
// Transformation and flow control | ||
@@ -153,3 +139,3 @@ | ||
// Short circuit: value will not change, simply share handler | ||
return promiseFromHandler(parent); | ||
return new Promise(Handler, parent); | ||
} | ||
@@ -191,3 +177,3 @@ | ||
Promise.prototype._bindContext = function(thisArg) { | ||
return promiseFromHandler(new BoundHandler(this._handler, thisArg)); | ||
return new Promise(Handler, new BoundHandler(this._handler, thisArg)); | ||
}; | ||
@@ -201,6 +187,5 @@ | ||
Promise.prototype._beget = function() { | ||
var p = new this.constructor(); | ||
var parent = this._handler; | ||
var child = new DeferredHandler(parent.receiver, parent.join().context); | ||
return configurePromise(child, p); | ||
return new this.constructor(Handler, child); | ||
}; | ||
@@ -218,7 +203,7 @@ | ||
var handler = getHandlerUnchecked(x); | ||
handler.context = this._handler.context; | ||
handler.chain(handler, void 0, function() { | ||
this._fatal(this.context); | ||
}); | ||
var handler = getHandler(x); | ||
var context = this._handler.context; | ||
handler.catchError(function() { | ||
this._fatal(context); | ||
}, handler); | ||
}; | ||
@@ -264,3 +249,3 @@ | ||
} else { | ||
resolver.reject(h.value); | ||
h.catchError(resolver.reject, resolver); | ||
break; | ||
@@ -279,6 +264,5 @@ } | ||
return promiseFromHandler(resolver); | ||
return new Promise(Handler, resolver); | ||
function resolveOne(resolver, results, handler, i) { | ||
handler.chain(resolver, function(x) { | ||
handler.map(function(x) { | ||
results[i] = x; | ||
@@ -288,3 +272,3 @@ if(--pending === 0) { | ||
} | ||
}, resolver.reject, resolver.notify); | ||
}, resolver); | ||
} | ||
@@ -322,3 +306,3 @@ } | ||
} | ||
return promiseFromHandler(h); | ||
return new Promise(Handler, h); | ||
} | ||
@@ -329,18 +313,2 @@ | ||
/** | ||
* Get an appropriate handler for x, checking for untrusted thenables | ||
* and promise graph cycles. | ||
* @private | ||
* @param {*} x | ||
* @param {object?} h optional handler to check for cycles | ||
* @returns {object} handler | ||
*/ | ||
function getHandler(x, h) { | ||
if(x instanceof Promise) { | ||
var xh = x._handler.join(); | ||
return h === xh ? promiseCycleHandler() : xh; | ||
} | ||
return maybeThenable(x) ? getHandlerUntrusted(x) : new FulfilledHandler(x); | ||
} | ||
/** | ||
* Get an appropriate handler for x, without checking for cycles | ||
@@ -351,3 +319,3 @@ * @private | ||
*/ | ||
function getHandlerUnchecked(x) { | ||
function getHandler(x) { | ||
if(x instanceof Promise) { | ||
@@ -376,15 +344,2 @@ return x._handler.join(); | ||
/** | ||
* Recursively collapse handler chain to find the handler | ||
* nearest to the fully resolved value. | ||
* @param {Handler} h | ||
* @returns {*} | ||
*/ | ||
function join(h) { | ||
while(h.handler !== void 0) { | ||
h = h.handler; | ||
} | ||
return h; | ||
} | ||
/** | ||
* Handler for a promise that is pending forever | ||
@@ -403,4 +358,4 @@ * @private | ||
= Handler.prototype._fatal | ||
= Handler.prototype._removeTrace | ||
= Handler.prototype._reportTrace | ||
= Handler.prototype._unreport | ||
= Handler.prototype._report | ||
= noop; | ||
@@ -410,3 +365,14 @@ | ||
Handler.prototype.join = function() { return join(this); }; | ||
/** | ||
* Recursively collapse handler chain to find the handler | ||
* nearest to the fully resolved value. | ||
* @returns {object} handler nearest the fully resolved value | ||
*/ | ||
Handler.prototype.join = function() { | ||
var h = this; | ||
while(h.handler !== void 0) { | ||
h = h.handler; | ||
} | ||
return h; | ||
}; | ||
@@ -425,8 +391,16 @@ Handler.prototype.chain = function(to, fulfilled, rejected, progress) { | ||
Handler.prototype.map = function(f, to) { | ||
this.chain(to, f, to.reject, to.notify); | ||
}; | ||
Handler.prototype.catchError = function(f, to) { | ||
this.chain(to, to.resolve, f, to.notify); | ||
}; | ||
Handler.prototype.fold = function(to, f, z) { | ||
join(this).chain(to, function(x) { | ||
getHandler(z).chain(this, function(z) { | ||
this.join().map(function(x) { | ||
getHandler(z).map(function(z) { | ||
this.resolve(tryCatchReject2(f, z, x, this.receiver)); | ||
}, this.reject, this.notify); | ||
}, to.reject, to.notify); | ||
}, this); | ||
}, to); | ||
}; | ||
@@ -442,3 +416,3 @@ | ||
this.consumers = []; | ||
this.consumers = void 0; | ||
this.receiver = receiver; | ||
@@ -458,3 +432,3 @@ this.handler = void 0; | ||
if(!this.resolved) { | ||
this._resolve(getHandler(x, this)); | ||
this.become(getHandler(x)); | ||
} | ||
@@ -465,3 +439,3 @@ }; | ||
if(!this.resolved) { | ||
this._resolve(new RejectedHandler(x)); | ||
this.become(new RejectedHandler(x)); | ||
} | ||
@@ -472,3 +446,10 @@ }; | ||
if (this.resolved) { | ||
return this.handler = join(this.handler); | ||
var h = this; | ||
while(h.handler !== void 0) { | ||
h = h.handler; | ||
if(h === this) { | ||
return this.handler = new Cycle(); | ||
} | ||
} | ||
return h; | ||
} else { | ||
@@ -481,3 +462,3 @@ return this; | ||
var q = this.consumers; | ||
var handler = this.handler.join(); | ||
var handler = this.join(); | ||
this.consumers = void 0; | ||
@@ -490,9 +471,11 @@ | ||
DeferredHandler.prototype._resolve = function(handler) { | ||
DeferredHandler.prototype.become = function(handler) { | ||
this.resolved = true; | ||
this.handler = handler; | ||
tasks.enqueue(this); | ||
if(this.consumers !== void 0) { | ||
tasks.enqueue(this); | ||
} | ||
if(this.context !== void 0) { | ||
handler._reportTrace(this.context); | ||
handler._report(this.context); | ||
} | ||
@@ -505,3 +488,7 @@ }; | ||
} else { | ||
this.consumers.push(continuation); | ||
if(this.consumers === void 0) { | ||
this.consumers = [continuation]; | ||
} else { | ||
this.consumers.push(continuation); | ||
} | ||
} | ||
@@ -512,12 +499,12 @@ }; | ||
if(!this.resolved) { | ||
tasks.enqueue(new ProgressTask(this.consumers, x)); | ||
tasks.enqueue(new ProgressTask(this, x)); | ||
} | ||
}; | ||
DeferredHandler.prototype._reportTrace = function(context) { | ||
this.resolved && this.handler.join()._reportTrace(context); | ||
DeferredHandler.prototype._report = function(context) { | ||
this.resolved && this.handler.join()._report(context); | ||
}; | ||
DeferredHandler.prototype._removeTrace = function() { | ||
this.resolved && this.handler.join()._removeTrace(); | ||
DeferredHandler.prototype._unreport = function() { | ||
this.resolved && this.handler.join()._unreport(); | ||
}; | ||
@@ -547,8 +534,8 @@ | ||
DelegateHandler.prototype._reportTrace = function(context) { | ||
this.join()._reportTrace(context); | ||
DelegateHandler.prototype._report = function(context) { | ||
this.join()._report(context); | ||
}; | ||
DelegateHandler.prototype._removeTrace = function() { | ||
this.join()._removeTrace(); | ||
DelegateHandler.prototype._unreport = function() { | ||
this.join()._unreport(); | ||
}; | ||
@@ -680,6 +667,6 @@ | ||
this.value = x; | ||
this.state = -1; | ||
this.state = -1; // -1: rejected, -2: rejected and reported | ||
this.handled = false; | ||
this._reportTrace(); | ||
this._report(); | ||
} | ||
@@ -697,3 +684,3 @@ | ||
if (typeof cont.rejected === 'function') { | ||
this._removeTrace(); | ||
this._unreport(); | ||
Promise.enterContext(this); | ||
@@ -703,3 +690,3 @@ x = tryCatchReject(cont.rejected, this.value, cont.receiver); | ||
} else { | ||
x = promiseFromHandler(this); | ||
x = new Promise(Handler, this); | ||
} | ||
@@ -711,9 +698,9 @@ | ||
RejectedHandler.prototype._reportTrace = function(context) { | ||
Promise.onPotentiallyUnhandledRejection(this, context); | ||
RejectedHandler.prototype._report = function(context) { | ||
tasks.afterQueue(reportUnhandled, this, context); | ||
}; | ||
RejectedHandler.prototype._removeTrace = function() { | ||
RejectedHandler.prototype._unreport = function() { | ||
this.handled = true; | ||
Promise.onPotentiallyUnhandledRejectionHandled(this); | ||
tasks.afterQueue(reportHandled, this); | ||
}; | ||
@@ -725,2 +712,15 @@ | ||
function reportUnhandled(rejection, context) { | ||
if(!rejection.handled) { | ||
rejection.state = -2; | ||
Promise.onPotentiallyUnhandledRejection(rejection, context); | ||
} | ||
} | ||
function reportHandled(rejection) { | ||
if(rejection.state === -2) { | ||
Promise.onPotentiallyUnhandledRejectionHandled(rejection); | ||
} | ||
} | ||
// Unhandled rejection hooks | ||
@@ -741,8 +741,10 @@ // By default, everything is a noop | ||
var foreverPendingHandler = new Handler(); | ||
var foreverPendingPromise = promiseFromHandler(foreverPendingHandler); | ||
var foreverPendingPromise = new Promise(Handler, foreverPendingHandler); | ||
function promiseCycleHandler() { | ||
return new RejectedHandler(new TypeError('Promise cycle')); | ||
function Cycle() { | ||
RejectedHandler.call(this, new TypeError('Promise cycle')); | ||
} | ||
inherit(RejectedHandler, Cycle); | ||
// Snapshot states | ||
@@ -780,4 +782,4 @@ | ||
*/ | ||
function ProgressTask(q, value) { | ||
this.q = q; | ||
function ProgressTask(handler, value) { | ||
this.handler = handler; | ||
this.value = value; | ||
@@ -787,3 +789,6 @@ } | ||
ProgressTask.prototype.run = function() { | ||
var q = this.q; | ||
var q = this.handler.consumers; | ||
if(q === void 0) { | ||
return; | ||
} | ||
// First progress handler is at index 1 | ||
@@ -790,0 +795,0 @@ for (var i = 0; i < q.length; ++i) { |
@@ -16,6 +16,8 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
this._handlerQueue = new Queue(15); | ||
this._afterQueue = new Queue(5); | ||
this._running = false; | ||
var self = this; | ||
this.drainQueue = function() { | ||
self._drainQueue(); | ||
this.drain = function() { | ||
self._drain(); | ||
}; | ||
@@ -30,8 +32,19 @@ } | ||
Scheduler.prototype.enqueue = function(task) { | ||
if(this._handlerQueue.length === 0) { | ||
this._enqueue(this.drainQueue); | ||
this._handlerQueue.push(task); | ||
if(!this._running) { | ||
this._running = true; | ||
this._enqueue(this.drain); | ||
} | ||
this._handlerQueue.push(task); | ||
}; | ||
Scheduler.prototype.afterQueue = function(f, x, y) { | ||
this._afterQueue.push(f); | ||
this._afterQueue.push(x); | ||
this._afterQueue.push(y); | ||
if(!this._running) { | ||
this._running = true; | ||
this._enqueue(this.drain); | ||
} | ||
}; | ||
/** | ||
@@ -42,3 +55,3 @@ * Drain the handler queue entirely, being careful to allow the | ||
*/ | ||
Scheduler.prototype._drainQueue = function() { | ||
Scheduler.prototype._drain = function() { | ||
var q = this._handlerQueue; | ||
@@ -48,2 +61,9 @@ while(q.length > 0) { | ||
} | ||
q = this._afterQueue; | ||
while(q.length > 0) { | ||
q.shift()(q.shift(), q.shift()); | ||
} | ||
this._running = false; | ||
}; | ||
@@ -50,0 +70,0 @@ |
{ | ||
"name": "when", | ||
"version": "3.2.1", | ||
"version": "3.2.2", | ||
"description": "A lightweight Promises/A+ and when() implementation, plus other async goodies.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -8,3 +8,3 @@ /** @license MIT License (c) copyright 2010-2014 original author or authors */ | ||
* @author John Hann | ||
* @version 3.2.1 | ||
* @version 3.2.2 | ||
*/ | ||
@@ -11,0 +11,0 @@ (function(define) { 'use strict'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
140083
4164