napi_add_env_cleanup_hook
NODE_EXTERN napi_status napi_add_env_cleanup_hook(node_api_nogc_env env,
napi_cleanup_hook fun,
void* arg);
一旦当前 Node.js 环境退出,将 fun
注册为要使用 arg
参数运行的函数。
¥Registers fun
as a function to be run with the arg
parameter once the
current Node.js environment exits.
可以使用不同的 arg
值安全地多次指定一个函数。在这种情况下,它也会被多次调用。不允许多次提供相同的 fun
和 arg
值,否则会导致进程中止。
¥A function can safely be specified multiple times with different
arg
values. In that case, it will be called multiple times as well.
Providing the same fun
and arg
values multiple times is not allowed
and will lead the process to abort.
钩子将以相反的顺序调用,即最近添加的钩子将首先被调用。
¥The hooks will be called in reverse order, i.e. the most recently added one will be called first.
可以使用 napi_remove_env_cleanup_hook
删除此钩子。通常,当为其添加此钩子的资源无论如何都被拆除时,就会发生这种情况。
¥Removing this hook can be done by using napi_remove_env_cleanup_hook
.
Typically, that happens when the resource for which this hook was added
is being torn down anyway.
对于异步清理,napi_add_async_cleanup_hook
可用。
¥For asynchronous cleanup, napi_add_async_cleanup_hook
is available.