@@ -626,6 +626,15 @@ typedef struct {
626
626
} napi_type_tag;
627
627
```
628
628
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
+
629
638
### N-API callback types
630
639
631
640
#### napi_callback_info
@@ -754,6 +763,30 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
754
763
Unless for reasons discussed in [Object Lifetime Management][], creating a
755
764
handle and/or callback scope inside the function body is not necessary.
756
765
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
+
757
790
## Error handling
758
791
759
792
N-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.
1583
1616
#### napi_add_async_cleanup_hook
1584
1617
<!-- YAML
1585
1618
added: REPLACEME
1619
+ changes:
1620
+ - version: REPLACEME
1621
+ pr-url: https://github.com/nodejs/node/pull/34819
1622
+ description: Changed signature of the `hook` callback.
1586
1623
-->
1587
1624
1588
1625
> Stability: 1 - Experimental
1589
1626
1590
1627
```c
1591
1628
NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
1592
1629
napi_env env,
1593
- void (*fun)(void* arg, void(* cb)(void*), void* cbarg) ,
1630
+ napi_async_cleanup_hook hook ,
1594
1631
void* arg,
1595
1632
napi_async_cleanup_hook_handle* remove_handle);
1596
1633
```
1597
1634
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.
1602
1646
1603
1647
Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
1604
1648
@@ -1611,19 +1655,25 @@ is being torn down anyway.
1611
1655
#### napi_remove_async_cleanup_hook
1612
1656
<!-- YAML
1613
1657
added: REPLACEME
1658
+ changes:
1659
+ - version: REPLACEME
1660
+ pr-url: https://github.com/nodejs/node/pull/34819
1661
+ description: Removed `env` parameter.
1614
1662
-->
1615
1663
1616
1664
> Stability: 1 - Experimental
1617
1665
1618
1666
```c
1619
1667
NAPI_EXTERN napi_status napi_remove_async_cleanup_hook(
1620
- napi_env env,
1621
1668
napi_async_cleanup_hook_handle remove_handle);
1622
1669
```
1623
1670
1671
+ * `[in] remove_handle`: The handle to an asynchronous cleanup hook that was
1672
+ created with [`napi_add_async_cleanup_hook`][].
1673
+
1624
1674
Unregisters the cleanup hook corresponding to `remove_handle`. This will prevent
1625
1675
the 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
1627
1677
from [`napi_add_async_cleanup_hook`][].
1628
1678
1629
1679
## Module registration
@@ -5731,6 +5781,7 @@ This API may only be called from the main thread.
5731
5781
[`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook
5732
5782
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
5733
5783
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5784
+ [`napi_async_cleanup_hook`]: #n_api_napi_async_cleanup_hook
5734
5785
[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
5735
5786
[`napi_async_init`]: #n_api_napi_async_init
5736
5787
[`napi_callback`]: #n_api_napi_callback
0 commit comments