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

Implement AsyncProgressWorker #529

Closed
wants to merge 2 commits into from

Conversation

legendecas
Copy link
Member

@legendecas legendecas commented Aug 27, 2019

Related: #473

  • npm test passed
  • test included
  • documents are added or changed

napi.h Outdated Show resolved Hide resolved
@mhdawson
Copy link
Member

@mhdawson
Copy link
Member

Seems to be a failure due to unused paramter in OnProgress. Will push a change to address that so can test further.

@mhdawson
Copy link
Member

@mhdawson
Copy link
Member

@legendecas can you take a look, seems to be failing in CI runs across platforms with:

All tests passed!
terminate called after throwing an instance of 'Napi::Error'
  what():  The expression evaluated to a falsy value:

  assert.ok(e instanceof Error)

Tests aborted with SIGABRT
npm ERR! Test failed.  See above for more details.

@mhdawson
Copy link
Member

@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

@legendecas
Copy link
Member Author

@legendecas can you take a look, seems to be failing in CI runs across platforms with:

All tests passed!
terminate called after throwing an instance of 'Napi::Error'
  what():  The expression evaluated to a falsy value:

  assert.ok(e instanceof Error)

Tests aborted with SIGABRT
npm ERR! Test failed.  See above for more details.

Yes, it's crashed for case https://github.com/nodejs/node-addon-api/blob/master/test/asyncworker.js. It could be reproduced locally.

@legendecas
Copy link
Member Author

One other thing that you'll need to add is docs in the "Async Operations" section of https://github.com/nodejs/node-addon-api

Added to checklist

napi-inl.h Outdated Show resolved Hide resolved
napi-inl.h Outdated Show resolved Hide resolved
napi.h Outdated Show resolved Hide resolved
@legendecas legendecas force-pushed the async-progress-work branch 2 times, most recently from f7d9fd4 to a9093d9 Compare August 28, 2019 09:22
Copy link
Member

@NickNaso NickNaso left a 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

@legendecas
Copy link
Member Author

#532 would blocking this PR from their routine work like CI/tests. (Just a note)

@mhdawson
Copy link
Member

mhdawson commented Sep 27, 2019

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/

@mhdawson
Copy link
Member

@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.

napi.h Outdated Show resolved Hide resolved
napi.h Show resolved Hide resolved
napi-inl.h Outdated Show resolved Hide resolved
@legendecas legendecas force-pushed the async-progress-work branch 3 times, most recently from 521711b to 2540375 Compare October 6, 2019 14:54
napi-inl.h Outdated Show resolved Hide resolved
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, @legendecas thanks for all of the work on this. Hope to do a pass of landing PRs in the next few days.

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.

Partial review. Will continue.

doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved

void OnProgress(const uint32_t* data, size_t /* count */) {
HandleScope scope(Env());
Callback().Call({Env().Null(), Env().Null(), Number::New(Env(), data)});
Copy link
Contributor

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;

Copy link
Member Author

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.

doc/async_progress_worker.md Outdated Show resolved Hide resolved
doc/async_progress_worker.md Outdated Show resolved Hide resolved
napi-inl.h Show resolved Hide resolved
@NickNaso NickNaso mentioned this pull request Oct 22, 2019
15 tasks
@legendecas legendecas force-pushed the async-progress-work branch 2 times, most recently from 7e0e636 to b9748f2 Compare October 23, 2019 12:22
@@ -0,0 +1,344 @@
# AsyncProgressWorker

`Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker`
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
`Napi::AsyncProgressWorker` is an abstract class, which implements `Napi::AsyncWorker`
`Napi::AsyncProgressWorker` is an abstract class which implements `Napi::AsyncWorker`


## Methods

[`Napi::AsyncWorker`]() provides detailed descriptions for most methods.
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
[`Napi::AsyncWorker`]() provides detailed descriptions for most methods.
[`Napi::AsyncWorker`][] provides detailed descriptions for most methods.

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.
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
- `[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 Show resolved Hide resolved
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.
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
can be used to report the process of the execution.
can be used to report the progress of the execution.

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.
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
JavaScript invoking the stored callback with its associated environment.
JavaScript when the stored callback is invoked with its associated environment.

The following code shows an example of how to create and use an `Napi::AsyncProgressWorker`

```cpp
#include<napi.h>
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
#include<napi.h>
#include <napi.h>

Comment on lines 338 to 342
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.
Copy link
Contributor

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.


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
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
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

use namespace Napi;

Value Echo(const CallbackInfo& info) {
// You need to validate the arguments here
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
// You need to validate the arguments here
// We need to validate the arguments here

mhdawson pushed a commit that referenced this pull request Nov 1, 2019
PR-URL: #529
Fixes: #473
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Gabriel Schulhof <[email protected]>
@mhdawson
Copy link
Member

mhdawson commented Nov 1, 2019

Landed as 650562c

@mhdawson mhdawson closed this Nov 1, 2019
@legendecas legendecas deleted the async-progress-work branch November 2, 2019 02:02
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
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]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
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]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
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]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
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]>
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.

4 participants