Skip to content

Commit 466213d

Browse files
bnoordhuistargos
authored andcommitted
n-api: simplify uv_idle wrangling
uv_idle_init(), uv_idle_start() and uv_idle_stop() always succeed. Remove the superfluous error handling. Refs: libuv/libuv#2803 PR-URL: #32997 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent c5a2f9a commit 466213d

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

src/node_api.cc

+13-33
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ class ThreadSafeFunction : public node::AsyncResource {
250250
if (max_queue_size > 0) {
251251
cond = std::make_unique<node::ConditionVariable>();
252252
}
253-
if ((max_queue_size == 0 || cond) &&
254-
uv_idle_init(loop, &idle) == 0) {
253+
if (max_queue_size == 0 || cond) {
254+
CHECK_EQ(0, uv_idle_init(loop, &idle));
255255
return napi_ok;
256256
}
257257

@@ -291,7 +291,6 @@ class ThreadSafeFunction : public node::AsyncResource {
291291
void DispatchOne() {
292292
void* data = nullptr;
293293
bool popped_value = false;
294-
bool idle_stop_failed = false;
295294

296295
{
297296
node::Mutex::ScopedLock lock(this->mutex);
@@ -317,43 +316,24 @@ class ThreadSafeFunction : public node::AsyncResource {
317316
}
318317
CloseHandlesAndMaybeDelete();
319318
} else {
320-
if (uv_idle_stop(&idle) != 0) {
321-
idle_stop_failed = true;
322-
}
319+
CHECK_EQ(0, uv_idle_stop(&idle));
323320
}
324321
}
325322
}
326323
}
327324

328-
if (popped_value || idle_stop_failed) {
325+
if (popped_value) {
329326
v8::HandleScope scope(env->isolate);
330327
CallbackScope cb_scope(this);
331-
332-
if (idle_stop_failed) {
333-
CHECK(napi_throw_error(env,
334-
"ERR_NAPI_TSFN_STOP_IDLE_LOOP",
335-
"Failed to stop the idle loop") == napi_ok);
336-
} else {
337-
napi_value js_callback = nullptr;
338-
if (!ref.IsEmpty()) {
339-
v8::Local<v8::Function> js_cb =
340-
v8::Local<v8::Function>::New(env->isolate, ref);
341-
js_callback = v8impl::JsValueFromV8LocalValue(js_cb);
342-
}
343-
env->CallIntoModuleThrow([&](napi_env env) {
344-
call_js_cb(env, js_callback, context, data);
345-
});
328+
napi_value js_callback = nullptr;
329+
if (!ref.IsEmpty()) {
330+
v8::Local<v8::Function> js_cb =
331+
v8::Local<v8::Function>::New(env->isolate, ref);
332+
js_callback = v8impl::JsValueFromV8LocalValue(js_cb);
346333
}
347-
}
348-
}
349-
350-
void MaybeStartIdle() {
351-
if (uv_idle_start(&idle, IdleCb) != 0) {
352-
v8::HandleScope scope(env->isolate);
353-
CallbackScope cb_scope(this);
354-
CHECK(napi_throw_error(env,
355-
"ERR_NAPI_TSFN_START_IDLE_LOOP",
356-
"Failed to start the idle loop") == napi_ok);
334+
env->CallIntoModuleThrow([&](napi_env env) {
335+
call_js_cb(env, js_callback, context, data);
336+
});
357337
}
358338
}
359339

@@ -435,7 +415,7 @@ class ThreadSafeFunction : public node::AsyncResource {
435415
static void AsyncCb(uv_async_t* async) {
436416
ThreadSafeFunction* ts_fn =
437417
node::ContainerOf(&ThreadSafeFunction::async, async);
438-
ts_fn->MaybeStartIdle();
418+
CHECK_EQ(0, uv_idle_start(&ts_fn->idle, IdleCb));
439419
}
440420

441421
static void Cleanup(void* data) {

0 commit comments

Comments
 (0)