unwalking the path to be able to take another

This commit is contained in:
Enrique García 2011-10-29 01:24:10 +02:00
parent aaafb8f28c
commit f318a4dcc8
3 changed files with 65 additions and 27 deletions

View File

@ -14,49 +14,43 @@ local function findNode(self, event)
return self._nodes[event]
end
local function findNodeById(self, id)
return self._ids[id]
end
local function createNode(self, event)
self._nodes[event] = {actions = {}}
self._nodes[event] = {actions={}}
return self._nodes[event]
end
local function findOrCreateNode(self, event)
return findNode(self, event) or createNode(self, event)
return findNode(self,event) or createNode(self,event)
end
local function registerActionInNode(self, node, action)
local function addActionToNode(self, node, action)
local id = {}
node.actions[id] = action
self._ids[id] = node
self._nodesById[id] = node
return id
end
local function unregisterActionFromNode(self, node, id)
node.actions[id] = nil
self._ids[id] = nil
local function executeNodeActions(node)
for _,action in pairs(node.actions) do action() end
end
function beholder:reset()
self._nodes = {}
self._ids = {}
self._nodesById = setmetatable({}, {__mode="k"})
end
function beholder:observe(event, action)
return registerActionInNode(self, findOrCreateNode(self, event), action)
return addActionToNode(self, findOrCreateNode(self, event), action)
end
function beholder:stopObserving(id)
unregisterActionFromNode(self, findNodeById(self, id), id)
local node = self._nodesById[id]
node.actions[id] = nil
end
function beholder:trigger(event,...)
local node = findNode(self, event) or {}
for _,action in pairs(node.actions) do
action(...)
end
function beholder:trigger(event)
local node = findNode(self, event)
if node then executeNodeActions(node) end
end
beholder:reset()

View File

@ -52,20 +52,41 @@ describe("Acceptance", function()
assert_equal(counter2, 3)
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()
local counter = 0
local lastKey = ""
local enterPressed = false
local escapePressed = false
beholder:observe("KEYPRESS", function() counter = counter + 1 end)
beholder:observe("KEYPRESS", function(key) lastKey = key end)
beholder:observe("KEYPRESS", "enter", function() enterPressed = true end)
beholder:trigger("KEYPRESS", "space")
assert_equal(counter, 1)
assert_equal(lastKey, "space")
assert_false(enterPressed)
assert_false(escapePressed)
beholder:trigger("KEYPRESS", "enter")
assert_equal(counter, 2)
assert_equal(lastKey, "enter")
assert_true(enterPressed)
assert_false(escapePressed)
end)
]]
end)

View File

@ -22,7 +22,29 @@ describe("Unit", function()
assert_equal(counter1, 1)
assert_equal(counter2, 1)
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)
describe(":stopObserving", function()
@ -35,7 +57,7 @@ describe("Unit", function()
assert_equal(counter, 1)
end)
it("when given an id, it stops observing it", function()
it("stops observing one id without disturbing the others", function()
local counter1, counter2 = 0,0
local id1 = beholder:observe("EVENT", function() counter1 = counter1 + 1 end)
beholder:observe("EVENT", function() counter2 = counter2 + 1 end)
@ -50,7 +72,7 @@ describe("Unit", function()
assert_equal(counter2, 2)
end)
--[[
it("passes parameters to the actions", function()
local counter = 0
@ -62,6 +84,7 @@ describe("Unit", function()
assert_equal(counter, 6)
end)
]]
end)