approaching previous status - trying to capture composed events

This commit is contained in:
Enrique García 2011-10-29 01:39:19 +02:00
parent f318a4dcc8
commit 2aa41a28c5
3 changed files with 30 additions and 43 deletions

View File

@ -14,6 +14,10 @@ local function findNode(self, event)
return self._nodes[event] return self._nodes[event]
end end
local function findNodeById(self, id)
return self._nodesById[id]
end
local function createNode(self, event) local function createNode(self, event)
self._nodes[event] = {actions={}} self._nodes[event] = {actions={}}
return self._nodes[event] return self._nodes[event]
@ -30,8 +34,14 @@ local function addActionToNode(self, node, action)
return id return id
end end
local function executeNodeActions(node) local function removeActionFromNode(node, id)
for _,action in pairs(node.actions) do action() end if not node then return false end
node.actions[id] = nil
return true
end
local function executeNodeActions(node, ...)
for _,action in pairs(node.actions) do action(...) end
end end
function beholder:reset() function beholder:reset()
@ -44,13 +54,12 @@ function beholder:observe(event, action)
end end
function beholder:stopObserving(id) function beholder:stopObserving(id)
local node = self._nodesById[id] return removeActionFromNode(findNodeById(self, id), id)
node.actions[id] = nil
end end
function beholder:trigger(event) function beholder:trigger(event, ...)
local node = findNode(self, event) local node = findNode(self, event)
if node then executeNodeActions(node) end if node then executeNodeActions(node, ...) end
end end
beholder:reset() beholder:reset()

View File

@ -52,17 +52,6 @@ describe("Acceptance", function()
assert_equal(counter2, 3) assert_equal(counter2, 3)
end) end)
--[[
test("callback parameters", function()
local counter = 0
beholder:observe("EVENT", function(x) counter = counter + x end)
beholder:trigger("EVENT", 1)
assert_equal(counter, 1)
beholder:trigger("EVENT", 5)
assert_equal(counter, 6)
end)
test("composed events", function() test("composed events", function()
local counter = 0 local counter = 0
@ -87,6 +76,5 @@ describe("Acceptance", function()
assert_false(escapePressed) assert_false(escapePressed)
end) end)
]]
end) end)

View File

@ -22,29 +22,6 @@ describe("Unit", function()
assert_equal(counter1, 1) assert_equal(counter1, 1)
assert_equal(counter2, 1) assert_equal(counter2, 1)
end) end)
--[[
describe("when observing a table", function()
local counter
before(function()
counter = 0
beholder:observe({"KEYPRESS", "enter"}, function() counter = counter + 1 end)
end)
it("matches a trigger of a structurally identical table", function()
beholder:trigger({"KEYPRESS", "enter"})
assert_equal(counter, 1)
end)
it("matches a trigger of a structurally identical params list", function()
beholder:trigger("KEYPRESS", "enter")
assert_equal(counter, 1)
end)
--it("triggering partials does not ", function()
--end)
end)
]]
end) end)
describe(":stopObserving", function() describe(":stopObserving", function()
@ -72,7 +49,7 @@ describe("Unit", function()
assert_equal(counter2, 2) assert_equal(counter2, 2)
end) end)
--[[
it("passes parameters to the actions", function() it("passes parameters to the actions", function()
local counter = 0 local counter = 0
@ -84,7 +61,20 @@ describe("Unit", function()
assert_equal(counter, 6) assert_equal(counter, 6)
end) end)
]]
it("does not raise an error when stopping observing an inexisting event", function()
assert_not_error(function() beholder:stopObserving({}) end)
end)
it("returns false when no action was found for an id", function()
assert_equal(false, beholder:stopObserving({}))
end)
it("returns true when an action was found and removed", function()
local id = beholder:observe("X", function() end)
assert_true(beholder:stopObserving(id))
end)
end) end)