From 5c73e5a00181144ec5eefbb822d71b4ce5f938bc Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Mon, 24 Aug 2015 14:10:02 -0700 Subject: [PATCH] Add CommandRegistry::onDidDispatch --- spec/command-registry-spec.coffee | 22 ++++++++++++++++++++++ src/command-registry.coffee | 13 +++++++++++++ 2 files changed, 35 insertions(+) diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index 0a8cafa6b..92e322478 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -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 = '<>' diff --git a/src/command-registry.coffee b/src/command-registry.coffee index 870093e2f..e41a637ef 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -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) ->