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.
  • Loading branch information
joyeecheung committed Mar 18, 2019
1 parent 4ac4f32 commit 4bc8ffa
Show file tree
Hide file tree
Showing 4 changed files with 471 additions and 429 deletions.
51 changes: 33 additions & 18 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,6 @@ if (isMainThread) {
process.exit = wrapped.exit;
}

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

process.emitWarning = emitWarning;

const {
nextTick,
runNextTicks
} = NativeModule.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 @@ -274,6 +256,39 @@ Object.defineProperty(process, 'features', {
hasUncaughtExceptionCaptureCallback;
}

const {
emitWarning
} = NativeModule.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
} = NativeModule.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 { getTimerCallbacks } = NativeModule.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 = NativeModule.require('events');
const origProcProto = Object.getPrototypeOf(process);
Expand Down
Loading

0 comments on commit 4bc8ffa

Please sign in to comment.