Skip to content

Commit

Permalink
Merge pull request atom#8053 from oggy/will-destroy-pane
Browse files Browse the repository at this point in the history
Fire event before a pane is destroyed.
  • Loading branch information
thomasjo committed Jul 25, 2015
2 parents 1b6cab0 + 45694dc commit 9efab86
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
21 changes: 21 additions & 0 deletions spec/pane-container-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ describe "PaneContainer", ->

expect(events).toEqual [{pane: pane2}, {pane: pane3}]

describe "::onWillDestroyPane(callback)", ->
it "invokes the given callback before panes or their items are destroyed", ->
class TestItem
constructor: -> @_isDestroyed = false
destroy: -> @_isDestroyed = true
isDestroyed: -> @_isDestroyed

container = new PaneContainer
events = []
container.onWillDestroyPane (event) ->
itemsDestroyed = (item.isDestroyed() for item in event.pane.getItems())
events.push([event, itemsDestroyed: itemsDestroyed])

pane1 = container.getActivePane()
pane2 = pane1.splitRight()
pane2.addItem(new TestItem)

pane2.destroy()

expect(events).toEqual [[{pane: pane2}, itemsDestroyed: [false]]]

describe "::onDidDestroyPane(callback)", ->
it "invokes the given callback when panes are destroyed", ->
container = new PaneContainer
Expand Down
7 changes: 7 additions & 0 deletions spec/pane-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ describe "Pane", ->
pane1.addItems([new Item("A"), new Item("B")])
pane2 = pane1.splitRight()

it "invokes ::onWillDestroy observers before destroying items", ->
itemsDestroyed = null
pane1.onWillDestroy ->
itemsDestroyed = (item.isDestroyed() for item in pane1.getItems())
pane1.destroy()
expect(itemsDestroyed).toEqual([false, false])

it "destroys the pane's destroyable items", ->
[item1, item2] = pane1.getItems()
pane1.destroy()
Expand Down
6 changes: 6 additions & 0 deletions src/pane-container.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class PaneContainer extends Model
onDidDestroyPane: (fn) ->
@emitter.on 'did-destroy-pane', fn

onWillDestroyPane: (fn) ->
@emitter.on 'will-destroy-pane', fn

onDidChangeActivePane: (fn) ->
@emitter.on 'did-change-active-pane', fn

Expand Down Expand Up @@ -178,6 +181,9 @@ class PaneContainer extends Model
didAddPane: (event) ->
@emitter.emit 'did-add-pane', event

willDestroyPane: (event) ->
@emitter.emit 'will-destroy-pane', event

didDestroyPane: (event) ->
@emitter.emit 'did-destroy-pane', event

Expand Down
10 changes: 10 additions & 0 deletions src/pane.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class Pane extends Model
onDidActivate: (callback) ->
@emitter.on 'did-activate', callback

# Public: Invoke the given callback before the pane is destroyed.
#
# * `callback` {Function} to be called before the pane is destroyed.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onWillDestroy: (callback) ->
@emitter.on 'will-destroy', callback

# Public: Invoke the given callback when the pane is destroyed.
#
# * `callback` {Function} to be called when the pane is destroyed.
Expand Down Expand Up @@ -573,6 +581,8 @@ class Pane extends Model
if @container?.isAlive() and @container.getPanes().length is 1
@destroyItems()
else
@emitter.emit 'will-destroy'
@container?.willDestroyPane(pane: this)
super

# Called by model superclass.
Expand Down
10 changes: 10 additions & 0 deletions src/workspace.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ class Workspace extends Model
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidAddPane: (callback) -> @paneContainer.onDidAddPane(callback)

# Extended: Invoke the given callback before a pane is destroyed in the
# workspace.
#
# * `callback` {Function} to be called before panes are destroyed.
# * `event` {Object} with the following keys:
# * `pane` The pane to be destroyed.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onWillDestroyPane: (callback) -> @paneContainer.onWillDestroyPane(callback)

# Extended: Invoke the given callback when a pane is destroyed in the
# workspace.
#
Expand Down

0 comments on commit 9efab86

Please sign in to comment.