Skip to content

Commit 1a6fb71

Browse files
committed
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 7866508 commit 1a6fb71

File tree

4 files changed

+469
-433
lines changed

4 files changed

+469
-433
lines changed

lib/internal/bootstrap/node.js

+31-22
Original file line numberDiff line numberDiff line change
@@ -113,28 +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-
setupTaskQueue,
124-
queueMicrotask
125-
} = require('internal/process/task_queues');
126-
const {
127-
nextTick,
128-
runNextTicks
129-
} = setupTaskQueue();
130-
131-
process.nextTick = nextTick;
132-
// Used to emulate a tick manually in the JS land.
133-
// A better name for this function would be `runNextTicks` but
134-
// it has been exposed to the process object so we keep this legacy name
135-
// TODO(joyeecheung): either remove it or make it public
136-
process._tickCallback = runNextTicks;
137-
138116
const credentials = internalBinding('credentials');
139117
if (credentials.implementsPosixCredentials) {
140118
process.getuid = credentials.getuid;
@@ -180,6 +158,11 @@ if (config.hasInspector) {
180158
internalBinding('inspector').registerAsyncHook(enable, disable);
181159
}
182160

161+
const {
162+
setupTaskQueue,
163+
queueMicrotask
164+
} = require('internal/process/task_queues');
165+
183166
if (!config.noBrowserGlobals) {
184167
// Override global console from the one provided by the VM
185168
// to the one implemented by Node.js
@@ -274,6 +257,32 @@ Object.defineProperty(process, 'features', {
274257
hasUncaughtExceptionCaptureCallback;
275258
}
276259

260+
const { emitWarning } = require('internal/process/warning');
261+
process.emitWarning = emitWarning;
262+
263+
// We initialize the tick callbacks and the timer callbacks last during
264+
// bootstrap to make sure that any operation done before this are synchronous.
265+
// If any ticks or timers are scheduled before this they are unlikely to work.
266+
{
267+
const { nextTick, runNextTicks } = setupTaskQueue();
268+
process.nextTick = nextTick;
269+
// Used to emulate a tick manually in the JS land.
270+
// A better name for this function would be `runNextTicks` but
271+
// it has been exposed to the process object so we keep this legacy name
272+
// TODO(joyeecheung): either remove it or make it public
273+
process._tickCallback = runNextTicks;
274+
275+
const { getTimerCallbacks } = require('internal/timers');
276+
const { setupTimers } = internalBinding('timers');
277+
const { processImmediate, processTimers } = getTimerCallbacks(runNextTicks);
278+
// Sets two per-Environment callbacks that will be run from libuv:
279+
// - processImmediate will be run in the callback of the per-Environment
280+
// check handle.
281+
// - processTimers will be run in the callback of the per-Environment timer.
282+
setupTimers(processImmediate, processTimers);
283+
// Note: only after this point are the timers effective
284+
}
285+
277286
function setupProcessObject() {
278287
const EventEmitter = require('events');
279288
const origProcProto = Object.getPrototypeOf(process);

0 commit comments

Comments
 (0)