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 AsyncContext class #252

Closed
wants to merge 1 commit into from
Closed

Conversation

romandev
Copy link
Contributor

This class provides a wrapper for the following custom asynchronous
operation APIs.

  • napi_async_init()
  • napi_async_destroy()
  • napi_make_callback()

This PR is initiated from #140 (comment).

@mhdawson
Copy link
Member

mhdawson commented May 1, 2018

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

@mhdawson
Copy link
Member

mhdawson commented Jul 9, 2018

@romandev are you going to have a chance to get back to this?

@romandev
Copy link
Contributor Author

romandev commented Jul 9, 2018

@mhdawson, I'll update this patch in this week.

@romandev
Copy link
Contributor Author

@mhdawson I've pushed a new patch. Please take a look.

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.

Great work and some notes about documentation. It could be good add one simple usage example at the end of documentation.

README.md Show resolved Hide resolved
@@ -0,0 +1,107 @@
# AsyncContext

The `AsyncWorker` class may not be appropriate for every scenario, because with
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor Author

@romandev romandev left a 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.

README.md Show resolved Hide resolved
@@ -0,0 +1,107 @@
# AsyncContext

The `AsyncWorker` class may not be appropriate for every scenario, because with
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@romandev romandev force-pushed the async_context branch 2 times, most recently from 8b13b94 to 4dc066e Compare July 13, 2018 22:53
# 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
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@romandev
Copy link
Contributor Author

romandev commented Sep 6, 2018

@mhdawson @NickNaso Could you pleaser review this?

@romandev
Copy link
Contributor Author

@mhdawson @NickNaso Gentle ping

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.

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.

@@ -0,0 +1,148 @@
# AsyncContext

The [AsyncWorker](async_worker.md) class may not be appropriate for every
Copy link
Member

Choose a reason for hiding this comment

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


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
Copy link
Member

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

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
Copy link
Member

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

returning from an async operation (when there is no other script on the stack).

```cpp
Value MakeCallback() const
Copy link
Member

Choose a reason for hiding this comment

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

Add Napi namepsace

Value MakeCallback() const
```

Returns a `Value` representing the JavaScript object returned.
Copy link
Member

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

Creates a new `AsyncContext`.

```cpp
explicit AsyncContext(const char* resource_name, const Function& callback);
Copy link
Member

Choose a reason for hiding this comment

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

Add Napi namespace

- `[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
Copy link
Member

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


### Constructor

Creates a new `AsyncContext`.
Copy link
Member

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

Creates a new `AsyncContext`.

```cpp
explicit AsyncContext(const char* resource_name, const Object& resource, const Function& callback);
Copy link
Member

Choose a reason for hiding this comment

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

Add Napi namespace

- `[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
Copy link
Member

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

@NickNaso
Copy link
Member

@mhdawson Could you review this PR?

@mhdawson
Copy link
Member

mhdawson commented Sep 18, 2018

@romandev sorry its taken so long to comment.

A couple of high level comments/questions

  1. It might be better if the class was not used with new/delete, but instead stack allocated and then automatically was cleaned up when existing a {} scope. I think that is the way that HandleScope works.

  2. I'm not sure including passing a callback and including the MakeCallback methods makes sense. I think it would be possible to make 2 callbacks within a since AsyncContext, and if you already have a FunctionReference its just as easy to call MakeCallback on that? In particular, I think it is mostly necessary when you want to do more than a single call back into JavaScript before closing the scope and things like the micro-tick queue running.

@mhdawson mhdawson mentioned this pull request Sep 18, 2018
6 tasks
@romandev
Copy link
Contributor Author

@NickNaso, @mhdawson Sorry for delay. I've been recently busy for other works but I'll take this work in this weekend. I know that this is release blocker. If you don't mind, can you wait for me? or if you urgent, please feel free to take this.

@mhdawson
Copy link
Member

@romandev if you can get to it this weekend I think it's worth waiting until next week for the release.

@romandev
Copy link
Contributor Author

romandev commented Oct 1, 2018

@mhdawson, @NickNaso I addressed all your comments. Please take another looks?

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.

Some little fix to do for the rest it's ok to me.

doc/function.md Outdated Show resolved Hide resolved
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
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

currenc -> current

```

- `[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
Copy link
Member

Choose a reason for hiding this comment

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

currenc -> current

```

- `[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
Copy link
Member

Choose a reason for hiding this comment

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

currenc -> current

```

- `[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
Copy link
Member

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()
@romandev
Copy link
Contributor Author

romandev commented Oct 1, 2018

FYI, I fixed typo as well :)

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.

LGTM

@NickNaso
Copy link
Member

NickNaso commented Oct 1, 2018

@mhdawson Could you take a look at this PR?

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

@mhdawson
Copy link
Member

mhdawson commented Oct 1, 2018

mhdawson pushed a commit that referenced this pull request Oct 1, 2018
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]>
@mhdawson
Copy link
Member

mhdawson commented Oct 1, 2018

Landed as dfcb939

@mhdawson mhdawson closed this Oct 1, 2018
yjaeseok pushed a commit to yjaeseok/node-addon-api that referenced this pull request Oct 1, 2018
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]>
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
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]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
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]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
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]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
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]>
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.

3 participants