timers: allow Immediates to be unrefed

Refactor Immediates handling to allow for them to be unrefed, similar
to setTimeout, but without extra handles.

Document the new `immediate.ref()` and `immediate.unref()` methods.

Add SetImmediateUnref on the C++ side.

Backport-PR-URL: https://github.com/nodejs/node/pull/19265
PR-URL: https://github.com/nodejs/node/pull/18139
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Anatoli Papirovski authored and Myles Borins committed 2018-05-01 17:16:29 -04:00
1 parent 84e0a03727
commit e15f57745d
1 file changed
+9
@@ -24,6 +24,15 @@ void* SetImmediate(napi_env env, T&& cb) {
assert(cb() != nullptr);
});
// Idle handle is needed only to stop the event loop from blocking in poll.
uv_idle_t* idle = new uv_idle_t;
uv_idle_init(loop, idle);
uv_idle_start(idle, [](uv_idle_t* idle) {
uv_close(reinterpret_cast<uv_handle_t*>(idle), [](uv_handle_t* handle) {
delete reinterpret_cast<uv_check_t*>(handle);
});
});
return nullptr;
}