Merge pull request #3721 from atom/emit-process-exit-event-with-code

Emit process exit event with app exit code
This commit is contained in:
Cheng Zhao 2015-12-10 11:39:14 +08:00
commit c180607ef6
8 changed files with 51 additions and 3 deletions

View file

@ -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();

View file

@ -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);

View file

@ -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.

View file

@ -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

View file

@ -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_

View file

@ -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 ->

12
spec/fixtures/api/quit-app/main.js vendored Normal file
View file

@ -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)
})

View file

@ -0,0 +1,4 @@
{
"name": "quit-app",
"main": "main.js"
}