mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
approaching previous status - trying to capture composed events
This commit is contained in:
parent
f318a4dcc8
commit
2aa41a28c5
21
beholder.lua
21
beholder.lua
@ -14,6 +14,10 @@ local function findNode(self, event)
|
||||
return self._nodes[event]
|
||||
end
|
||||
|
||||
local function findNodeById(self, id)
|
||||
return self._nodesById[id]
|
||||
end
|
||||
|
||||
local function createNode(self, event)
|
||||
self._nodes[event] = {actions={}}
|
||||
return self._nodes[event]
|
||||
@ -30,8 +34,14 @@ local function addActionToNode(self, node, action)
|
||||
return id
|
||||
end
|
||||
|
||||
local function executeNodeActions(node)
|
||||
for _,action in pairs(node.actions) do action() end
|
||||
local function removeActionFromNode(node, id)
|
||||
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
|
||||
|
||||
function beholder:reset()
|
||||
@ -44,13 +54,12 @@ function beholder:observe(event, action)
|
||||
end
|
||||
|
||||
function beholder:stopObserving(id)
|
||||
local node = self._nodesById[id]
|
||||
node.actions[id] = nil
|
||||
return removeActionFromNode(findNodeById(self, id), id)
|
||||
end
|
||||
|
||||
function beholder:trigger(event)
|
||||
function beholder:trigger(event, ...)
|
||||
local node = findNode(self, event)
|
||||
if node then executeNodeActions(node) end
|
||||
if node then executeNodeActions(node, ...) end
|
||||
end
|
||||
|
||||
beholder:reset()
|
||||
|
@ -52,17 +52,6 @@ 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
|
||||
@ -87,6 +76,5 @@ describe("Acceptance", function()
|
||||
assert_false(escapePressed)
|
||||
end)
|
||||
|
||||
]]
|
||||
|
||||
end)
|
||||
|
@ -22,29 +22,6 @@ 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()
|
||||
@ -72,7 +49,7 @@ describe("Unit", function()
|
||||
assert_equal(counter2, 2)
|
||||
|
||||
end)
|
||||
--[[
|
||||
|
||||
it("passes parameters to the actions", function()
|
||||
local counter = 0
|
||||
|
||||
@ -84,7 +61,20 @@ describe("Unit", function()
|
||||
|
||||
assert_equal(counter, 6)
|
||||
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)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user