@@ -1550,10 +1550,12 @@ and will lead the process to abort.
1550
1550
The hooks will be called in reverse order, i.e. the most recently added one
1551
1551
will be called first.
1552
1552
1553
- Removing this hook can be done by using `napi_remove_env_cleanup_hook`.
1553
+ Removing this hook can be done by using [ `napi_remove_env_cleanup_hook`][] .
1554
1554
Typically, that happens when the resource for which this hook was added
1555
1555
is being torn down anyway.
1556
1556
1557
+ For asynchronous cleanup, [`napi_add_async_cleanup_hook`][] is available.
1558
+
1557
1559
#### napi_remove_env_cleanup_hook
1558
1560
<!-- YAML
1559
1561
added: v10.2.0
@@ -1573,6 +1575,52 @@ need to be exact matches.
1573
1575
The function must have originally been registered
1574
1576
with `napi_add_env_cleanup_hook`, otherwise the process will abort.
1575
1577
1578
+ #### napi_add_async_cleanup_hook
1579
+ <!-- YAML
1580
+ added: REPLACEME
1581
+ -->
1582
+
1583
+ > Stability: 1 - Experimental
1584
+
1585
+ ```c
1586
+ NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
1587
+ napi_env env,
1588
+ void (*fun)(void* arg, void(* cb)(void*), void* cbarg),
1589
+ void* arg,
1590
+ napi_async_cleanup_hook_handle* remove_handle);
1591
+ ```
1592
+
1593
+ Registers `fun` as a function to be run with the `arg` parameter once the
1594
+ current Node.js environment exits. Unlike [`napi_add_env_cleanup_hook`][],
1595
+ the hook is allowed to be asynchronous in this case, and must invoke the passed
1596
+ `cb()` function with `cbarg` once all asynchronous activity is finished.
1597
+
1598
+ Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
1599
+
1600
+ If `remove_handle` is not `NULL`, an opaque value will be stored in it
1601
+ that must later be passed to [`napi_remove_async_cleanup_hook`][],
1602
+ regardless of whether the hook has already been invoked.
1603
+ Typically, that happens when the resource for which this hook was added
1604
+ is being torn down anyway.
1605
+
1606
+ #### napi_remove_async_cleanup_hook
1607
+ <!-- YAML
1608
+ added: REPLACEME
1609
+ -->
1610
+
1611
+ > Stability: 1 - Experimental
1612
+
1613
+ ```c
1614
+ NAPI_EXTERN napi_status napi_remove_async_cleanup_hook(
1615
+ napi_env env,
1616
+ napi_async_cleanup_hook_handle remove_handle);
1617
+ ```
1618
+
1619
+ Unregisters the cleanup hook corresponding to `remove_handle`. This will prevent
1620
+ the hook from being executed, unless it has already started executing.
1621
+ This must be called on any `napi_async_cleanup_hook_handle` value retrieved
1622
+ from [`napi_add_async_cleanup_hook`][].
1623
+
1576
1624
## Module registration
1577
1625
N-API modules are registered in a manner similar to other modules
1578
1626
except that instead of using the `NODE_MODULE` macro the following
@@ -5704,6 +5752,7 @@ This API may only be called from the main thread.
5704
5752
[`Worker`]: worker_threads.html#worker_threads_class_worker
5705
5753
[`global`]: globals.html#globals_global
5706
5754
[`init` hooks]: async_hooks.html#async_hooks_init_asyncid_type_triggerasyncid_resource
5755
+ [`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook
5707
5756
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
5708
5757
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5709
5758
[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
@@ -5744,6 +5793,8 @@ This API may only be called from the main thread.
5744
5793
[`napi_queue_async_work`]: #n_api_napi_queue_async_work
5745
5794
[`napi_reference_ref`]: #n_api_napi_reference_ref
5746
5795
[`napi_reference_unref`]: #n_api_napi_reference_unref
5796
+ [`napi_remove_async_cleanup_hook`]: #n_api_napi_remove_async_cleanup_hook
5797
+ [`napi_remove_env_cleanup_hook`]: #n_api_napi_remove_env_cleanup_hook
5747
5798
[`napi_set_instance_data`]: #n_api_napi_set_instance_data
5748
5799
[`napi_set_property`]: #n_api_napi_set_property
5749
5800
[`napi_threadsafe_function_call_js`]: #n_api_napi_threadsafe_function_call_js
0 commit comments