Skip to content

Commit

Permalink
add handlers for unhandled paths in order to emit destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
omsmith committed Jan 25, 2016
1 parent 5dd16fb commit 7d7dbfb
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions patches/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ module.exports = function patchPromise() {
const oldThen = Promise.prototype.then;
Promise.prototype.then = wrappedThen;

function makeWrappedHandler(fn, handle, uid) {
if ('function' !== typeof fn) return fn;
function makeWrappedHandler(fn, handle, uid, isOnFulfilled) {
if ('function' !== typeof fn) {
return isOnFulfilled
? makeUnhandledResolutionHandler(uid)
: makeUnhandledRejectionHandler(uid);
}

return function wrappedHandler() {
hooks.pre.call(handle);
Expand All @@ -29,6 +33,20 @@ module.exports = function patchPromise() {
};
}

function makeUnhandledResolutionHandler(uid) {
return function unhandledResolutionHandler(val) {
hooks.destroy.call(null, uid);
return val;
};
}

function makeUnhandledRejectionHandler(uid) {
return function unhandledRejectedHandler(val) {
hooks.destroy.call(null, uid);
throw val;
};
}

function wrappedThen(onFulfilled, onRejected) {
if (!state.enabled) return oldThen.call(this, onFulfilled, onRejected);

Expand All @@ -39,8 +57,8 @@ module.exports = function patchPromise() {

return oldThen.call(
this,
makeWrappedHandler(onFulfilled, handle, uid),
makeWrappedHandler(onRejected, handle, uid)
makeWrappedHandler(onFulfilled, handle, uid, true),
makeWrappedHandler(onRejected, handle, uid, false)
);
}
};

0 comments on commit 7d7dbfb

Please sign in to comment.