-
Notifications
You must be signed in to change notification settings - Fork 462
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
Test and fix AsyncWorker #30
Conversation
@digitalinfinity please review, this is somewhat based on your feedback. |
napi.h
Outdated
@@ -850,18 +846,16 @@ namespace Napi { | |||
void Queue(); | |||
void Cancel(); | |||
|
|||
virtual void Execute() = 0; | |||
virtual void WorkComplete(); | |||
|
|||
ObjectReference& Persistent(); | |||
|
|||
protected: | |||
explicit AsyncWorker(const Function& callback); |
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.
Were you still planning on having an overload that allowed the user to pass in the this pointer for use with the callbacks?
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 had been thinking that another object reference would be wasteful, but now I realize it could make sense to use the receiver object as the _persistent
object reference, if one was supplied, instead of creating a new object. I'll update this PR.
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.
LGTM
napi.h
Outdated
virtual void OnOK(); | ||
virtual void OnError(Error e); | ||
|
||
void SetError(Error error); | ||
void SetError(std::string error); |
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 should be const std::string&
napi-inl.h
Outdated
catch (const Error& e) { | ||
self->SetError(e); | ||
} | ||
self->Execute(); |
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.
We could still store error.what()
for any std::exception
we run into … anything else is going to hard crash the process anyway, afaict?
I was working on updating this PR, but after adding more tests I ran into a bug #31. I'm going to fix that first, then come back to this. |
I rebased on top of the error fix, and updated to address review feedback so far. So when reviewing here, please only look at the second commit. I encountered an interesting problem: how to deal with unhandled JS exceptions during an async callback. See the TODO comment in |
Related to the TODO mentioned above, I'm working on a change in the C APIs that will call |
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.
LGTM
Related node PR: nodejs/node#12838 |
- Remove MakeCallback overload that defaulted to undefined receiver, because node::MakeCallback requires an object. - Allow the AsyncWorker constructor to optionally specify a receiver object, that is persisted and used as of an async callback. - Persist async errors as strings, because an Error object cannot be created outside of a JS context. - Remove overridable AsyncWorker::WorkComplete() because it wasn't useful and caused confusion. OnOK() and/or OnError() should be (optionally) overridden instead. - Add tests to validate basic success and error scenarios for AsyncWorker. - Also add necessary cast to Object when calling Unwrap.
After nodejs/node#12838 is merged, the exception will be properly reported.
MakeCallback()
overloads that defaulted to undefined receiver, becausenode::MakeCallback()
requires an object.Error
object cannot be created outside of a JS contextAsyncWorker::WorkComplete()
because it wasn't useful and caused confusion.OnOK()
and/orOnError()
should be (optionally) overridden instead.AsyncWorker