diff --git a/atom.gyp b/atom.gyp index f9b4a011f3d2..288785c13d31 100644 --- a/atom.gyp +++ b/atom.gyp @@ -30,6 +30,7 @@ 'common/api/lib/id-weak-map.coffee', 'common/api/lib/screen.coffee', 'common/api/lib/shell.coffee', + 'common/lib/init.coffee', 'renderer/lib/init.coffee', 'renderer/api/lib/ipc.coffee', 'renderer/api/lib/remote.coffee', diff --git a/browser/lib/init.coffee b/browser/lib/init.coffee index d16450f701f4..950d4671ac56 100644 --- a/browser/lib/init.coffee +++ b/browser/lib/init.coffee @@ -1,4 +1,4 @@ -fs = require 'fs' +fs = require 'fs' path = require 'path' # Expose information of current process. @@ -14,12 +14,12 @@ process.argv.splice 1, 1 globalPaths = require('module').globalPaths globalPaths.push path.join process.resourcesPath, 'browser', 'api', 'lib' -# And also common/api/lib -globalPaths.push path.join process.resourcesPath, 'common', 'api', 'lib' - # Do loading in next tick since we still need some initialize work before # native bindings can work. setImmediate -> + # Import common settings. + require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init.js') + if process.platform is 'win32' # Redirect node's console to use our own implementations, since node can not # handle console output when running as GUI program. diff --git a/common/lib/init.coffee b/common/lib/init.coffee new file mode 100644 index 000000000000..48c1b29424ea --- /dev/null +++ b/common/lib/init.coffee @@ -0,0 +1,28 @@ +path = require 'path' +timers = require 'timers' +Module = require 'module' + +# Add common/api/lib to module search paths. +globalPaths = Module.globalPaths +globalPaths.push path.join(process.resourcesPath, 'common', 'api', 'lib') + +# setImmediate and process.nextTick makes use of uv_check and uv_prepare to +# run the callbacks, however since we only run uv loop on requests, the +# callbacks wouldn't be called until something else activated the uv loop, +# which would delay the callbacks for arbitrary long time. So we should +# initiatively activate the uv loop once setImmediate and process.nextTick is +# called. +wrapWithActivateUvLoop = (func) -> + -> + process.activateUvLoop() + func.apply this, arguments +process.nextTick = wrapWithActivateUvLoop process.nextTick +global.setImmediate = wrapWithActivateUvLoop timers.setImmediate +global.clearImmediate = timers.clearImmediate + +# The child_process module also needs to activate the uv loop to make the ipc +# channel setup. +# TODO(zcbenz): Find out why this is needed. +childProcess = require 'child_process' +childProcess.spawn = wrapWithActivateUvLoop childProcess.spawn +childProcess.fork = wrapWithActivateUvLoop childProcess.fork diff --git a/renderer/lib/init.coffee b/renderer/lib/init.coffee index 84fb257c19b9..230529d39baa 100644 --- a/renderer/lib/init.coffee +++ b/renderer/lib/init.coffee @@ -1,5 +1,4 @@ path = require 'path' -timers = require 'timers' Module = require 'module' # Expose information of current process. @@ -14,36 +13,16 @@ process.argv.splice 1, 1 # of Atom's built-in libraries. globalPaths = Module.globalPaths globalPaths.push path.join(process.resourcesPath, 'renderer', 'api', 'lib') -# And also common/api/lib. -globalPaths.push path.join(process.resourcesPath, 'common', 'api', 'lib') # And also app. globalPaths.push path.join(process.resourcesPath, 'app') +# Import common settings. +require path.resolve(__dirname, '..', '..', 'common', 'lib', 'init.js') + # Expose global variables. global.require = require global.module = module -# setImmediate and process.nextTick makes use of uv_check and uv_prepare to -# run the callbacks, however since we only run uv loop on requests, the -# callbacks wouldn't be called until something else activated the uv loop, -# which would delay the callbacks for arbitrary long time. So we should -# initiatively activate the uv loop once setImmediate and process.nextTick is -# called. -wrapWithActivateUvLoop = (func) -> - -> - process.activateUvLoop() - func.apply this, arguments -process.nextTick = wrapWithActivateUvLoop process.nextTick -global.setImmediate = wrapWithActivateUvLoop timers.setImmediate -global.clearImmediate = timers.clearImmediate - -# The child_process module also needs to activate the uv loop to make the ipc -# channel setup. -# TODO(zcbenz): Find out why this is needed. -childProcess = require 'child_process' -childProcess.spawn = wrapWithActivateUvLoop childProcess.spawn -childProcess.fork = wrapWithActivateUvLoop childProcess.fork - # Set the __filename to the path of html file if it's file:// protocol. if window.location.protocol is 'file:' global.__filename =