Skip to content

Implement AsyncContext class#252

Closed
romandev wants to merge 1 commit intonodejs:masterfrom
romandev:async_context
Closed

Implement AsyncContext class#252
romandev wants to merge 1 commit intonodejs:masterfrom
romandev:async_context

Conversation

@romandev
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Member

mhdawson commented Jul 9, 2018

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

@romandev
Copy link
Copy Markdown
Contributor Author

romandev commented Jul 9, 2018

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

@romandev
Copy link
Copy Markdown
Contributor Author

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

Copy link
Copy Markdown
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.

Comment thread README.md
Comment thread doc/async_context.md Outdated
@@ -0,0 +1,107 @@
# AsyncContext

The `AsyncWorker` class may not be appropriate for every scenario, because with
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread README.md
Comment thread doc/async_context.md Outdated
@@ -0,0 +1,107 @@
# AsyncContext

The `AsyncWorker` class may not be appropriate for every scenario, because with
Copy link
Copy Markdown
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
Comment thread 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
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

romandev commented Sep 6, 2018

@mhdawson @NickNaso Could you pleaser review this?

@romandev
Copy link
Copy Markdown
Contributor Author

@mhdawson @NickNaso Gentle ping

Copy link
Copy Markdown
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.

Comment thread doc/async_context.md Outdated
@@ -0,0 +1,148 @@
# AsyncContext

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

Choose a reason for hiding this comment

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

Comment thread 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
Copy link
Copy Markdown
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

Comment thread 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
Copy link
Copy Markdown
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

Comment thread doc/async_context.md Outdated
returning from an async operation (when there is no other script on the stack).

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

Choose a reason for hiding this comment

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

Add Napi namepsace

Comment thread doc/async_context.md Outdated
Value MakeCallback() const
```

Returns a `Value` representing the JavaScript object returned.
Copy link
Copy Markdown
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

Comment thread doc/async_context.md Outdated
Creates a new `AsyncContext`.

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

Choose a reason for hiding this comment

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

Add Napi namespace

Comment thread 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
Copy link
Copy Markdown
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

Comment thread doc/async_context.md Outdated

### Constructor

Creates a new `AsyncContext`.
Copy link
Copy Markdown
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

Comment thread doc/async_context.md Outdated
Creates a new `AsyncContext`.

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

Choose a reason for hiding this comment

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

Add Napi namespace

Comment thread 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
Copy link
Copy Markdown
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
Copy Markdown
Member

@mhdawson Could you review this PR?

@mhdawson
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

romandev commented Oct 1, 2018

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

Copy link
Copy Markdown
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.

Comment thread doc/function.md Outdated
Comment thread doc/function.md Outdated
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

currenc -> current

Comment thread doc/function.md Outdated
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

currenc -> current

Comment thread doc/function_reference.md Outdated
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

currenc -> current

Comment thread doc/function_reference.md Outdated
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

currenc -> current

Comment thread doc/function_reference.md Outdated
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
Copy Markdown
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
Copy Markdown
Contributor Author

romandev commented Oct 1, 2018

FYI, I fixed typo as well :)

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

NickNaso commented Oct 1, 2018

@mhdawson Could you take a look at this PR?

Copy link
Copy Markdown
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
Copy Markdown
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 <michael_dawson@ca.ibm.com>
Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
@mhdawson
Copy link
Copy Markdown
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 <michael_dawson@ca.ibm.com>
Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
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 <michael_dawson@ca.ibm.com>
Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
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 <michael_dawson@ca.ibm.com>
Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
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 <michael_dawson@ca.ibm.com>
Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
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 <michael_dawson@ca.ibm.com>
Reviewed-By: Nicola Del Gobbo <nicoladelgobbo@NickNaso.local>
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