Merge pull request #8496 from atom/mb-command-registry-on-did-dispatch

Add CommandRegistry::onDidDispatch
This commit is contained in:
Max Brunsfeld
2015-08-24 14:28:30 -07:00
2 changed files with 35 additions and 0 deletions

View File

@@ -148,6 +148,28 @@ describe "CommandRegistry", ->
grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true))
expect(calls).toEqual []
it "invokes callbacks registered with ::onWillDispatch and ::onDidDispatch", ->
sequence = []
registry.onDidDispatch (event) ->
sequence.push ['onDidDispatch', event]
registry.add '.grandchild', 'command', (event) ->
sequence.push ['listener', event]
registry.onWillDispatch (event) ->
sequence.push ['onWillDispatch', event]
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(sequence[0][0]).toBe 'onWillDispatch'
expect(sequence[1][0]).toBe 'listener'
expect(sequence[2][0]).toBe 'onDidDispatch'
expect(sequence[0][1] is sequence[1][1] is sequence[2][1]).toBe true
expect(sequence[0][1].constructor).toBe CustomEvent
expect(sequence[0][1].target).toBe grandchild
describe "::add(selector, commandName, callback)", ->
it "throws an error when called with an invalid selector", ->
badSelector = '<>'

View File

@@ -182,9 +182,20 @@ class CommandRegistry
stopImmediatePropagation: value: ->
@handleCommandEvent(eventWithTarget)
# Public: Invoke the given callback before dispatching a command event.
#
# * `callback` {Function} to be called before dispatching each command
# * `event` The Event that will be dispatched
onWillDispatch: (callback) ->
@emitter.on 'will-dispatch', callback
# Public: Invoke the given callback after dispatching a command event.
#
# * `callback` {Function} to be called after dispatching each command
# * `event` The Event that was dispatched
onDidDispatch: (callback) ->
@emitter.on 'did-dispatch', callback
getSnapshot: ->
snapshot = {}
for commandName, listeners of @selectorBasedListenersByCommandName
@@ -239,6 +250,8 @@ class CommandRegistry
break if propagationStopped
currentTarget = currentTarget.parentNode ? window
@emitter.emit 'did-dispatch', syntheticEvent
matched
commandRegistered: (commandName) ->