-
Notifications
You must be signed in to change notification settings - Fork 465
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
Implement AsyncProgressWorker #529
Conversation
CI run to check across platforms: https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/503/nodes=rhel72-s390x/console |
Seems to be a failure due to unused paramter in OnProgress. Will push a change to address that so can test further. |
@legendecas can you take a look, seems to be failing in CI runs across platforms with:
|
@legendecas sorry, see you are still working on the tests from your checklist in the original post. Let us know when it's ready to review. One other thing that you'll need to add is docs in the "Async Operations" section of https://github.com/nodejs/node-addon-api |
Yes, it's crashed for case https://github.com/nodejs/node-addon-api/blob/master/test/asyncworker.js. It could be reproduced locally. |
Added to checklist |
f7d9fd4
to
a9093d9
Compare
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.
@legendecas I downloaded your work and it seems to work on macOS, but to execute the tests you need to add asyncprogressworker
to here https://github.com/legendecas/node-addon-api/blob/async-progress-work/test/index.js#L13 and remove it in case the N-API version is less then 4 like we did here https://github.com/legendecas/node-addon-api/blob/async-progress-work/test/index.js#L67
#532 would blocking this PR from their routine work like CI/tests. (Just a note) |
a9093d9
to
16f7249
Compare
16f7249
to
3c3d321
Compare
3c3d321
to
f150a98
Compare
Was a bit worried that some of the locking features might have requuired later compilers. This CI run seems to confirm it can build/run on Node.js 8.X so that is not an issue: https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api-new/675/ |
@legendecas sorry it's taking so long to get the review done. I'm out of time for today but I'll try to continue reviewing next week. |
521711b
to
2540375
Compare
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, @legendecas thanks for all of the work on this. Hope to do a pass of landing PRs in the next few days.
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.
Partial review. Will continue.
doc/async_progress_worker.md
Outdated
|
||
void OnProgress(const uint32_t* data, size_t /* count */) { | ||
HandleScope scope(Env()); | ||
Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), data)}); |
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.
data
here is a pointer to an integer, not an integer. Also, there is no synchronization between the Execute
writing to i
and OnProgress
reading from i
. Perhaps above the code should be changed to
progress.Send(new uint32_t(i), 1);
and here it should be
Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), *data)});
delete data;
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.
Yes, it's a pointer to an integer. I'll update the doc.
For the later question, as documented above, progress.Send
will copy the data pointed to right on the call to the progress.Send
. So it is safe to do so without any synchronizations.
7e0e636
to
b9748f2
Compare
21fd0ba
to
5c11a56
Compare
5c11a56
to
390507c
Compare
doc/async_progress_worker.md
Outdated
@@ -0,0 +1,344 @@ | |||
# AsyncProgressWorker | |||
|
|||
`Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker` |
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.
`Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker` | |
`Napi::AsyncProgressWorker` is an abstract class which implements `Napi::AsyncWorker` |
doc/async_progress_worker.md
Outdated
|
||
## Methods | ||
|
||
[`Napi::AsyncWorker`]() provides detailed descriptions for most methods. |
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.
[`Napi::AsyncWorker`]() provides detailed descriptions for most methods. | |
[`Napi::AsyncWorker`][] provides detailed descriptions for most methods. |
doc/async_progress_worker.md
Outdated
explicit Napi::AsyncProgressWorker(const Napi::Object& receiver, const Napi::Function& callback, const char* resource_name, const Napi::Object& resource); | ||
``` | ||
|
||
- `[in] receiver`: The `this` object passed to the called function. |
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.
- `[in] receiver`: The `this` object passed to the called function. | |
- `[in] receiver`: The `this` object to pass to the called function. |
doc/async_progress_worker.md
Outdated
passed in through its constructor. | ||
|
||
During the worker thread execution, the first argument of `Napi::AsyncProgressWorker::Execute` | ||
can be used to report the process of the execution. |
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.
can be used to report the process of the execution. | |
can be used to report the progress of the execution. |
doc/async_progress_worker.md
Outdated
callback that the `Napi::AsyncProgressWorker` base class will store persistently. When | ||
the work on the `Napi::AsyncProgressWorker::Execute` method is done the | ||
`Napi::AsyncProgressWorker::OnOk` method is called and the results return back to | ||
JavaScript invoking the stored callback with its associated environment. |
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.
JavaScript invoking the stored callback with its associated environment. | |
JavaScript when the stored callback is invoked with its associated environment. |
doc/async_progress_worker.md
Outdated
The following code shows an example of how to create and use an `Napi::AsyncProgressWorker` | ||
|
||
```cpp | ||
#include<napi.h> |
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.
#include<napi.h> | |
#include <napi.h> |
doc/async_progress_worker.md
Outdated
Using the implementation of a `Napi::AsyncProgressWorker` is straight forward. You only | ||
need to create a new instance and pass to its constructor the callback you want to | ||
execute when your asynchronous task ends and other data you need for your | ||
computation. Once created the only other action you have to do is to call the | ||
`Napi::AsyncProgressWorker::Queue` method that will queue the created worker for execution. |
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 generally avoid using "you" in our documentation. Let's rephrase this as
The implementation of a `Napi::AsyncProgressWorker` can be used by creating a
new instance and passing to its constructore the callback to execute when the
asynchronous task ends and other data needed for the computation. Once created,
the only other action needed is to call the `Napi::AsyncProgressWorker::Queue`
method that will queue the created worker for execution.
doc/async_progress_worker.md
Outdated
|
||
The first step to use the `Napi::AsyncProgressWorker` class is to create a new class that | ||
inherits from it and implement the `Napi::AsyncProgressWorker::Execute` abstract method. | ||
Typically input to your worker will be saved within the class' fields generally |
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.
Typically input to your worker will be saved within the class' fields generally | |
Typically input to the worker will be saved within the class' fields generally |
doc/async_progress_worker.md
Outdated
use namespace Napi; | ||
|
||
Value Echo(const CallbackInfo& info) { | ||
// You need to validate the arguments here |
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.
// You need to validate the arguments here | |
// We need to validate the arguments here |
PR-URL: #529 Fixes: #473 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
Landed as 650562c |
PR-URL: nodejs/node-addon-api#529 Fixes: nodejs/node-addon-api#473 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
PR-URL: nodejs/node-addon-api#529 Fixes: nodejs/node-addon-api#473 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
PR-URL: nodejs/node-addon-api#529 Fixes: nodejs/node-addon-api#473 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
PR-URL: nodejs/node-addon-api#529 Fixes: nodejs/node-addon-api#473 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Gabriel Schulhof <[email protected]>
Related: #473