diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 27a0dc099..a2381b29d 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -3229,6 +3229,13 @@ describe "Editor", -> editor.destroy() expect(buffer.getMarkerCount()).toBe 0 + it "notifies ::onDidDestroy observers when the editor is destroyed", -> + destroyObserverCalled = false + editor.onDidDestroy -> destroyObserverCalled = true + + editor.destroy() + expect(destroyObserverCalled).toBe true + describe ".joinLines()", -> describe "when no text is selected", -> describe "when the line below isn't empty", -> diff --git a/src/atom.coffee b/src/atom.coffee index 3a84070f9..04361bf36 100644 --- a/src/atom.coffee +++ b/src/atom.coffee @@ -8,6 +8,7 @@ shell = require 'shell' _ = require 'underscore-plus' {deprecate} = require 'grim' +{Emitter} = require 'event-kit' {Model} = require 'theorist' fs = require 'fs-plus' @@ -147,6 +148,7 @@ class Atom extends Model # Call .loadOrCreate instead constructor: (@state) -> + @emitter = new Emitter {@mode} = @state DeserializerManager = require './deserializer-manager' @deserializers = new DeserializerManager() @@ -211,6 +213,18 @@ class Atom extends Model @windowEventHandler = new WindowEventHandler + ### + Section: Event Subscription + ### + + # Extended: Invoke the given callback whenever {::beep} is called. + # + # * `callback` {Function} to be called whenever {::beep} is called. + # + # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onDidBeep: (callback) -> + @emitter.on 'did-beep', callback + ### Section: Atom Metadata ### @@ -493,6 +507,7 @@ class Atom extends Model beep: -> shell.beep() if @config.get('core.audioBeep') @workspaceView.trigger 'beep' + @emitter.emit 'did-beep' # Essential: A flexible way to open a dialog akin to an alert dialog. # diff --git a/src/editor.coffee b/src/editor.coffee index 3e0b5bd37..f63dfc0b5 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -171,6 +171,7 @@ class Editor extends Model @buffer.release() @displayBuffer.destroy() @languageMode.destroy() + @emitter.emit 'did-destroy' ### Section: Event Subscription @@ -304,6 +305,14 @@ class Editor extends Model onDidSave: (callback) -> @getBuffer().onDidSave(callback) + # Public: Invoke the given callback when the editor is destroyed. + # + # * `callback` {Function} to be called when the editor is destroyed. + # + # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. + onDidDestroy: (callback) -> + @emitter.on 'did-destroy', callback + # Extended: Calls your `callback` when a {Cursor} is added to the editor. # Immediately calls your callback for each existing cursor. # diff --git a/src/pane-view.coffee b/src/pane-view.coffee index 21f4c8cfa..ddeab69af 100644 --- a/src/pane-view.coffee +++ b/src/pane-view.coffee @@ -162,7 +162,7 @@ class PaneView extends View deprecate 'Please return a Disposable object from your ::onDidChangeTitle method!' unless disposable?.dispose? @activeItemDisposables.add(disposable) if disposable?.dispose? else if item.on? - deprecate '::on methods for items are no longer supported. If you would like your item to title change behavior, please implement a ::onDidChangeTitle() method.' + deprecate '::on methods for items are no longer supported. If you would like your item to support title change behavior, please implement a ::onDidChangeTitle() method.' disposable = item.on('title-changed', @activeItemTitleChanged) @activeItemDisposables.add(disposable) if disposable?.dispose? diff --git a/src/pane.coffee b/src/pane.coffee index c6351f656..658cee6dd 100644 --- a/src/pane.coffee +++ b/src/pane.coffee @@ -518,6 +518,7 @@ class Pane extends Model destroyed: -> @container.activateNextPane() if @isActive() @emitter.emit 'did-destroy' + @emitter.dispose() item.destroy?() for item in @items.slice() ### diff --git a/src/workspace-view.coffee b/src/workspace-view.coffee index a46b46434..547d8daa9 100644 --- a/src/workspace-view.coffee +++ b/src/workspace-view.coffee @@ -167,6 +167,8 @@ class WorkspaceView extends View @command 'core:save', => @saveActivePaneItem() @command 'core:save-as', => @saveActivePaneItemAs() + @deprecatedViewEvents() + ### Section: Accessing the Workspace Model ### @@ -411,6 +413,100 @@ class WorkspaceView extends View Section: Deprecated ### + deprecatedViewEvents: -> + originalWorkspaceViewOn = @on + + @on = (eventName) => + switch eventName + when 'beep' + deprecate('Use Atom::onDidBeep instead') + when 'cursor:moved' + deprecate('Use Editor::onDidChangeCursorPosition instead') + when 'editor:attached' + deprecate('Use Editor::onDidAddTextEditor instead') + when 'editor:detached' + deprecate('Use Editor::onDidDestroy instead') + when 'editor:will-be-removed' + deprecate('Use Editor::onDidDestroy instead') + when 'pane:active-item-changed' + deprecate('Use Pane::onDidChangeActiveItem instead') + when 'pane:active-item-modified-status-changed' + deprecate('Use Pane::onDidChangeActiveItem and call onDidChangeModified on the active item instead') + when 'pane:active-item-title-changed' + deprecate('Use Pane::onDidChangeActiveItem and call onDidChangeTitle on the active item instead') + when 'pane:attached' + deprecate('Use Workspace::onDidAddPane instead') + when 'pane:became-active' + deprecate('Use Pane::onDidActivate instead') + when 'pane:became-inactive' + deprecate('Use Pane::onDidChangeActive instead') + when 'pane:item-added' + deprecate('Use Pane::onDidAddItem instead') + when 'pane:item-moved' + deprecate('Use Pane::onDidMoveItem instead') + when 'pane:item-removed' + deprecate('Use Pane::onDidRemoveItem instead') + when 'pane:removed' + deprecate('Use Pane::onDidDestroy instead') + when 'pane-container:active-pane-item-changed' + deprecate('Use Workspace::onDidChangeActivePaneItem instead') + when 'selection:changed' + deprecate('Use Editor::onDidChangeSelectionRange instead') + when 'uri-opened' + deprecate('Use Workspace::onDidOpen instead') + originalWorkspaceViewOn.apply(this, arguments) + + EditorView = require './editor-view' + originalEditorViewOn = EditorView::on + EditorView::on = (eventName) -> + switch eventName + when 'cursor:moved' + deprecate('Use Editor::onDidChangeCursorPosition instead') + when 'editor:attached' + deprecate('Use Editor::onDidAddTextEditor instead') + when 'editor:detached' + deprecate('Use Editor::onDidDestroy instead') + when 'editor:will-be-removed' + deprecate('Use Editor::onDidDestroy instead') + when 'selection:changed' + deprecate('Use Editor::onDidChangeSelectionRange instead') + originalEditorViewOn.apply(this, arguments) + + originalPaneViewOn = PaneView::on + PaneView::on = (eventName) -> + switch eventName + when 'cursor:moved' + deprecate('Use Editor::onDidChangeCursorPosition instead') + when 'editor:attached' + deprecate('Use Editor::onDidAddTextEditor instead') + when 'editor:detached' + deprecate('Use Editor::onDidDestroy instead') + when 'editor:will-be-removed' + deprecate('Use Editor::onDidDestroy instead') + when 'pane:active-item-changed' + deprecate('Use Pane::onDidChangeActiveItem instead') + when 'pane:active-item-modified-status-changed' + deprecate('Use Pane::onDidChangeActiveItem and call onDidChangeModified on the active item instead') + when 'pane:active-item-title-changed' + deprecate('Use Pane::onDidChangeActiveItem and call onDidChangeTitle on the active item instead') + when 'pane:attached' + deprecate('Use Workspace::onDidAddPane instead') + when 'pane:became-active' + deprecate('Use Pane::onDidActivate instead') + when 'pane:became-inactive' + deprecate('Use Pane::onDidChangeActive instead') + when 'pane:item-added' + deprecate('Use Pane::onDidAddItem instead') + when 'pane:item-moved' + deprecate('Use Pane::onDidMoveItem instead') + when 'pane:item-removed' + deprecate('Use Pane::onDidRemoveItem instead') + when 'pane:removed' + deprecate('Use Pane::onDidDestroy instead') + when 'selection:changed' + deprecate('Use Editor::onDidChangeSelectionRange instead') + originalPaneViewOn.apply(this, arguments) + # Deprecated eachPane: (callback) -> deprecate("Use WorkspaceView::eachPaneView instead") diff --git a/src/workspace.coffee b/src/workspace.coffee index 31b7cb261..db97e6732 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -104,7 +104,7 @@ class Workspace extends Model @onDidAddTextEditor ({textEditor}) -> callback(textEditor) # Essential: Invoke the given callback whenever an item is opened. Unlike - # ::onDidAddPaneItem, observers will be notified for items that are already + # {::onDidAddPaneItem}, observers will be notified for items that are already # present in the workspace when they are reopened. # # * `callback` {Function} to be called whenever an item is opened.