@@ -1543,10 +1543,12 @@ and will lead the process to abort.
15431543The hooks will be called in reverse order, i.e. the most recently added one
15441544will be called first.
15451545
1546- Removing this hook can be done by using `napi_remove_env_cleanup_hook`.
1546+ Removing this hook can be done by using [ `napi_remove_env_cleanup_hook`][] .
15471547Typically, that happens when the resource for which this hook was added
15481548is being torn down anyway.
15491549
1550+ For asynchronous cleanup, [`napi_add_async_cleanup_hook`][] is available.
1551+
15501552#### napi_remove_env_cleanup_hook
15511553<!-- YAML
15521554added: v10.2.0
@@ -1566,6 +1568,52 @@ need to be exact matches.
15661568The function must have originally been registered
15671569with `napi_add_env_cleanup_hook`, otherwise the process will abort.
15681570
1571+ #### napi_add_async_cleanup_hook
1572+ <!-- YAML
1573+ added: REPLACEME
1574+ -->
1575+
1576+ > Stability: 1 - Experimental
1577+
1578+ ```c
1579+ NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
1580+ napi_env env,
1581+ void (*fun)(void* arg, void(* cb)(void*), void* cbarg),
1582+ void* arg,
1583+ napi_async_cleanup_hook_handle* remove_handle);
1584+ ```
1585+
1586+ Registers `fun` as a function to be run with the `arg` parameter once the
1587+ current Node.js environment exits. Unlike [`napi_add_env_cleanup_hook`][],
1588+ the hook is allowed to be asynchronous in this case, and must invoke the passed
1589+ `cb()` function with `cbarg` once all asynchronous activity is finished.
1590+
1591+ Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
1592+
1593+ If `remove_handle` is not `NULL`, an opaque value will be stored in it
1594+ that must later be passed to [`napi_remove_async_cleanup_hook`][],
1595+ regardless of whether the hook has already been invoked.
1596+ Typically, that happens when the resource for which this hook was added
1597+ is being torn down anyway.
1598+
1599+ #### napi_remove_async_cleanup_hook
1600+ <!-- YAML
1601+ added: REPLACEME
1602+ -->
1603+
1604+ > Stability: 1 - Experimental
1605+
1606+ ```c
1607+ NAPI_EXTERN napi_status napi_remove_async_cleanup_hook(
1608+ napi_env env,
1609+ napi_async_cleanup_hook_handle remove_handle);
1610+ ```
1611+
1612+ Unregisters the cleanup hook corresponding to `remove_handle`. This will prevent
1613+ the hook from being executed, unless it has already started executing.
1614+ This must be called on any `napi_async_cleanup_hook_handle` value retrieved
1615+ from [`napi_add_async_cleanup_hook`][].
1616+
15691617## Module registration
15701618N-API modules are registered in a manner similar to other modules
15711619except that instead of using the `NODE_MODULE` macro the following
@@ -5659,6 +5707,7 @@ This API may only be called from the main thread.
56595707[`Worker`]: worker_threads.html#worker_threads_class_worker
56605708[`global`]: globals.html#globals_global
56615709[`init` hooks]: async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource
5710+ [`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook
56625711[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
56635712[`napi_add_finalizer`]: #n_api_napi_add_finalizer
56645713[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
@@ -5699,6 +5748,8 @@ This API may only be called from the main thread.
56995748[`napi_queue_async_work`]: #n_api_napi_queue_async_work
57005749[`napi_reference_ref`]: #n_api_napi_reference_ref
57015750[`napi_reference_unref`]: #n_api_napi_reference_unref
5751+ [`napi_remove_async_cleanup_hook`]: #n_api_napi_remove_async_cleanup_hook
5752+ [`napi_remove_env_cleanup_hook`]: #n_api_napi_remove_env_cleanup_hook
57025753[`napi_set_instance_data`]: #n_api_napi_set_instance_data
57035754[`napi_set_property`]: #n_api_napi_set_property
57045755[`napi_threadsafe_function_call_js`]: #n_api_napi_threadsafe_function_call_js
0 commit comments