-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, handlerStack was designed only for removal of the handler right at the top of the stack. However, some handlers sought to remove themselves when they were not at the top of the stack, creating confusion. The new handlerStack ensures that such removal can always be done safely.
- Loading branch information
Showing
11 changed files
with
119 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -3,3 +3,4 @@ | ||
*.swp | *.swp | ||
*.crx | *.crx | ||
*.js | *.js | ||
node_modules/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
root = exports ? window | |||
|
|||
class root.HandlerStack | |||
|
|||
constructor: -> | |||
@stack = [] | |||
@counter = 0 | |||
|
|||
genId: -> @counter = ++@counter & 0xffff | |||
|
|||
# Adds a handler to the stack. Returns a unique ID for that handler that can be used to remove it later. | |||
push: (handler) -> | |||
handler.id = @genId() | |||
@stack.push handler | |||
handler.id | |||
|
|||
# Called whenever we receive a key event. Each individual handler has the option to stop the event's | |||
# propagation by returning a falsy value. | |||
bubbleEvent: (type, event) -> | |||
for i in [(@stack.length - 1)..0] by -1 | |||
handler = @stack[i] | |||
# We need to check for existence of handler because the last function call may have caused the release | |||
# of more than one handler. | |||
if handler && handler[type] | |||
@currentId = handler.id | |||
passThrough = handler[type].call(@, event) | |||
if not passThrough | |||
DomUtils.suppressEvent(event) | |||
return false | |||
true | |||
|
|||
remove: (id = @currentId) -> | |||
for i in [(@stack.length - 1)..0] by -1 | |||
handler = @stack[i] | |||
if handler.id == id | |||
@stack.splice(i, 1) | |||
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,52 @@ | |||
require "./test_helper.js" | |||
extend(global, require "../../lib/handler_stack.js") | |||
|
|||
context "handlerStack", | |||
setup -> | |||
stub global, "DomUtils", {} | |||
stub DomUtils, "suppressEvent", -> | |||
@handlerStack = new HandlerStack | |||
@handler1Called = false | |||
@handler2Called = false | |||
|
|||
should "bubble events", -> | |||
@handlerStack.push { keydown: => @handler1Called = true } | |||
@handlerStack.push { keydown: => @handler2Called = true } | |||
@handlerStack.bubbleEvent 'keydown', {} | |||
assert.isTrue @handler2Called | |||
assert.isTrue @handler1Called | |||
|
|||
should "terminate bubbling on falsy return value", -> | |||
@handlerStack.push { keydown: => @handler1Called = true } | |||
@handlerStack.push { keydown: => @handler2Called = true; false } | |||
@handlerStack.bubbleEvent 'keydown', {} | |||
assert.isTrue @handler2Called | |||
assert.isFalse @handler1Called | |||
|
|||
should "remove handlers correctly", -> | |||
@handlerStack.push { keydown: => @handler1Called = true } | |||
handlerId = @handlerStack.push { keydown: => @handler2Called = true } | |||
@handlerStack.remove handlerId | |||
@handlerStack.bubbleEvent 'keydown', {} | |||
assert.isFalse @handler2Called | |||
assert.isTrue @handler1Called | |||
|
|||
should "remove handlers correctly", -> | |||
handlerId = @handlerStack.push { keydown: => @handler1Called = true } | |||
@handlerStack.push { keydown: => @handler2Called = true } | |||
@handlerStack.remove handlerId | |||
@handlerStack.bubbleEvent 'keydown', {} | |||
assert.isTrue @handler2Called | |||
assert.isFalse @handler1Called | |||
|
|||
should "handle self-removing handlers correctly", -> | |||
ctx = @ | |||
@handlerStack.push { keydown: => @handler1Called = true } | |||
@handlerStack.push { keydown: -> | |||
ctx.handler2Called = true | |||
@remove() | |||
} | |||
@handlerStack.bubbleEvent 'keydown', {} | |||
assert.isTrue @handler2Called | |||
assert.isTrue @handler1Called | |||
assert.equal @handlerStack.stack.length, 1 |
ea73be1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice tests.