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 <anna@addaleax.net>
  • Loading branch information
joyeecheung committed Mar 18, 2019
1 parent 7866508 commit 1a6fb71
Show file tree
Hide file tree
Showing 4 changed files with 469 additions and 433 deletions.
53 changes: 31 additions & 22 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,6 @@ if (isMainThread) {
process.exit = wrapped.exit;
}

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

process.emitWarning = emitWarning;

const {
setupTaskQueue,
queueMicrotask
} = require('internal/process/task_queues');
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 credentials = internalBinding('credentials');
if (credentials.implementsPosixCredentials) {
process.getuid = credentials.getuid;
Expand Down Expand Up @@ -180,6 +158,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 @@ -274,6 +257,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
Loading

0 comments on commit 1a6fb71

Please sign in to comment.