Skip to content

Commit 4d7f986

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 <cjihrig@gmail.com> Reviewed-By: David Carlier <devnexen@gmail.com> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 58bd92a commit 4d7f986

File tree

1 file changed

+13
-33
lines changed

1 file changed

+13
-33
lines changed

src/node_api.cc

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ class ThreadSafeFunction : public node::AsyncResource {
227227
if (max_queue_size > 0) {
228228
cond = std::make_unique<node::ConditionVariable>();
229229
}
230-
if ((max_queue_size == 0 || cond) &&
231-
uv_idle_init(loop, &idle) == 0) {
230+
if (max_queue_size == 0 || cond) {
231+
CHECK_EQ(0, uv_idle_init(loop, &idle));
232232
return napi_ok;
233233
}
234234

@@ -268,7 +268,6 @@ class ThreadSafeFunction : public node::AsyncResource {
268268
void DispatchOne() {
269269
void* data = nullptr;
270270
bool popped_value = false;
271-
bool idle_stop_failed = false;
272271

273272
{
274273
node::Mutex::ScopedLock lock(this->mutex);
@@ -294,43 +293,24 @@ class ThreadSafeFunction : public node::AsyncResource {
294293
}
295294
CloseHandlesAndMaybeDelete();
296295
} else {
297-
if (uv_idle_stop(&idle) != 0) {
298-
idle_stop_failed = true;
299-
}
296+
CHECK_EQ(0, uv_idle_stop(&idle));
300297
}
301298
}
302299
}
303300
}
304301

305-
if (popped_value || idle_stop_failed) {
302+
if (popped_value) {
306303
v8::HandleScope scope(env->isolate);
307304
CallbackScope cb_scope(this);
308-
309-
if (idle_stop_failed) {
310-
CHECK(napi_throw_error(env,
311-
"ERR_NAPI_TSFN_STOP_IDLE_LOOP",
312-
"Failed to stop the idle loop") == napi_ok);
313-
} else {
314-
napi_value js_callback = nullptr;
315-
if (!ref.IsEmpty()) {
316-
v8::Local<v8::Function> js_cb =
317-
v8::Local<v8::Function>::New(env->isolate, ref);
318-
js_callback = v8impl::JsValueFromV8LocalValue(js_cb);
319-
}
320-
env->CallIntoModuleThrow([&](napi_env env) {
321-
call_js_cb(env, js_callback, context, data);
322-
});
305+
napi_value js_callback = nullptr;
306+
if (!ref.IsEmpty()) {
307+
v8::Local<v8::Function> js_cb =
308+
v8::Local<v8::Function>::New(env->isolate, ref);
309+
js_callback = v8impl::JsValueFromV8LocalValue(js_cb);
323310
}
324-
}
325-
}
326-
327-
void MaybeStartIdle() {
328-
if (uv_idle_start(&idle, IdleCb) != 0) {
329-
v8::HandleScope scope(env->isolate);
330-
CallbackScope cb_scope(this);
331-
CHECK(napi_throw_error(env,
332-
"ERR_NAPI_TSFN_START_IDLE_LOOP",
333-
"Failed to start the idle loop") == napi_ok);
311+
env->CallIntoModuleThrow([&](napi_env env) {
312+
call_js_cb(env, js_callback, context, data);
313+
});
334314
}
335315
}
336316

@@ -412,7 +392,7 @@ class ThreadSafeFunction : public node::AsyncResource {
412392
static void AsyncCb(uv_async_t* async) {
413393
ThreadSafeFunction* ts_fn =
414394
node::ContainerOf(&ThreadSafeFunction::async, async);
415-
ts_fn->MaybeStartIdle();
395+
CHECK_EQ(0, uv_idle_start(&ts_fn->idle, IdleCb));
416396
}
417397

418398
static void Cleanup(void* data) {

0 commit comments

Comments
 (0)