Skip to content
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

src: make OnWorkComplete and OnExecute override-able #589

Closed
wants to merge 3 commits into from

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented Nov 6, 2019

No breaking changes on existing code were expected. All existing tests shall pass without any touch.
Changes on declaration:

  • Added Napi::AsyncWorker::OnWorkComplete.
  • Added Napi::AsyncWorker::OnExecute.

Fixes: #582

  • npm test passed.
  • documentation has changed.

@legendecas legendecas force-pushed the async-worker branch 2 times, most recently from 0eb4bba to e1e5e6f Compare November 6, 2019 12:09
@legendecas legendecas mentioned this pull request Nov 6, 2019
4 tasks
@legendecas
Copy link
Member Author

legendecas commented Nov 25, 2019

Napi::AsyncProgressWorkerBase should be able to accept an option to set the queue size of the tsfn and pass an opaque data pointer on its virtual OnWorkProgress method. Just wondering if this would produce another overhead on the TSFN callback since we already have an wrapper on the callback by Napi::ThreadSafeFunction.

Just mark the PR as working in progress.

@legendecas
Copy link
Member Author

There might be issues on sharing the AsyncProgressWorker base class between AsyncProgressWorker and AsyncProgressQueueWorker if the ThreadSafeFunction queue size is different: a possible redundant wrapper around ThreadSafeFunction Call might be introduced to AsyncProgressWorker.

So I'd like to remove the AsyncProgressWorker base class from the PR and leave the PR for the AsyncWorker's methods.

@mhdawson
Copy link
Member

mhdawson commented Dec 9, 2019

This one is ready to be reviewed @nodejs/n-api please review/comment.

doc/async_worker.md Outdated Show resolved Hide resolved
doc/async_worker.md Outdated Show resolved Hide resolved
doc/async_worker.md Outdated Show resolved Hide resolved
doc/async_worker.md Outdated Show resolved Hide resolved
doc/async_worker.md Outdated Show resolved Hide resolved
doc/async_worker.md Outdated Show resolved Hide resolved
napi.h Outdated Show resolved Hide resolved
Copy link
Contributor

@gabrielschulhof gabrielschulhof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM % nits

@@ -136,6 +136,30 @@ class was created, passing in the error as the first parameter.
virtual void Napi::AsyncWorker::OnError(const Napi::Error& e);
```

### OnWorkComplete

This method is invoked after the work has completed on JavaScript thread.
Copy link
Contributor

@gabrielschulhof gabrielschulhof Dec 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
This method is invoked after the work has completed on JavaScript thread.
This method is invoked after the work has completed on the JavaScript thread.

doc/async_worker.md Show resolved Hide resolved
napi-inl.h Outdated
Comment on lines 4202 to 4217
// The OnAsyncWorkExecute method receives an napi_env argument. However, do NOT
// use it within this method, as it does not run on the main thread and must
// not run any method that would cause JavaScript to run. In practice, this
// means that almost any use of napi_env will be incorrect.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This blurb should be copied into the documentation above.

napi-inl.h Outdated
#ifdef NAPI_CPP_EXCEPTIONS
try {
self->Execute();
this->Execute();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this-> here and in the lines below?

napi-inl.h Outdated
if (status != napi_cancelled) {
HandleScope scope(self->_env);
HandleScope scope(this->_env);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. I don't think we need this->.

@gabrielschulhof gabrielschulhof dismissed their stale review December 27, 2019 23:02

Noticed some greater-than-nit things like the removal of this->

No breaking changes on existing code were expected. All existing tests
shall pass without any touch.

Changes on declaration:
- Added `Napi::AsyncWorker::OnWorkComplete`.
- Added `Napi::AsyncWorker::OnExecute`.
@legendecas
Copy link
Member Author

@gabrielschulhof updated! PTAL :D

Copy link
Contributor

@gabrielschulhof gabrielschulhof left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with a few nits.

The default implementation of this method just calls the `Napi::AsyncWorker::Execute`
and handles exceptions if cpp exceptions were enabled.

The `OnExecute` method receives an `napi_env` argument. However, do NOT
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The `OnExecute` method receives an `napi_env` argument. However, do NOT
The `OnExecute` method receives an `napi_env` argument. However, it must NOT be used

and handles exceptions if cpp exceptions were enabled.

The `OnExecute` method receives an `napi_env` argument. However, do NOT
use it within this method, as it does not run on the JavaScript thread and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use it within this method, as it does not run on the JavaScript thread and
within this method, as it does not run on the JavaScript thread and

@gabrielschulhof
Copy link
Contributor

gabrielschulhof commented Jan 5, 2020

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

gabrielschulhof pushed a commit that referenced this pull request Jan 10, 2020
No breaking changes on existing code were expected. All existing tests
shall pass without any touch.

Changes on declaration:
- Added `Napi::AsyncWorker::OnWorkComplete`.
- Added `Napi::AsyncWorker::OnExecute`.

PR-URL: #589
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
@gabrielschulhof
Copy link
Contributor

Landed in 23ff7f0.

kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
No breaking changes on existing code were expected. All existing tests
shall pass without any touch.

Changes on declaration:
- Added `Napi::AsyncWorker::OnWorkComplete`.
- Added `Napi::AsyncWorker::OnExecute`.

PR-URL: nodejs/node-addon-api#589
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
No breaking changes on existing code were expected. All existing tests
shall pass without any touch.

Changes on declaration:
- Added `Napi::AsyncWorker::OnWorkComplete`.
- Added `Napi::AsyncWorker::OnExecute`.

PR-URL: nodejs/node-addon-api#589
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
No breaking changes on existing code were expected. All existing tests
shall pass without any touch.

Changes on declaration:
- Added `Napi::AsyncWorker::OnWorkComplete`.
- Added `Napi::AsyncWorker::OnExecute`.

PR-URL: nodejs/node-addon-api#589
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
No breaking changes on existing code were expected. All existing tests
shall pass without any touch.

Changes on declaration:
- Added `Napi::AsyncWorker::OnWorkComplete`.
- Added `Napi::AsyncWorker::OnExecute`.

PR-URL: nodejs/node-addon-api#589
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow overriding AsyncWorker::OnWorkComplete etc
3 participants