From fe27ebec1bc206c776d2c5b77de1512e680f9fee Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 5 Sep 2014 09:12:42 -0600 Subject: [PATCH] Handle .stopImmediatePropagation() being called on command events --- spec/command-registry-spec.coffee | 10 ++++++++++ src/command-registry.coffee | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index a76767523..2b28c175a 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -67,3 +67,13 @@ describe "CommandRegistry", -> grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) expect(calls).toEqual ['child-1', 'child-2'] + + it "stops invoking callbacks when .stopImmediatePropagation() is called on the event", -> + calls = [] + + registry.add 'command', '.parent', -> calls.push('parent') + registry.add 'command', '.child', -> calls.push('child-2') + registry.add 'command', '.child', (event) -> calls.push('child-1'); event.stopImmediatePropagation() + + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual ['child-1'] diff --git a/src/command-registry.coffee b/src/command-registry.coffee index cb29c4a2f..650dd8450 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -17,12 +17,17 @@ class CommandRegistry dispatchCommand: (event) => propagationStopped = false + immediatePropagationStopped = false currentTarget = event.target syntheticEvent = Object.create event, eventPhase: value: Event.BUBBLING_PHASE currentTarget: get: -> currentTarget - stopPropagation: value: -> propagationStopped = true + stopPropagation: value: -> + propagationStopped = true + stopImmediatePropagation: value: -> + propagationStopped = true + immediatePropagationStopped = true loop matchingListeners = @@ -31,6 +36,7 @@ class CommandRegistry .sort (a, b) -> a.compare(b) for listener in matchingListeners + break if immediatePropagationStopped listener.callback.call(currentTarget, syntheticEvent) break if propagationStopped