-
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 AsyncContext class #252
Conversation
@romandev thanks for putting this together. Can you add the doc for this class? I have a few questions but I think the doc would likely answer them and we'll need it anyway. |
@romandev are you going to have a chance to get back to this? |
@mhdawson, I'll update this patch in this week. |
@mhdawson I've pushed a new patch. Please take a look. |
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.
Great work and some notes about documentation. It could be good add one simple usage example at the end of documentation.
doc/async_context.md
Outdated
@@ -0,0 +1,107 @@ | |||
# AsyncContext | |||
|
|||
The `AsyncWorker` class may not be appropriate for every scenario, because with |
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.
Maybe could be good link the AsyncWorker doc
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.
Done.
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.
Thank you for review. I addressed your comments.
doc/async_context.md
Outdated
@@ -0,0 +1,107 @@ | |||
# AsyncContext | |||
|
|||
The `AsyncWorker` class may not be appropriate for every scenario, because with |
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.
Done.
8b13b94
to
4dc066e
Compare
doc/async_context.md
Outdated
# AsyncContext | ||
|
||
The [AsyncWorker](async_worker.md) class may not be appropriate for every scenario, because with | ||
those the async execution still happens on the main event loop. When using any |
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 think this is quite right as the async execution happens in a thread from the existing thread pool. I'll have a closer look and add some I think describes the difference.
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 matched up with https://nodejs.org/api/n-api.html#n_api_custom_asynchronous_operations.
4dc066e
to
ef365f6
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.
Hi @romandev the code is good to me. Just some suggestions about the documentation. Recently we decided to use the complete namespace to refer at node-addon-api class. So please do another pass on documentation and add Napi namespace where necessary.
doc/async_context.md
Outdated
@@ -0,0 +1,148 @@ | |||
# AsyncContext | |||
|
|||
The [AsyncWorker](async_worker.md) class may not be appropriate for every |
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.
AsyncWorker should be Napi::AsyncWorker
doc/async_context.md
Outdated
|
||
The [AsyncWorker](async_worker.md) class may not be appropriate for every | ||
scenario. When using any other async mechanism, introducing a new class | ||
`AsyncContext`is necessary to ensure an async operation is properly tracked by |
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.
AsyncContext
should be Napi::AsyncContext
doc/async_context.md
Outdated
The [AsyncWorker](async_worker.md) class may not be appropriate for every | ||
scenario. When using any other async mechanism, introducing a new class | ||
`AsyncContext`is necessary to ensure an async operation is properly tracked by | ||
the runtime. The `AsyncContext` class provides `MakeCallback()` method to |
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.
AsyncContext
should be Napi::AsyncContext
doc/async_context.md
Outdated
returning from an async operation (when there is no other script on the stack). | ||
|
||
```cpp | ||
Value MakeCallback() const |
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.
Add Napi namepsace
doc/async_context.md
Outdated
Value MakeCallback() const | ||
``` | ||
|
||
Returns a `Value` representing the JavaScript object returned. |
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.
Value
should be Napi::Value
doc/async_context.md
Outdated
Creates a new `AsyncContext`. | ||
|
||
```cpp | ||
explicit AsyncContext(const char* resource_name, 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.
Add Napi namespace
doc/async_context.md
Outdated
- `[in] callback`: The function which will be called when an asynchronous | ||
operations ends. | ||
|
||
Returns an AsyncContext instance which can later make the given callback by |
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.
an AsyncContext should be a Napi::AsyncContext
doc/async_context.md
Outdated
|
||
### Constructor | ||
|
||
Creates a new `AsyncContext`. |
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.
AsyncContext
should be Napi::AsyncContext
doc/async_context.md
Outdated
Creates a new `AsyncContext`. | ||
|
||
```cpp | ||
explicit AsyncContext(const char* resource_name, const Object& resource, 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.
Add Napi namespace
doc/async_context.md
Outdated
- `[in] callback`: The function which will be called when an asynchronous | ||
operations ends. | ||
|
||
Returns an AsyncContext instance which can later make the given callback by |
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.
Returns an AsyncContext should be Returns a Napi::AsyncContext
@mhdawson Could you review this PR? |
@romandev sorry its taken so long to comment. A couple of high level comments/questions
|
@romandev if you can get to it this weekend I think it's worth waiting until next week for the release. |
ef365f6
to
aeab2b4
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.
Some little fix to do for the rest it's ok to me.
doc/function.md
Outdated
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] args`: List of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the currenc async context |
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.
currenc -> current
doc/function.md
Outdated
``` | ||
|
||
- `[in] recv`: The `this` object passed to the called function. | ||
- `[in] argc`: The number of the arguments passed to the function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the currenc async context |
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.
currenc -> current
doc/function_reference.md
Outdated
``` | ||
|
||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] args`: Initializer list of JavaScript values as `napi_value` representing | ||
the arguments of the referenced function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the currenc async context |
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.
currenc -> current
doc/function_reference.md
Outdated
``` | ||
|
||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] args`: Vector of JavaScript values as `napi_value` representing the | ||
arguments of the referenced function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the currenc async context |
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.
currenc -> current
doc/function_reference.md
Outdated
``` | ||
|
||
- `[in] recv`: The `this` object passed to the referenced function when it's called. | ||
- `[in] argc`: The number of arguments passed to the referenced function. | ||
- `[in] args`: Array of JavaScript values as `napi_value` representing the | ||
arguments of the referenced function. | ||
- `[in] context`: Context for the async operation that is invoking the callback. | ||
This should normally be a value previously obtained from [Napi::AsyncContext](async_context.md). | ||
However `nullptr` is also allowed, which indicates the currenc async context |
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.
currenc -> current
This class provides a wrapper for the following custom asynchronous operation APIs. - napi_async_init() - napi_async_destroy()
aeab2b4
to
0e6219b
Compare
FYI, I fixed typo as well :) |
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
@mhdawson Could you take a look at 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
CI run to make sure its ok on 6.x https://ci.nodejs.org/view/x%20-%20Abi%20stable%20module%20API/job/node-test-node-addon-api/784/ |
This class provides a wrapper for the following custom asynchronous operation APIs. - napi_async_init() - napi_async_destroy() PR-URL: #252 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
Landed as dfcb939 |
This class provides a wrapper for the following custom asynchronous operation APIs. - napi_async_init() - napi_async_destroy() PR-URL: nodejs#252 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
This class provides a wrapper for the following custom asynchronous operation APIs. - napi_async_init() - napi_async_destroy() PR-URL: nodejs/node-addon-api#252 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
This class provides a wrapper for the following custom asynchronous operation APIs. - napi_async_init() - napi_async_destroy() PR-URL: nodejs/node-addon-api#252 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
This class provides a wrapper for the following custom asynchronous operation APIs. - napi_async_init() - napi_async_destroy() PR-URL: nodejs/node-addon-api#252 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
This class provides a wrapper for the following custom asynchronous operation APIs. - napi_async_init() - napi_async_destroy() PR-URL: nodejs/node-addon-api#252 Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Nicola Del Gobbo <[email protected]>
This class provides a wrapper for the following custom asynchronous
operation APIs.
This PR is initiated from #140 (comment).