diff --git a/spec-nylas/stores/draft-store-spec.coffee b/spec-nylas/stores/draft-store-spec.coffee index 6832652b3..8c7159bf8 100644 --- a/spec-nylas/stores/draft-store-spec.coffee +++ b/spec-nylas/stores/draft-store-spec.coffee @@ -365,14 +365,14 @@ describe "DraftStore", -> }} it "should return false and call window.close itself", -> - spyOn(window, 'close') + spyOn(atom, 'close') expect(DraftStore._onBeforeUnload()).toBe(false) runs -> @resolve() waitsFor -> - window.close.callCount > 0 + atom.close.callCount > 0 runs -> - expect(window.close).toHaveBeenCalled() + expect(atom.close).toHaveBeenCalled() describe "when no drafts return unresolved commit promises", -> beforeEach -> diff --git a/src/atom.coffee b/src/atom.coffee index c11fbbc1f..f999a3da2 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -217,7 +217,8 @@ class Atom extends Model @subscribe @packages.onDidActivateInitialPackages => @watchThemes() @windowEventHandler = new WindowEventHandler - window.onbeforeunload = => @onBeforeUnload() + window.onbeforeunload = => @_unloading() + @_unloadCallbacks = [] # Start our error reporting to the backend and attach error handlers # to the window and the Bluebird Promise library, converting things @@ -847,6 +848,18 @@ class Atom extends Model ipc.send('call-window-method', 'setAutoHideMenuBar', autoHide) ipc.send('call-window-method', 'setMenuBarVisibility', !autoHide) - onBeforeUnload: -> - Actions = require './flux/actions' - Actions.unloading() + # Lets multiple components register callbacks. + # The callbacks are expected to return either true or false + onBeforeUnload: (callback) -> @_unloadCallbacks.push(callback) + + _unloading: -> + continueUnload = true + for callback in @_unloadCallbacks + returnValue = callback() + if returnValue is true + continue + else if returnValue is false + continueUnload = false + else + console.warn "You registered an `onBeforeUnload` callback that does not return either exactly `true` or `false`. It returned #{returnValue}", callback + return continueUnload diff --git a/src/flux/actions.coffee b/src/flux/actions.coffee index 269359b17..1f76d3e0b 100644 --- a/src/flux/actions.coffee +++ b/src/flux/actions.coffee @@ -441,7 +441,6 @@ class Actions @metadataError: ActionScopeWindow @metadataCreated: ActionScopeWindow @metadataDestroyed: ActionScopeWindow - @unloading: ActionScopeWindow # Read the actions we declared on the dummy Actions object above diff --git a/src/flux/stores/draft-store.coffee b/src/flux/stores/draft-store.coffee index dae95baf7..e5ee14e0a 100644 --- a/src/flux/stores/draft-store.coffee +++ b/src/flux/stores/draft-store.coffee @@ -58,7 +58,8 @@ class DraftStore @listenTo Actions.sendDraftError, @_onSendDraftError @listenTo Actions.sendDraftSuccess, @_onSendDraftSuccess - @listenTo Actions.unloading, @_onBeforeUnload + + atom.onBeforeUnload @_onBeforeUnload @_draftSessions = {} @_sendingState = {} @@ -163,7 +164,7 @@ class DraftStore if promises.length > 0 Promise.settle(promises).then => @_draftSessions = {} - window.close() + atom.close() # Stop and wait before closing return false