From 04392c562e07c3362e4b2133acfd441665edcec7 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Kevin Sawicki Date: Thu, 30 May 2013 11:38:08 -0700 Subject: [PATCH] Create AtomApplication after `app` is finished launching --- src/atom-application.coffee | 50 ++++++++++++++----------------------- src/main.coffee | 14 +++++++++-- 2 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/atom-application.coffee b/src/atom-application.coffee index e16e3c102..fb383e2d2 100644 --- a/src/atom-application.coffee +++ b/src/atom-application.coffee @@ -16,7 +16,6 @@ class AtomApplication resourcePath: null pathsToOpen: null version: null - launched: false socketPath: '/tmp/atom.sock' constructor: ({@resourcePath, @pathsToOpen, @version, test, pidToKillWhenClosed}) -> @@ -24,33 +23,21 @@ class AtomApplication @pathsToOpen ?= [] @windows = [] - app.on 'open-file', (event, filePath) => - event.preventDefault() - if @launched - @openPath filePath + @sendArgumentsToExistingProcess pidToKillWhenClosed, (success) => + app.terminate() if success # An Atom already exists, kill this process + @listenForArgumentsFromNewProcess() + @setupNodePath() + @setupJavaScriptArguments() + @buildApplicationMenu() + @handleEvents() + + if test + @runSpecs(true) + else if @pathsToOpen.length > 0 + @openPaths(@pathsToOpen, pidToKillWhenClosed) else - # Delay opening until Atom has finished launching, this condition - # happens when user double clicks a file in Finder to open it. - @pathsToOpen.push filePath - - app.on 'finish-launching', => - @launched = true - - @sendArgumentsToExistingProcess pidToKillWhenClosed, (success) => - app.terminate() if success # An Atom already exists, kill this process - @listenForArgumentsFromNewProcess() - @setupNodePath() - @setupJavaScriptArguments() - @buildApplicationMenu() - @handleEvents() - - if test - @runSpecs(true) - else if @pathsToOpen.length > 0 - @openPaths(@pathsToOpen, pidToKillWhenClosed) - else - # Always open a editor window if this is the first instance of Atom. - @openPath(null) + # Always open a editor window if this is the first instance of Atom. + @openPath(null) removeWindow: (window) -> @windows.splice @windows.indexOf(window), 1 @@ -82,10 +69,6 @@ class AtomApplication process.env['NODE_PATH'] = resourcePaths.join path.delimiter sendArgumentsToExistingProcess: (pidToKillWhenClosed, callback) -> - if not fs.existsSync(@socketPath) - callback(false) - return - client = net.connect {path: @socketPath}, (args...) => client.write(JSON.stringify({@pathsToOpen, pidToKillWhenClosed})) callback(true) @@ -163,6 +146,11 @@ class AtomApplication app.on 'will-quit', => fs.unlinkSync @socketPath if fs.existsSync(@socketPath) + app.on 'open-file', (event, filePath) => + event.preventDefault() + @openPath filePath + + ipc.on 'close-without-confirm', (processId, routingId) -> window = BrowserWindow.fromProcessIdAndRoutingId processId, routingId window.removeAllListeners 'close' diff --git a/src/main.coffee b/src/main.coffee index 6ad76226d..70fb5d03d 100644 --- a/src/main.coffee +++ b/src/main.coffee @@ -1,6 +1,7 @@ +delegate = require 'atom-delegate' +app = require 'app' fs = require 'fs' path = require 'path' -delegate = require 'atom-delegate' optimist = require 'optimist' nslog = require 'nslog' AtomApplication = require './atom-application' @@ -12,7 +13,16 @@ require 'coffee-script' delegate.browserMainParts.preMainMessageLoopRun = -> commandLineArgs = parseCommandLine() - global.atomApplication = new AtomApplication(commandLineArgs) + + addPathToOpen = (event, filePath) -> + event.preventDefault() + commandLineArgs.pathsToOpen ?= [] + commandLineArgs.pathsToOpen.push(filePath) + + app.on 'open-file', addPathToOpen + app.on 'finish-launching', -> + app.removeListener 'open-file', addPathToOpen + global.atomApplication = new AtomApplication(commandLineArgs) getHomeDir = -> process.env[if process.platform is 'win32' then 'USERPROFILE' else 'HOME']