diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 697d6eca6aab..256ecff53e10 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -181,7 +181,8 @@ void App::OnWindowAllClosed() { } void App::OnQuit() { - Emit("quit"); + int exitCode = AtomBrowserMainParts::Get()->GetExitCode(); + Emit("quit", exitCode); if (process_singleton_.get()) { process_singleton_->Cleanup(); diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index fd72f5e4ae9a..eadd52ac44c8 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -61,6 +61,10 @@ bool AtomBrowserMainParts::SetExitCode(int code) { return true; } +int AtomBrowserMainParts::GetExitCode() { + return exit_code_ != nullptr ? *exit_code_ : 0; +} + base::Closure AtomBrowserMainParts::RegisterDestructionCallback( const base::Closure& callback) { auto iter = destructors_.insert(destructors_.end(), callback); diff --git a/atom/browser/atom_browser_main_parts.h b/atom/browser/atom_browser_main_parts.h index fbc59f7f811d..c1c0c89c6786 100644 --- a/atom/browser/atom_browser_main_parts.h +++ b/atom/browser/atom_browser_main_parts.h @@ -34,6 +34,9 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { // Sets the exit code, will fail if the the message loop is not ready. bool SetExitCode(int code); + // Gets the exit code + int GetExitCode(); + // Register a callback that should be destroyed before JavaScript environment // gets destroyed. // Returns a closure that can be used to remove |callback| from the list. diff --git a/atom/browser/lib/init.coffee b/atom/browser/lib/init.coffee index 85faf0f038ad..c3d56d9c7c82 100644 --- a/atom/browser/lib/init.coffee +++ b/atom/browser/lib/init.coffee @@ -53,8 +53,8 @@ process.on 'uncaughtException', (error) -> # Emit 'exit' event on quit. {app} = require 'electron' -app.on 'quit', -> - process.emit 'exit' +app.on 'quit', (event, exitCode) -> + process.emit 'exit', exitCode # Map process.exit to app.exit, which quits gracefully. process.exit = app.exit diff --git a/docs/api/app.md b/docs/api/app.md index 3faf4a1f0ac6..f21f37039705 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -64,6 +64,11 @@ the `will-quit` and `window-all-closed` events. ### Event: 'quit' +Returns: + +* `event` Event +* `exitCode` Integer + Emitted when the application is quitting. ### Event: 'open-file' _OS X_ diff --git a/spec/api-app-spec.coffee b/spec/api-app-spec.coffee index 490727488d99..adb64d363eb1 100644 --- a/spec/api-app-spec.coffee +++ b/spec/api-app-spec.coffee @@ -1,4 +1,6 @@ assert = require 'assert' +ChildProcess = require 'child_process' +path = require 'path' {remote} = require 'electron' {app, BrowserWindow} = remote.require 'electron' @@ -29,6 +31,23 @@ describe 'app module', -> it 'should not be empty', -> assert.notEqual app.getLocale(), '' + describe 'app.exit(exitCode)', -> + appProcess = null + afterEach -> + appProcess?.kill() + + it 'emits a process exit event with the code', (done) -> + appPath = path.join(__dirname, 'fixtures', 'api', 'quit-app') + electronPath = remote.getGlobal('process').execPath + appProcess = ChildProcess.spawn(electronPath, [appPath]) + + output = '' + appProcess.stdout.on 'data', (data) -> output += data + appProcess.on 'close', (code) -> + assert.notEqual output.indexOf('Exit event with code: 123'), -1 + assert.equal code, 123 + done() + describe 'BrowserWindow events', -> w = null afterEach -> diff --git a/spec/fixtures/api/quit-app/main.js b/spec/fixtures/api/quit-app/main.js new file mode 100644 index 000000000000..e2f97affe6de --- /dev/null +++ b/spec/fixtures/api/quit-app/main.js @@ -0,0 +1,12 @@ +var app = require('electron').app + +app.on('ready', function () { + // This setImmediate call gets the spec passing on Linux + setImmediate(function () { + app.exit(123) + }) +}) + +process.on('exit', function (code) { + console.log('Exit event with code: ' + code) +}) diff --git a/spec/fixtures/api/quit-app/package.json b/spec/fixtures/api/quit-app/package.json new file mode 100644 index 000000000000..ea5bb1643b9b --- /dev/null +++ b/spec/fixtures/api/quit-app/package.json @@ -0,0 +1,4 @@ +{ + "name": "quit-app", + "main": "main.js" +}