napi_add_env_cleanup_hook
NODE_EXTERN napi_status napi_add_env_cleanup_hook(node_api_basic_env env,
napi_cleanup_hook fun,
void* arg); 注册 fun 作为一个函数,在当前 Node.js 环境退出时使用 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.】