fix: override the timers module impls to activate the uv loop (#18948)

This commit is contained in:
Samuel Attard 2019-06-24 10:18:29 -07:00 committed by GitHub
parent fb01c94511
commit 764be844ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 8 deletions

View file

@ -1,8 +1,9 @@
import * as timers from 'timers'
import * as util from 'util'
import { electronBindingSetup } from '@electron/internal/common/electron-binding-setup'
const timers = require('timers')
process.electronBinding = electronBindingSetup(process._linkedBinding, process.type)
type AnyFn = (...args: any[]) => any
@ -38,16 +39,20 @@ function wrap <T extends AnyFn> (func: T, wrapper: (fn: AnyFn) => T) {
process.nextTick = wrapWithActivateUvLoop(process.nextTick)
global.setImmediate = wrapWithActivateUvLoop(timers.setImmediate)
global.setImmediate = timers.setImmediate = wrapWithActivateUvLoop(timers.setImmediate)
global.clearImmediate = timers.clearImmediate
// setTimeout needs to update the polling timeout of the event loop, when
// called under Chromium's event loop the node's event loop won't get a chance
// to update the timeout, so we have to force the node's event loop to
// recalculate the timeout in browser process.
timers.setTimeout = wrapWithActivateUvLoop(timers.setTimeout)
timers.setInterval = wrapWithActivateUvLoop(timers.setInterval)
// Only override the global setTimeout/setInterval impls in the browser process
if (process.type === 'browser') {
// setTimeout needs to update the polling timeout of the event loop, when
// called under Chromium's event loop the node's event loop won't get a chance
// to update the timeout, so we have to force the node's event loop to
// recalculate the timeout in browser process.
global.setTimeout = wrapWithActivateUvLoop(timers.setTimeout)
global.setInterval = wrapWithActivateUvLoop(timers.setInterval)
global.setTimeout = timers.setTimeout
global.setInterval = timers.setInterval
}
if (process.platform === 'win32') {