Browse Source

refactor: simplify Node.js event loop with `SpinEventLoop` (#37259)

refactor: simplify Node.js event loop with SpinEventLoop

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <[email protected]>
trop[bot] 2 years ago
parent
commit
c7e9011f73
1 changed files with 3 additions and 31 deletions
  1. 3 31
      shell/app/node_main.cc

+ 3 - 31
shell/app/node_main.cc

@@ -256,37 +256,9 @@ int NodeMain(int argc, char* argv[]) {
     v8::HandleScope scope(isolate);
     node::LoadEnvironment(env, node::StartExecutionCallback{});
 
-    env->set_trace_sync_io(env->options()->trace_sync_io);
-
-    {
-      v8::SealHandleScope seal(isolate);
-      bool more;
-      env->performance_state()->Mark(
-          node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_START);
-      do {
-        uv_run(env->event_loop(), UV_RUN_DEFAULT);
-
-        gin_env.platform()->DrainTasks(isolate);
-
-        more = uv_loop_alive(env->event_loop());
-        if (more && !env->is_stopping())
-          continue;
-
-        if (!uv_loop_alive(env->event_loop())) {
-          EmitBeforeExit(env);
-        }
-
-        // Emit `beforeExit` if the loop became alive either after emitting
-        // event, or after running some callbacks.
-        more = uv_loop_alive(env->event_loop());
-      } while (more && !env->is_stopping());
-      env->performance_state()->Mark(
-          node::performance::NODE_PERFORMANCE_MILESTONE_LOOP_EXIT);
-    }
-
-    env->set_trace_sync_io(false);
-
-    exit_code = node::EmitExit(env);
+    // Potential reasons we get Nothing here may include: the env
+    // is stopping, or the user hooks process.emit('exit').
+    exit_code = node::SpinEventLoop(env).FromMaybe(1);
 
     node::ResetStdio();