Skip to content

Commit 5b694e4

Browse files
author
Gabriel Schulhof
committed
storing the handle at creation is optional again
1 parent 6037705 commit 5b694e4

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

doc/api/n-api.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,9 @@ typedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle,
771771
void* data);
772772
```
773773

774-
* `[in] handle`: The handle obtained from [`napi_add_async_cleanup_hook`][].
774+
* `[in] handle`: The handle that must be passed to
775+
[`napi_remove_async_cleanup_hook`][] after completion of the asynchronous
776+
cleanup.
775777
* `[in] data`: The data that was passed to [`napi_add_async_cleanup_hook`][].
776778

777779
The body of the function should initiate the asynchronous cleanup actions at the
@@ -1626,24 +1628,22 @@ NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
16261628
* `[in] env`: The environment that the API is invoked under.
16271629
* `[in] hook`: The function pointer to call at environment teardown.
16281630
* `[in] arg`: The pointer to pass to `hook` when it gets called.
1629-
* `[out] remove_handle`: The handle that refers to the asynchronous cleanup
1630-
hook. This handle must be passed to [`napi_remove_async_cleanup_hook`][] even if
1631-
`hook` gets called.
1631+
* `[out] remove_handle`: Optional handle that refers to the asynchronous cleanup
1632+
hook.
16321633

16331634
Registers `hook`, which is a function of type [`napi_async_cleanup_hook`][], as
16341635
a function to be run with the `remove_handle` and `arg` parameters once the
16351636
current Node.js environment exits.
16361637

1637-
Unlike [`napi_add_env_cleanup_hook`][], the hook
1638-
is allowed to be asynchronous, and must pass `remove_handle` in a call to
1639-
[`napi_remove_env_cleanup_hook`][] once all asynchronous activity is finished.
1638+
Unlike [`napi_add_env_cleanup_hook`][], the hook is allowed to be asynchronous.
16401639

16411640
Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
16421641

1643-
An opaque value will be stored in `remove_handle` that must later be passed to
1644-
[`napi_remove_async_cleanup_hook`][], regardless of whether the hook has already
1645-
been invoked. Typically, that happens when the resource for which this hook was
1646-
added is being torn down anyway.
1642+
If `remove_handle` is not `NULL`, an opaque value will be stored in it
1643+
that must later be passed to [`napi_remove_async_cleanup_hook`][],
1644+
regardless of whether the hook has already been invoked.
1645+
Typically, that happens when the resource for which this hook was added
1646+
is being torn down anyway.
16471647

16481648
#### napi_remove_async_cleanup_hook
16491649
<!-- YAML

src/node_api.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,13 @@ napi_status napi_add_async_cleanup_hook(
562562
napi_async_cleanup_hook_handle* remove_handle) {
563563
CHECK_ENV(env);
564564
CHECK_ARG(env, hook);
565-
CHECK_ARG(env, remove_handle);
566565

567-
*remove_handle = new napi_async_cleanup_hook_handle__(env, hook, arg);
566+
napi_async_cleanup_hook_handle__* handle =
567+
new napi_async_cleanup_hook_handle__(env, hook, arg);
568+
569+
if (remove_handle != nullptr)
570+
*remove_handle = handle;
571+
568572
return napi_clear_last_error(env);
569573
}
570574

test/node-api/test_async_cleanup_hook/binding.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static void AsyncCleanupHook(napi_async_cleanup_hook_handle handle, void* arg) {
4141
assert(err == 0);
4242

4343
data->async.data = data;
44+
data->handle = handle;
4445
uv_async_send(&data->async);
4546
}
4647

@@ -51,6 +52,12 @@ static napi_value Init(napi_env env, napi_value exports) {
5152
napi_add_async_cleanup_hook(env, AsyncCleanupHook, data, &data->handle);
5253
}
5354

55+
{
56+
struct AsyncData* data = CreateAsyncData();
57+
data->env = env;
58+
napi_add_async_cleanup_hook(env, AsyncCleanupHook, data, NULL);
59+
}
60+
5461
{
5562
napi_async_cleanup_hook_handle must_not_call_handle;
5663
napi_add_async_cleanup_hook(

0 commit comments

Comments
 (0)