mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
unwalking the path to be able to take another
This commit is contained in:
parent
aaafb8f28c
commit
f318a4dcc8
28
beholder.lua
28
beholder.lua
@ -14,10 +14,6 @@ local function findNode(self, event)
|
|||||||
return self._nodes[event]
|
return self._nodes[event]
|
||||||
end
|
end
|
||||||
|
|
||||||
local function findNodeById(self, id)
|
|
||||||
return self._ids[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]
|
||||||
@ -27,36 +23,34 @@ local function findOrCreateNode(self, event)
|
|||||||
return findNode(self,event) or createNode(self,event)
|
return findNode(self,event) or createNode(self,event)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function registerActionInNode(self, node, action)
|
local function addActionToNode(self, node, action)
|
||||||
local id = {}
|
local id = {}
|
||||||
node.actions[id] = action
|
node.actions[id] = action
|
||||||
self._ids[id] = node
|
self._nodesById[id] = node
|
||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
|
||||||
local function unregisterActionFromNode(self, node, id)
|
local function executeNodeActions(node)
|
||||||
node.actions[id] = nil
|
for _,action in pairs(node.actions) do action() end
|
||||||
self._ids[id] = nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function beholder:reset()
|
function beholder:reset()
|
||||||
self._nodes = {}
|
self._nodes = {}
|
||||||
self._ids = {}
|
self._nodesById = setmetatable({}, {__mode="k"})
|
||||||
end
|
end
|
||||||
|
|
||||||
function beholder:observe(event, action)
|
function beholder:observe(event, action)
|
||||||
return registerActionInNode(self, findOrCreateNode(self, event), action)
|
return addActionToNode(self, findOrCreateNode(self, event), action)
|
||||||
end
|
end
|
||||||
|
|
||||||
function beholder:stopObserving(id)
|
function beholder:stopObserving(id)
|
||||||
unregisterActionFromNode(self, findNodeById(self, id), id)
|
local node = self._nodesById[id]
|
||||||
|
node.actions[id] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function beholder:trigger(event,...)
|
function beholder:trigger(event)
|
||||||
local node = findNode(self, event) or {}
|
local node = findNode(self, event)
|
||||||
for _,action in pairs(node.actions) do
|
if node then executeNodeActions(node) end
|
||||||
action(...)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
beholder:reset()
|
beholder:reset()
|
||||||
|
@ -52,20 +52,41 @@ describe("Acceptance", function()
|
|||||||
assert_equal(counter2, 3)
|
assert_equal(counter2, 3)
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
--[[
|
||||||
test("callback parameters", function()
|
test("callback parameters", function()
|
||||||
local counter = 0
|
local counter = 0
|
||||||
|
|
||||||
beholder:observe("EVENT", function(x) counter = counter + x end)
|
beholder:observe("EVENT", function(x) counter = counter + x end)
|
||||||
|
|
||||||
beholder:trigger("EVENT", 1)
|
beholder:trigger("EVENT", 1)
|
||||||
|
|
||||||
assert_equal(counter, 1)
|
assert_equal(counter, 1)
|
||||||
|
|
||||||
beholder:trigger("EVENT", 5)
|
beholder:trigger("EVENT", 5)
|
||||||
|
|
||||||
assert_equal(counter, 6)
|
assert_equal(counter, 6)
|
||||||
|
|
||||||
end)
|
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)
|
end)
|
||||||
|
@ -22,7 +22,29 @@ 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()
|
||||||
@ -35,7 +57,7 @@ describe("Unit", function()
|
|||||||
assert_equal(counter, 1)
|
assert_equal(counter, 1)
|
||||||
end)
|
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 counter1, counter2 = 0,0
|
||||||
local id1 = beholder:observe("EVENT", function() counter1 = counter1 + 1 end)
|
local id1 = beholder:observe("EVENT", function() counter1 = counter1 + 1 end)
|
||||||
beholder:observe("EVENT", function() counter2 = counter2 + 1 end)
|
beholder:observe("EVENT", function() counter2 = counter2 + 1 end)
|
||||||
@ -50,7 +72,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
|
||||||
|
|
||||||
@ -62,6 +84,7 @@ describe("Unit", function()
|
|||||||
|
|
||||||
assert_equal(counter, 6)
|
assert_equal(counter, 6)
|
||||||
end)
|
end)
|
||||||
|
]]
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user