nil events working

This commit is contained in:
Enrique García 2011-10-30 12:33:34 +01:00
parent 86daaea54f
commit 5b4b085156
4 changed files with 66 additions and 17 deletions

View File

@ -1,6 +1,6 @@
h1. beholder.lua h1. beholder.lua
A simple event observer for Lua. Can be used with "middleclass":https://github.com/kikito/middleclass A simple event observer for Lua.
h1. Example h1. Example

View File

@ -1,11 +1,9 @@
-- beholder.lua - v1.0 (2011-11) -- beholder.lua - v1.0 (2011-11)
-- requires middleclass 2.0
-- Copyright (c) 2011 Enrique García Cota -- Copyright (c) 2011 Enrique García Cota
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra
local beholder = {} local beholder = {}
@ -35,24 +33,36 @@ local function findOrCreateNode(self, event)
return current return current
end end
local function executeNodeActions(self, event) local function executeNodeActions(node, params)
local current = self._root
local counter = 0 local counter = 0
local params = copy(event) params = params or {}
local key for _,action in pairs(node.actions) do
action(unpack(params))
counter = counter + 1
end
return counter
end
for i=1,#event do local function executeAllActions(node)
key = event[i] local counter = executeNodeActions(node)
current = current.children[key] for _,child in pairs(node.children) do
if not current then break end counter = counter + executeAllActions(child)
end
return counter
end
local function executeEventActions(node, event)
local params = copy(event)
local counter = executeNodeActions(node, params)
for i=1, #event do
node = node.children[event[i]]
if not node then break end
table.remove(params, 1) table.remove(params, 1)
for _,action in pairs(current.actions) do counter = counter + executeNodeActions(node, params)
action(unpack(params))
counter = counter + 1
end
end end
return counter > 0 and counter return counter
end end
local function addActionToNode(self, node, action) local function addActionToNode(self, node, action)
@ -83,7 +93,9 @@ function beholder:stopObserving(id)
end end
function beholder:trigger(...) function beholder:trigger(...)
return executeNodeActions(self, {...}) local event = {...}
local counter = (#event == 0) and executeAllActions(self._root) or executeEventActions(self._root, event)
return counter > 0 and counter
end end
beholder:reset() beholder:reset()

View File

@ -76,5 +76,24 @@ describe("Acceptance", function()
assert_false(escapePressed) assert_false(escapePressed)
end) end)
test("nil events", function()
local counter = 0
local id = beholder:observe(function(_, x) counter = counter + x end)
beholder:trigger("FOO", 1)
beholder:trigger("BAR", 2)
assert_equal(3, counter)
beholder:stopObserving(id)
beholder:observe("BAZ", function() counter = counter + 1 end)
beholder:trigger()
assert_equal(4, counter)
end)
end) end)

View File

@ -29,6 +29,14 @@ describe("Unit", function()
beholder:trigger("KEYPRESS", "start") beholder:trigger("KEYPRESS", "start")
assert_equal(counter, 1) assert_equal(counter, 1)
end) end)
it("observes all events with the nil event", function()
local counter = 0
beholder:observe(function(_,x) counter = counter + x end)
beholder:trigger("FOO", 1)
beholder:trigger("BAR", 2)
assert_equal(3, counter)
end)
end) end)
describe(":stopObserving", function() describe(":stopObserving", function()
@ -104,6 +112,16 @@ describe("Unit", function()
assert_equal(2, beholder:trigger("X")) assert_equal(2, beholder:trigger("X"))
end) end)
it("triggers any observation with the nil event", function()
local counter = 0
beholder:observe("X", function() counter = counter + 1 end)
beholder:observe("Y", 1, function() counter = counter + 2 end)
beholder:trigger()
assert_equal(3, counter)
end)
end) end)