-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,77 @@ var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 && | |
var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 && | ||
(warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN")); | ||
|
||
var deferUnhandledRejectionCheck; | ||
(function() { | ||
var promises = []; | ||
|
||
function unhandledRejectionCheck() { | ||
for (var i = 0; i < promises.length; ++i) { | ||
promises[i]._notifyUnhandledRejection(); | ||
} | ||
unhandledRejectionClear(); | ||
} | ||
|
||
function unhandledRejectionClear() { | ||
promises.length = 0; | ||
} | ||
|
||
if (util.isNode) { | ||
deferUnhandledRejectionCheck = (function() { | ||
var timers = require("timers"); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
petkaantonov
Author
Owner
|
||
var timerSetTimeout = timers.setTimeout; | ||
var timer = timerSetTimeout(unhandledRejectionCheck, 1); | ||
timer.unref(); | ||
|
||
return function(promise) { | ||
promises.push(promise); | ||
if (typeof timer.refresh === "function") { | ||
timer.refresh(); | ||
} else { | ||
timerSetTimeout(unhandledRejectionCheck, 1).unref(); | ||
} | ||
}; | ||
})(); | ||
} else if (typeof document === "object" && document.createElement) { | ||
deferUnhandledRejectionCheck = (function() { | ||
var iframeSetTimeout; | ||
|
||
function checkIframe() { | ||
if (document.body) { | ||
var iframe = document.createElement("iframe"); | ||
document.body.appendChild(iframe); | ||
if (iframe.contentWindow && | ||
iframe.contentWindow.setTimeout) { | ||
iframeSetTimeout = iframe.contentWindow.setTimeout; | ||
} | ||
document.body.removeChild(iframe); | ||
} | ||
} | ||
checkIframe(); | ||
return function(promise) { | ||
promises.push(promise); | ||
if (iframeSetTimeout) { | ||
iframeSetTimeout(unhandledRejectionCheck, 1); | ||
} else { | ||
checkIframe(); | ||
} | ||
}; | ||
})(); | ||
} else { | ||
deferUnhandledRejectionCheck = function(promise) { | ||
promises.push(promise); | ||
setTimeout(unhandledRejectionCheck, 1); | ||
}; | ||
} | ||
|
||
es5.defineProperty(Promise, "_unhandledRejectionCheck", { | ||
value: unhandledRejectionCheck | ||
}); | ||
es5.defineProperty(Promise, "_unhandledRejectionClear", { | ||
value: unhandledRejectionClear | ||
}); | ||
})(); | ||
|
||
Promise.prototype.suppressUnhandledRejections = function() { | ||
var target = this._target(); | ||
target._bitField = ((target._bitField & (~IS_REJECTION_UNHANDLED)) | | ||
|
@@ -40,10 +111,7 @@ Promise.prototype.suppressUnhandledRejections = function() { | |
Promise.prototype._ensurePossibleRejectionHandled = function () { | ||
if ((this._bitField & IS_REJECTION_IGNORED) !== 0) return; | ||
this._setRejectionIsUnhandled(); | ||
var self = this; | ||
setTimeout(function() { | ||
self._notifyUnhandledRejection(); | ||
}, 1); | ||
deferUnhandledRejectionCheck(this); | ||
}; | ||
|
||
Promise.prototype._notifyUnhandledRejectionIsHandled = function () { | ||
|
@@ -144,46 +212,87 @@ Promise.hasLongStackTraces = function () { | |
return config.longStackTraces && longStackTracesIsSupported(); | ||
}; | ||
|
||
|
||
var legacyHandlers = { | ||
unhandledrejection: { | ||
before: function() { | ||
var ret = util.global.onunhandledrejection; | ||
util.global.onunhandledrejection = null; | ||
return ret; | ||
}, | ||
after: function(fn) { | ||
util.global.onunhandledrejection = fn; | ||
} | ||
}, | ||
rejectionhandled: { | ||
before: function() { | ||
var ret = util.global.onrejectionhandled; | ||
util.global.onrejectionhandled = null; | ||
return ret; | ||
}, | ||
after: function(fn) { | ||
util.global.onrejectionhandled = fn; | ||
} | ||
} | ||
}; | ||
|
||
var fireDomEvent = (function() { | ||
var dispatch = function(legacy, e) { | ||
if (legacy) { | ||
var fn; | ||
try { | ||
fn = legacy.before(); | ||
return !util.global.dispatchEvent(e); | ||
} finally { | ||
legacy.after(fn); | ||
} | ||
} else { | ||
return !util.global.dispatchEvent(e); | ||
} | ||
}; | ||
try { | ||
if (typeof CustomEvent === "function") { | ||
var event = new CustomEvent("CustomEvent"); | ||
util.global.dispatchEvent(event); | ||
return function(name, event) { | ||
name = name.toLowerCase(); | ||
var eventData = { | ||
detail: event, | ||
cancelable: true | ||
}; | ||
var domEvent = new CustomEvent(name.toLowerCase(), eventData); | ||
var domEvent = new CustomEvent(name, eventData); | ||
es5.defineProperty( | ||
domEvent, "promise", {value: event.promise}); | ||
es5.defineProperty( | ||
domEvent, "reason", {value: event.reason}); | ||
return !util.global.dispatchEvent(domEvent); | ||
|
||
return dispatch(legacyHandlers[name], domEvent); | ||
}; | ||
// In Firefox < 48 CustomEvent is not available in workers but | ||
// Event is. | ||
} else if (typeof Event === "function") { | ||
var event = new Event("CustomEvent"); | ||
util.global.dispatchEvent(event); | ||
return function(name, event) { | ||
var domEvent = new Event(name.toLowerCase(), { | ||
name = name.toLowerCase(); | ||
var domEvent = new Event(name, { | ||
cancelable: true | ||
}); | ||
domEvent.detail = event; | ||
es5.defineProperty(domEvent, "promise", {value: event.promise}); | ||
es5.defineProperty(domEvent, "reason", {value: event.reason}); | ||
return !util.global.dispatchEvent(domEvent); | ||
return dispatch(legacyHandlers[name], domEvent); | ||
}; | ||
} else { | ||
var event = document.createEvent("CustomEvent"); | ||
event.initCustomEvent("testingtheevent", false, true, {}); | ||
util.global.dispatchEvent(event); | ||
return function(name, event) { | ||
name = name.toLowerCase(); | ||
var domEvent = document.createEvent("CustomEvent"); | ||
domEvent.initCustomEvent(name.toLowerCase(), false, true, | ||
domEvent.initCustomEvent(name, false, true, | ||
event); | ||
return !util.global.dispatchEvent(domEvent); | ||
return dispatch(legacyHandlers[name], domEvent); | ||
}; | ||
} | ||
} catch (e) {} | ||
|
1 comment
on commit 60ef7a0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This commit makes the tests unreliable for me locally on macOS using Node.js 12.11.1. It doesn't fail every time but it fails most of the time if I use npm test
and less often if I use node tools/test --run=unhandled_rejections.js
:
55 passing (222ms)
1 failing
1) issues GH-1487-8:
Error: Reporting handled rejection as unhandled from: function testFunction() {
var ret = onUnhandledFail(testFunction);
var arr = [ Promise.reject( new Error('foo') ) ];
var p = Promise.resolve( arr );
p.filter( function() {} ).caught( function() {} );
return ret;
}
at /Users/trott/temp/bluebird/test/mocha/helpers/util.js:142:23
From previous event:
at onUnhandledFail (/Users/trott/temp/bluebird/test/mocha/helpers/util.js:141:16)
at context.testFunction (/Users/trott/temp/bluebird/test/mocha/unhandled_rejections.js:869:19)
at callFn (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:251:21)
at Test.Runnable.run (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:244:7)
at Runner.runTest (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:374:10)
at /Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:452:12
at next (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:299:14)
at /Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:309:7
at next (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:248:23)
at /Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:271:7
at done (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:207:5)
at callFn (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:262:7)
at Hook.Runnable.run (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:244:7)
at next (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:259:10)
at Immediate.<anonymous> (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:276:5)
at processImmediate (internal/timers.js:439:21)
Error: Reporting handled rejection as unhandled from: function testFunction() {
var ret = onUnhandledFail(testFunction);
var arr = [ Promise.reject( new Error('foo') ) ];
var p = Promise.resolve( arr );
p.filter( function() {} ).caught( function() {} );
return ret;
}
at /Users/trott/temp/bluebird/test/mocha/helpers/util.js:142:23
at onUnhandledFail (/Users/trott/temp/bluebird/test/mocha/helpers/util.js:141:16)
at context.testFunction (/Users/trott/temp/bluebird/test/mocha/unhandled_rejections.js:869:19)
at callFn (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:251:21)
at Test.Runnable.run (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:244:7)
at Runner.runTest (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:374:10)
at /Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:452:12
at next (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:299:14)
at /Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:309:7
at next (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:248:23)
at /Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:271:7
at done (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:207:5)
at callFn (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:262:7)
at Hook.Runnable.run (/Users/trott/temp/bluebird/node_modules/mocha/lib/runnable.js:244:7)
at next (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:259:10)
at Immediate.<anonymous> (/Users/trott/temp/bluebird/node_modules/mocha/lib/runner.js:276:5)
From previous event:
at Object.run (/Users/trott/temp/bluebird/tools/job-runner/job-runner.js:141:27)
at runTestGroup (/Users/trott/temp/bluebird/tools/test.js:123:22)
at /Users/trott/temp/bluebird/tools/test.js:261:16
at processImmediate (internal/timers.js:439:21)
From previous event:
at Object.<anonymous> (/Users/trott/temp/bluebird/tools/test.js:254:27)
at Module._compile (internal/modules/cjs/loader.js:945:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
at Module.load (internal/modules/cjs/loader.js:798:32)
at Function.Module._load (internal/modules/cjs/loader.js:711:12)
at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10)
at internal/main/run_main_module.js:17:11
libraries like Jest do override this sometimes.