-
Notifications
You must be signed in to change notification settings - Fork 30.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
process: add multipleResolves event #22218
Closed
+177
−35
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,13 @@ using v8::Context; | |
using v8::Function; | ||
using v8::FunctionCallbackInfo; | ||
using v8::Isolate; | ||
using v8::kPromiseHandlerAddedAfterReject; | ||
using v8::kPromiseRejectAfterResolved; | ||
using v8::kPromiseRejectWithNoHandler; | ||
using v8::kPromiseResolveAfterResolved; | ||
using v8::Local; | ||
using v8::MaybeLocal; | ||
using v8::Number; | ||
using v8::Object; | ||
using v8::Promise; | ||
using v8::PromiseRejectEvent; | ||
|
@@ -67,34 +72,40 @@ void PromiseRejectCallback(PromiseRejectMessage message) { | |
PromiseRejectEvent event = message.GetEvent(); | ||
|
||
Environment* env = Environment::GetCurrent(isolate); | ||
|
||
if (env == nullptr) return; | ||
Local<Function> callback; | ||
|
||
Local<Function> callback = env->promise_handler_function(); | ||
Local<Value> value; | ||
Local<Value> type = Number::New(env->isolate(), event); | ||
|
||
if (event == v8::kPromiseRejectWithNoHandler) { | ||
callback = env->promise_reject_unhandled_function(); | ||
if (event == kPromiseRejectWithNoHandler) { | ||
value = message.GetValue(); | ||
|
||
if (value.IsEmpty()) | ||
value = Undefined(isolate); | ||
|
||
unhandledRejections++; | ||
} else if (event == v8::kPromiseHandlerAddedAfterReject) { | ||
callback = env->promise_reject_handled_function(); | ||
TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections), | ||
"rejections", | ||
"unhandled", unhandledRejections, | ||
"handledAfter", rejectionsHandledAfter); | ||
} else if (event == kPromiseHandlerAddedAfterReject) { | ||
value = Undefined(isolate); | ||
|
||
rejectionsHandledAfter++; | ||
TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections), | ||
"rejections", | ||
"unhandled", unhandledRejections, | ||
"handledAfter", rejectionsHandledAfter); | ||
} else if (event == kPromiseResolveAfterResolved) { | ||
value = message.GetValue(); | ||
} else if (event == kPromiseRejectAfterResolved) { | ||
value = message.GetValue(); | ||
} else { | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but I was asked to keep the return: #22218 (comment) |
||
} | ||
|
||
TRACE_COUNTER2(TRACING_CATEGORY_NODE2(promises, rejections), | ||
"rejections", | ||
"unhandled", unhandledRejections, | ||
"handledAfter", rejectionsHandledAfter); | ||
|
||
if (value.IsEmpty()) { | ||
value = Undefined(isolate); | ||
} | ||
|
||
Local<Value> args[] = { promise, value }; | ||
Local<Value> args[] = { type, promise, value }; | ||
MaybeLocal<Value> ret = callback->Call(env->context(), | ||
Undefined(isolate), | ||
arraysize(args), | ||
|
@@ -109,11 +120,17 @@ void SetupPromises(const FunctionCallbackInfo<Value>& args) { | |
Isolate* isolate = env->isolate(); | ||
|
||
CHECK(args[0]->IsFunction()); | ||
CHECK(args[1]->IsFunction()); | ||
CHECK(args[1]->IsObject()); | ||
|
||
Local<Object> constants = args[1].As<Object>(); | ||
|
||
NODE_DEFINE_CONSTANT(constants, kPromiseRejectWithNoHandler); | ||
NODE_DEFINE_CONSTANT(constants, kPromiseHandlerAddedAfterReject); | ||
NODE_DEFINE_CONSTANT(constants, kPromiseResolveAfterResolved); | ||
NODE_DEFINE_CONSTANT(constants, kPromiseRejectAfterResolved); | ||
|
||
isolate->SetPromiseRejectCallback(PromiseRejectCallback); | ||
env->set_promise_reject_unhandled_function(args[0].As<Function>()); | ||
env->set_promise_reject_handled_function(args[1].As<Function>()); | ||
env->set_promise_handler_function(args[0].As<Function>()); | ||
} | ||
|
||
#define BOOTSTRAP_METHOD(name, fn) env->SetMethod(bootstrapper, #name, fn) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const resolveMessage = 'First call'; | ||
const swallowedResolve = 'Swallowed resolve'; | ||
const rejection = new Error('Swallowed reject'); | ||
|
||
const expected = [ | ||
'resolveAfterResolved', | ||
swallowedResolve, | ||
'Resolve was called more than once.', | ||
'rejectAfterResolved', | ||
rejection, | ||
'Reject was called after resolve.' | ||
]; | ||
|
||
function handler(type, p, reason, message) { | ||
assert.strictEqual(type, expected.shift()); | ||
assert.deepStrictEqual(p, Promise.resolve(resolveMessage)); | ||
assert.strictEqual(reason, expected.shift()); | ||
assert.strictEqual(message, expected.shift()); | ||
} | ||
|
||
process.on('multipleResolves', common.mustCall(handler, 2)); | ||
|
||
new Promise((resolve, reject) => { | ||
resolve(resolveMessage); | ||
resolve(swallowedResolve); | ||
reject(rejection); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
could just call type
'resolve'
or'reject'
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.
I see that this could be sufficient but I personally like the very explicit nature here. If you feel strongly about it, I'll change it, otherwise I would rather keep it as is.
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.
I don't know which is best but my instincts tell me that it should be named "resolvedAfterResolved" or "resolveAfterResolve
d"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.
current naming is correct, there was a resolve after the promise resolved.
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.
Thanks, you're right. I didn't know "resolve" was also a noun.