From ca2f8db0229a03c05da3a92e5dbcf16125ffb8de Mon Sep 17 00:00:00 2001 From: Ash Wilson Date: Fri, 19 Apr 2019 12:01:03 -0400 Subject: [PATCH] Inject an application creation function to AtomApplication::open This lets me call open() and create properly stubbed-out AtomApplication instances. --- src/main-process/atom-application.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main-process/atom-application.js b/src/main-process/atom-application.js index e81345adb..1ee188481 100644 --- a/src/main-process/atom-application.js +++ b/src/main-process/atom-application.js @@ -120,6 +120,11 @@ class AtomApplication extends EventEmitter { static open (options) { const socketSecret = getExistingSocketSecret(options.version) const socketPath = getSocketPath(socketSecret) + const createApplication = options.createApplication || (async () => { + const app = new AtomApplication(options) + await app.initialize(options) + return app + }) // FIXME: Sometimes when socketPath doesn't exist, net.connect would strangely // take a few seconds to trigger 'error' event, it could be a bug of node @@ -129,18 +134,20 @@ class AtomApplication extends EventEmitter { !socketPath || options.test || options.benchmark || options.benchmarkTest || (process.platform !== 'win32' && !fs.existsSync(socketPath)) ) { - new AtomApplication(options).initialize(options) - return + return createApplication(options) } - const client = net.connect({path: socketPath}, () => { - client.write(encryptOptions(options, socketSecret), () => { - client.end() - app.quit() + return new Promise(resolve => { + const client = net.connect({path: socketPath}, () => { + client.write(encryptOptions(options, socketSecret), () => { + client.end() + app.quit() + resolve(null) + }) }) - }) - client.on('error', () => new AtomApplication(options).initialize(options)) + client.on('error', () => resolve(createApplication(options))) + }) } exit (status) {