Skip to content

Commit

Permalink
timers: refactor timer callback initialization
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
joyeecheung authored and targos committed Mar 27, 2019
1 parent ebb0c2a commit 1f4a5bc
Show file tree
Hide file tree
Showing 4 changed files with 469 additions and 430 deletions.
50 changes: 31 additions & 19 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,6 @@ if (isMainThread) {
process.exit = wrapped.exit;
}

const {
emitWarning
} = require('internal/process/warning');

process.emitWarning = emitWarning;

const {
nextTick,
runNextTicks
} = require('internal/process/task_queues').setupTaskQueue();

process.nextTick = nextTick;
// Used to emulate a tick manually in the JS land.
// A better name for this function would be `runNextTicks` but
// it has been exposed to the process object so we keep this legacy name
// TODO(joyeecheung): either remove it or make it public
process._tickCallback = runNextTicks;

const credentials = internalBinding('credentials');
if (credentials.implementsPosixCredentials) {
process.getuid = credentials.getuid;
Expand Down Expand Up @@ -186,6 +168,11 @@ if (config.hasInspector) {
internalBinding('inspector').registerAsyncHook(enable, disable);
}

const {
setupTaskQueue,
queueMicrotask
} = require('internal/process/task_queues');

if (!config.noBrowserGlobals) {
// Override global console from the one provided by the VM
// to the one implemented by Node.js
Expand Down Expand Up @@ -317,6 +304,32 @@ Object.defineProperty(process, 'features', {
hasUncaughtExceptionCaptureCallback;
}

const { emitWarning } = require('internal/process/warning');
process.emitWarning = emitWarning;

// We initialize the tick callbacks and the timer callbacks last during
// bootstrap to make sure that any operation done before this are synchronous.
// If any ticks or timers are scheduled before this they are unlikely to work.
{
const { nextTick, runNextTicks } = setupTaskQueue();
process.nextTick = nextTick;
// Used to emulate a tick manually in the JS land.
// A better name for this function would be `runNextTicks` but
// it has been exposed to the process object so we keep this legacy name
// TODO(joyeecheung): either remove it or make it public
process._tickCallback = runNextTicks;

const { getTimerCallbacks } = require('internal/timers');
const { setupTimers } = internalBinding('timers');
const { processImmediate, processTimers } = getTimerCallbacks(runNextTicks);
// Sets two per-Environment callbacks that will be run from libuv:
// - processImmediate will be run in the callback of the per-Environment
// check handle.
// - processTimers will be run in the callback of the per-Environment timer.
setupTimers(processImmediate, processTimers);
// Note: only after this point are the timers effective
}

function setupProcessObject() {
const EventEmitter = require('events');
const origProcProto = Object.getPrototypeOf(process);
Expand Down Expand Up @@ -430,7 +443,6 @@ function setupQueueMicrotask() {
get() {
process.emitWarning('queueMicrotask() is experimental.',
'ExperimentalWarning');
const { queueMicrotask } = require('internal/process/task_queues');

Object.defineProperty(global, 'queueMicrotask', {
value: queueMicrotask,
Expand Down
Loading

0 comments on commit 1f4a5bc

Please sign in to comment.