Skip to content

Commit 1f4a5bc

Browse files
joyeecheungtargos
authored andcommitted
timers: refactor timer callback initialization
This patch: - Moves the timer callback initialization into bootstrap/node.js, documents when they will be called, and make the dependency on process._tickCallback explicit. - Moves the initialization of tick callbacks and timer callbacks to the end of the bootstrap to make sure the operations done before those initializations are synchronous. - Moves more internals into internal/timers.js from timers.js. PR-URL: #26583 Refs: #26546 Reviewed-By: Anna Henningsen <[email protected]>
1 parent ebb0c2a commit 1f4a5bc

File tree

4 files changed

+469
-430
lines changed

4 files changed

+469
-430
lines changed

lib/internal/bootstrap/node.js

+31-19
Original file line numberDiff line numberDiff line change
@@ -113,24 +113,6 @@ if (isMainThread) {
113113
process.exit = wrapped.exit;
114114
}
115115

116-
const {
117-
emitWarning
118-
} = require('internal/process/warning');
119-
120-
process.emitWarning = emitWarning;
121-
122-
const {
123-
nextTick,
124-
runNextTicks
125-
} = require('internal/process/task_queues').setupTaskQueue();
126-
127-
process.nextTick = nextTick;
128-
// Used to emulate a tick manually in the JS land.
129-
// A better name for this function would be `runNextTicks` but
130-
// it has been exposed to the process object so we keep this legacy name
131-
// TODO(joyeecheung): either remove it or make it public
132-
process._tickCallback = runNextTicks;
133-
134116
const credentials = internalBinding('credentials');
135117
if (credentials.implementsPosixCredentials) {
136118
process.getuid = credentials.getuid;
@@ -186,6 +168,11 @@ if (config.hasInspector) {
186168
internalBinding('inspector').registerAsyncHook(enable, disable);
187169
}
188170

171+
const {
172+
setupTaskQueue,
173+
queueMicrotask
174+
} = require('internal/process/task_queues');
175+
189176
if (!config.noBrowserGlobals) {
190177
// Override global console from the one provided by the VM
191178
// to the one implemented by Node.js
@@ -317,6 +304,32 @@ Object.defineProperty(process, 'features', {
317304
hasUncaughtExceptionCaptureCallback;
318305
}
319306

307+
const { emitWarning } = require('internal/process/warning');
308+
process.emitWarning = emitWarning;
309+
310+
// We initialize the tick callbacks and the timer callbacks last during
311+
// bootstrap to make sure that any operation done before this are synchronous.
312+
// If any ticks or timers are scheduled before this they are unlikely to work.
313+
{
314+
const { nextTick, runNextTicks } = setupTaskQueue();
315+
process.nextTick = nextTick;
316+
// Used to emulate a tick manually in the JS land.
317+
// A better name for this function would be `runNextTicks` but
318+
// it has been exposed to the process object so we keep this legacy name
319+
// TODO(joyeecheung): either remove it or make it public
320+
process._tickCallback = runNextTicks;
321+
322+
const { getTimerCallbacks } = require('internal/timers');
323+
const { setupTimers } = internalBinding('timers');
324+
const { processImmediate, processTimers } = getTimerCallbacks(runNextTicks);
325+
// Sets two per-Environment callbacks that will be run from libuv:
326+
// - processImmediate will be run in the callback of the per-Environment
327+
// check handle.
328+
// - processTimers will be run in the callback of the per-Environment timer.
329+
setupTimers(processImmediate, processTimers);
330+
// Note: only after this point are the timers effective
331+
}
332+
320333
function setupProcessObject() {
321334
const EventEmitter = require('events');
322335
const origProcProto = Object.getPrototypeOf(process);
@@ -430,7 +443,6 @@ function setupQueueMicrotask() {
430443
get() {
431444
process.emitWarning('queueMicrotask() is experimental.',
432445
'ExperimentalWarning');
433-
const { queueMicrotask } = require('internal/process/task_queues');
434446

435447
Object.defineProperty(global, 'queueMicrotask', {
436448
value: queueMicrotask,

0 commit comments

Comments
 (0)