napi_add_async_cleanup_hook


NAPI_EXTERN napi_status napi_add_async_cleanup_hook(
    node_api_basic_env env,
    napi_async_cleanup_hook hook,
    void* arg,
    napi_async_cleanup_hook_handle* remove_handle); 
  • [in] env:调用该 API 时所处的环境。
  • [in] hook:在环境拆卸时要调用的函数指针。
  • [in] arg:在调用 hook 时要传递的指针。
  • [out] remove_handle:可选句柄,指向异步清理钩子。

注册 hook,它是 napi_async_cleanup_hook 类型的函数,作为一个函数在当前 Node.js 环境退出时使用 remove_handlearg 参数运行。

【Registers hook, which is a function of type napi_async_cleanup_hook, as a function to be run with the remove_handle and arg parameters once the current Node.js environment exits.】

napi_add_env_cleanup_hook 不同,该钩子允许异步操作。

【Unlike napi_add_env_cleanup_hook, the hook is allowed to be asynchronous.】

否则,行为通常与napi_add_env_cleanup_hook相同。

【Otherwise, behavior generally matches that of napi_add_env_cleanup_hook.】

如果 remove_handle 不是 NULL,一个不透明的值将存储在其中,之后必须传递给 napi_remove_async_cleanup_hook,无论该钩子是否已经被调用。通常,这种情况发生在为其添加该钩子的资源无论如何都要被拆除时。

【If remove_handle is not NULL, an opaque value will be stored in it that must later be passed to napi_remove_async_cleanup_hook, regardless of whether the hook has already been invoked. Typically, that happens when the resource for which this hook was added is being torn down anyway.】