Order listeners by reverse registration order

This commit is contained in:
Willem Van Lint 2016-04-17 17:06:22 -07:00
parent 005022567b
commit 73748ef768
2 changed files with 14 additions and 4 deletions

View File

@ -74,6 +74,13 @@ describe "CommandRegistry", ->
grandchild.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual ['.foo.bar', '.bar', '.foo']
it "orders inline listeners by reverse registration order", ->
calls = []
registry.add child, 'command', -> calls.push('child1')
registry.add child, 'command', -> calls.push('child2')
child.dispatchEvent(new CustomEvent('command', bubbles: true))
expect(calls).toEqual ['child2', 'child1']
it "stops bubbling through ancestors when .stopPropagation() is called on the event", ->
calls = []

View File

@ -244,11 +244,14 @@ class CommandRegistry
(@selectorBasedListenersByCommandName[event.type] ? [])
.filter (listener) -> currentTarget.webkitMatchesSelector(listener.selector)
.sort (a, b) -> a.compare(b)
listeners = listeners.concat(selectorBasedListeners)
listeners = selectorBasedListeners.concat(listeners)
matched = true if listeners.length > 0
for listener in listeners
# Call inline listeners first in reverse registration order,
# and selector-based listeners by specificity and reverse
# registration order.
for listener in listeners by -1
break if immediatePropagationStopped
listener.callback.call(currentTarget, dispatchedEvent)
@ -271,8 +274,8 @@ class SelectorBasedListener
@sequenceNumber = SequenceCount++
compare: (other) ->
other.specificity - @specificity or
other.sequenceNumber - @sequenceNumber
@specificity - other.specificity or
@sequenceNumber - other.sequenceNumber
class InlineListener
constructor: (@callback) ->