From f318a4dcc88f3a4c2a37e6cadcfeefb878a0613a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Sat, 29 Oct 2011 01:24:10 +0200 Subject: [PATCH] unwalking the path to be able to take another --- beholder.lua | 32 +++++++++++++------------------- spec/acceptance.lua | 33 +++++++++++++++++++++++++++------ spec/unit.lua | 27 +++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 27 deletions(-) diff --git a/beholder.lua b/beholder.lua index a8aa947..4dd717c 100644 --- a/beholder.lua +++ b/beholder.lua @@ -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() diff --git a/spec/acceptance.lua b/spec/acceptance.lua index 3d8a8ea..5750c2c 100644 --- a/spec/acceptance.lua +++ b/spec/acceptance.lua @@ -27,7 +27,7 @@ describe("Acceptance", function() end) test("several actions on the same event", function() - + local counter1, counter2 = 0,0 local id1 = beholder:observe("EVENT", function() counter1 = counter1 + 1 end) @@ -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) diff --git a/spec/unit.lua b/spec/unit.lua index e550b69..cfce5db 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -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)