@@ -623,6 +623,15 @@ typedef struct {
623
623
} napi_type_tag;
624
624
```
625
625
626
+ #### napi_async_cleanup_hook_handle
627
+ <!-- YAML
628
+ added: REPLACEME
629
+ -->
630
+
631
+ An opaque value returned by [`napi_add_async_cleanup_hook`][]. It must be passed
632
+ to [`napi_remove_async_cleanup_hook`][] when the chain of asynchronous cleanup
633
+ events completes.
634
+
626
635
### N-API callback types
627
636
628
637
#### napi_callback_info
@@ -751,6 +760,30 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
751
760
Unless for reasons discussed in [Object Lifetime Management][], creating a
752
761
handle and/or callback scope inside the function body is not necessary.
753
762
763
+ #### napi_async_cleanup_hook
764
+ <!-- YAML
765
+ added: REPLACEME
766
+ -->
767
+
768
+ Function pointer used with [`napi_add_async_cleanup_hook`][]. It will be called
769
+ when the environment is being torn down.
770
+
771
+ Callback functions must satisfy the following signature:
772
+
773
+ ```c
774
+ typedef void (*napi_async_cleanup_hook)(napi_async_cleanup_hook_handle handle,
775
+ void* data);
776
+ ```
777
+
778
+ * `[in] handle`: The handle that must be passed to
779
+ [`napi_remove_async_cleanup_hook`][] after completion of the asynchronous
780
+ cleanup.
781
+ * `[in] data`: The data that was passed to [`napi_add_async_cleanup_hook`][].
782
+
783
+ The body of the function should initiate the asynchronous cleanup actions at the
784
+ end of which `handle` must be passed in a call to
785
+ [`napi_remove_async_cleanup_hook`][].
786
+
754
787
## Error handling
755
788
756
789
N-API uses both return values and JavaScript exceptions for error handling.
@@ -1580,22 +1613,33 @@ with `napi_add_env_cleanup_hook`, otherwise the process will abort.
1580
1613
#### napi_add_async_cleanup_hook
1581
1614
<!-- YAML
1582
1615
added: v14.8.0
1616
+ changes:
1617
+ - version: REPLACEME
1618
+ pr-url: https://github.com/nodejs/node/pull/34819
1619
+ description: Changed signature of the `hook` callback.
1583
1620
-->
1584
1621
1585
1622
> Stability: 1 - Experimental
1586
1623
1587
1624
```c
1588
1625
NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
1589
1626
napi_env env,
1590
- void (*fun)(void* arg, void(* cb)(void*), void* cbarg) ,
1627
+ napi_async_cleanup_hook hook ,
1591
1628
void* arg,
1592
1629
napi_async_cleanup_hook_handle* remove_handle);
1593
1630
```
1594
1631
1595
- Registers `fun` as a function to be run with the `arg` parameter once the
1596
- current Node.js environment exits. Unlike [`napi_add_env_cleanup_hook`][],
1597
- the hook is allowed to be asynchronous in this case, and must invoke the passed
1598
- `cb()` function with `cbarg` once all asynchronous activity is finished.
1632
+ * `[in] env`: The environment that the API is invoked under.
1633
+ * `[in] hook`: The function pointer to call at environment teardown.
1634
+ * `[in] arg`: The pointer to pass to `hook` when it gets called.
1635
+ * `[out] remove_handle`: Optional handle that refers to the asynchronous cleanup
1636
+ hook.
1637
+
1638
+ Registers `hook`, which is a function of type [`napi_async_cleanup_hook`][], as
1639
+ a function to be run with the `remove_handle` and `arg` parameters once the
1640
+ current Node.js environment exits.
1641
+
1642
+ Unlike [`napi_add_env_cleanup_hook`][], the hook is allowed to be asynchronous.
1599
1643
1600
1644
Otherwise, behavior generally matches that of [`napi_add_env_cleanup_hook`][].
1601
1645
@@ -1608,19 +1652,25 @@ is being torn down anyway.
1608
1652
#### napi_remove_async_cleanup_hook
1609
1653
<!-- YAML
1610
1654
added: v14.8.0
1655
+ changes:
1656
+ - version: REPLACEME
1657
+ pr-url: https://github.com/nodejs/node/pull/34819
1658
+ description: Removed `env` parameter.
1611
1659
-->
1612
1660
1613
1661
> Stability: 1 - Experimental
1614
1662
1615
1663
```c
1616
1664
NAPI_EXTERN napi_status napi_remove_async_cleanup_hook(
1617
- napi_env env,
1618
1665
napi_async_cleanup_hook_handle remove_handle);
1619
1666
```
1620
1667
1668
+ * `[in] remove_handle`: The handle to an asynchronous cleanup hook that was
1669
+ created with [`napi_add_async_cleanup_hook`][].
1670
+
1621
1671
Unregisters the cleanup hook corresponding to `remove_handle`. This will prevent
1622
1672
the hook from being executed, unless it has already started executing.
1623
- This must be called on any `napi_async_cleanup_hook_handle` value retrieved
1673
+ This must be called on any `napi_async_cleanup_hook_handle` value obtained
1624
1674
from [`napi_add_async_cleanup_hook`][].
1625
1675
1626
1676
## Module registration
@@ -5757,6 +5807,7 @@ This API may only be called from the main thread.
5757
5807
[`napi_add_async_cleanup_hook`]: #n_api_napi_add_async_cleanup_hook
5758
5808
[`napi_add_env_cleanup_hook`]: #n_api_napi_add_env_cleanup_hook
5759
5809
[`napi_add_finalizer`]: #n_api_napi_add_finalizer
5810
+ [`napi_async_cleanup_hook`]: #n_api_napi_async_cleanup_hook
5760
5811
[`napi_async_complete_callback`]: #n_api_napi_async_complete_callback
5761
5812
[`napi_async_init`]: #n_api_napi_async_init
5762
5813
[`napi_callback`]: #n_api_napi_callback
0 commit comments