@@ -1543,10 +1543,12 @@ and will lead the process to abort.
1543
1543
The hooks will be called in reverse order, i.e. the most recently added one
1544
1544
will be called first.
1545
1545
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`][] .
1547
1547
Typically, that happens when the resource for which this hook was added
1548
1548
is being torn down anyway.
1549
1549
1550
+ For asynchronous cleanup, [`napi_add_async_cleanup_hook`][] is available.
1551
+
1550
1552
#### napi_remove_env_cleanup_hook
1551
1553
<!-- YAML
1552
1554
added: v10.2.0
@@ -1566,6 +1568,52 @@ need to be exact matches.
1566
1568
The function must have originally been registered
1567
1569
with `napi_add_env_cleanup_hook`, otherwise the process will abort.
1568
1570
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
+
1569
1617
## Module registration
1570
1618
N-API modules are registered in a manner similar to other modules
1571
1619
except that instead of using the `NODE_MODULE` macro the following
@@ -5659,6 +5707,7 @@ This API may only be called from the main thread.
5659
5707
[`Worker`]: worker_threads.html#worker_threads_class_worker
5660
5708
[`global`]: globals.html#globals_global
5661
5709
[`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
5662
5711
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
5663
5712
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5664
5713
[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
@@ -5699,6 +5748,8 @@ This API may only be called from the main thread.
5699
5748
[`napi_queue_async_work`]: #n_api_napi_queue_async_work
5700
5749
[`napi_reference_ref`]: #n_api_napi_reference_ref
5701
5750
[`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
5702
5753
[`napi_set_instance_data`]: #n_api_napi_set_instance_data
5703
5754
[`napi_set_property`]: #n_api_napi_set_property
5704
5755
[`napi_threadsafe_function_call_js`]: #n_api_napi_threadsafe_function_call_js
0 commit comments