@@ -626,6 +626,15 @@ typedef struct {
626626} napi_type_tag;
627627```
628628
629+ #### napi_async_cleanup_hook_handle
630+ <!-- YAML
631+ added: REPLACEME
632+ -->
633+
634+ An opaque value returned by [`napi_add_async_cleanup_hook`][]. It must be passed
635+ to [`napi_remove_async_cleanup_hook`][] when the chain of asynchronous cleanup
636+ events completes.
637+
629638### N-API callback types
630639
631640#### napi_callback_info
@@ -754,6 +763,30 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
754763Unless for reasons discussed in [Object Lifetime Management][], creating a
755764handle and/or callback scope inside the function body is not necessary.
756765
766+ #### napi_async_cleanup_hook
767+ <!-- YAML
768+ added: REPLACEME
769+ -->
770+
771+ Function pointer used with [`napi_add_async_cleanup_hook`][]. It will be called
772+ when the environment is being torn down.
773+
774+ Callback functions must satisfy the following signature:
775+
776+ ```c
777+ typedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle,
778+ void* data);
779+ ```
780+
781+ * `[in] handle`: The handle that must be passed to
782+ [`napi_remove_async_cleanup_hook`][] after completion of the asynchronous
783+ cleanup.
784+ * `[in] data`: The data that was passed to [`napi_add_async_cleanup_hook`][].
785+
786+ The body of the function should initiate the asynchronous cleanup actions at the
787+ end of which `handle` must be passed in a call to
788+ [`napi_remove_async_cleanup_hook`][].
789+
757790## Error handling
758791
759792N-API uses both return values and JavaScript exceptions for error handling.
@@ -1583,22 +1616,33 @@ with `napi_add_env_cleanup_hook`, otherwise the process will abort.
15831616#### napi_add_async_cleanup_hook
15841617<!-- YAML
15851618added: REPLACEME
1619+ changes:
1620+ - version: REPLACEME
1621+ pr-url: https://github.com/nodejs/node/pull/34819
1622+ description: Changed signature of the `hook` callback.
15861623-->
15871624
15881625> Stability: 1 - Experimental
15891626
15901627```c
15911628NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
15921629 napi_env env,
1593- void (*fun)(void* arg, void(* cb)(void*), void* cbarg) ,
1630+ napi_async_cleanup_hook hook ,
15941631 void* arg,
15951632 napi_async_cleanup_hook_handle* remove_handle);
15961633```
15971634
1598- Registers `fun` as a function to be run with the `arg` parameter once the
1599- current Node.js environment exits. Unlike [`napi_add_env_cleanup_hook`][],
1600- the hook is allowed to be asynchronous in this case, and must invoke the passed
1601- `cb()` function with `cbarg` once all asynchronous activity is finished.
1635+ * `[in] env`: The environment that the API is invoked under.
1636+ * `[in] hook`: The function pointer to call at environment teardown.
1637+ * `[in] arg`: The pointer to pass to `hook` when it gets called.
1638+ * `[out] remove_handle`: Optional handle that refers to the asynchronous cleanup
1639+ hook.
1640+
1641+ Registers `hook`, which is a function of type [`napi_async_cleanup_hook`][], as
1642+ a function to be run with the `remove_handle` and `arg` parameters once the
1643+ current Node.js environment exits.
1644+
1645+ Unlike [`napi_add_env_cleanup_hook`][], the hook is allowed to be asynchronous.
16021646
16031647Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
16041648
@@ -1611,19 +1655,25 @@ is being torn down anyway.
16111655#### napi_remove_async_cleanup_hook
16121656<!-- YAML
16131657added: REPLACEME
1658+ changes:
1659+ - version: REPLACEME
1660+ pr-url: https://github.com/nodejs/node/pull/34819
1661+ description: Removed `env` parameter.
16141662-->
16151663
16161664> Stability: 1 - Experimental
16171665
16181666```c
16191667NAPI_EXTERN napi_status napi_remove_async_cleanup_hook(
1620- napi_env env,
16211668 napi_async_cleanup_hook_handle remove_handle);
16221669```
16231670
1671+ * `[in] remove_handle`: The handle to an asynchronous cleanup hook that was
1672+ created with [`napi_add_async_cleanup_hook`][].
1673+
16241674Unregisters the cleanup hook corresponding to `remove_handle`. This will prevent
16251675the hook from being executed, unless it has already started executing.
1626- This must be called on any `napi_async_cleanup_hook_handle` value retrieved
1676+ This must be called on any `napi_async_cleanup_hook_handle` value obtained
16271677from [`napi_add_async_cleanup_hook`][].
16281678
16291679## Module registration
@@ -5731,6 +5781,7 @@ This API may only be called from the main thread.
57315781[`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook
57325782[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
57335783[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5784+ [`napi_async_cleanup_hook`]: #n_api_napi_async_cleanup_hook
57345785[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
57355786[`napi_async_init`]: #n_api_napi_async_init
57365787[`napi_callback`]: #n_api_napi_callback
0 commit comments