init.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict'
  2. const timers = require('timers')
  3. const util = require('util')
  4. process.atomBinding = require('@electron/internal/common/atom-binding-setup')(process.binding, process.type)
  5. // setImmediate and process.nextTick makes use of uv_check and uv_prepare to
  6. // run the callbacks, however since we only run uv loop on requests, the
  7. // callbacks wouldn't be called until something else activated the uv loop,
  8. // which would delay the callbacks for arbitrary long time. So we should
  9. // initiatively activate the uv loop once setImmediate and process.nextTick is
  10. // called.
  11. const wrapWithActivateUvLoop = function (func) {
  12. return wrap(func, function (func) {
  13. return function () {
  14. process.activateUvLoop()
  15. return func.apply(this, arguments)
  16. }
  17. })
  18. }
  19. function wrap (func, wrapper) {
  20. const wrapped = wrapper(func)
  21. if (func[util.promisify.custom]) {
  22. wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom])
  23. }
  24. return wrapped
  25. }
  26. process.nextTick = wrapWithActivateUvLoop(process.nextTick)
  27. global.setImmediate = wrapWithActivateUvLoop(timers.setImmediate)
  28. global.clearImmediate = timers.clearImmediate
  29. if (process.type === 'browser') {
  30. // setTimeout needs to update the polling timeout of the event loop, when
  31. // called under Chromium's event loop the node's event loop won't get a chance
  32. // to update the timeout, so we have to force the node's event loop to
  33. // recalculate the timeout in browser process.
  34. global.setTimeout = wrapWithActivateUvLoop(timers.setTimeout)
  35. global.setInterval = wrapWithActivateUvLoop(timers.setInterval)
  36. }
  37. if (process.platform === 'win32') {
  38. // Always returns EOF for stdin stream.
  39. const { Readable } = require('stream')
  40. const stdin = new Readable()
  41. stdin.push(null)
  42. process.__defineGetter__('stdin', function () {
  43. return stdin
  44. })
  45. }