From 5b4b085156ec97d477ec7abd508c0bc6ed88c7bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa?= Date: Sun, 30 Oct 2011 12:33:34 +0100 Subject: [PATCH] nil events working --- README.textile | 2 +- beholder.lua | 44 ++++++++++++++++++++++++++++---------------- spec/acceptance.lua | 19 +++++++++++++++++++ spec/unit.lua | 18 ++++++++++++++++++ 4 files changed, 66 insertions(+), 17 deletions(-) diff --git a/README.textile b/README.textile index 4174f1f..22c57e0 100644 --- a/README.textile +++ b/README.textile @@ -1,6 +1,6 @@ h1. beholder.lua -A simple event observer for Lua. Can be used with "middleclass":https://github.com/kikito/middleclass +A simple event observer for Lua. h1. Example diff --git a/beholder.lua b/beholder.lua index 8e4dcb0..d2f3b8e 100644 --- a/beholder.lua +++ b/beholder.lua @@ -1,11 +1,9 @@ -- beholder.lua - v1.0 (2011-11) --- requires middleclass 2.0 -- Copyright (c) 2011 Enrique GarcĂ­a Cota -- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: -- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. --- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra local beholder = {} @@ -35,24 +33,36 @@ local function findOrCreateNode(self, event) return current end -local function executeNodeActions(self, event) - local current = self._root +local function executeNodeActions(node, params) local counter = 0 - local params = copy(event) - local key + params = params or {} + for _,action in pairs(node.actions) do + action(unpack(params)) + counter = counter + 1 + end + return counter +end - for i=1,#event do - key = event[i] - current = current.children[key] - if not current then break end +local function executeAllActions(node) + local counter = executeNodeActions(node) + for _,child in pairs(node.children) do + counter = counter + executeAllActions(child) + end + return counter +end + +local function executeEventActions(node, event) + local params = copy(event) + local counter = executeNodeActions(node, params) + + for i=1, #event do + node = node.children[event[i]] + if not node then break end table.remove(params, 1) - for _,action in pairs(current.actions) do - action(unpack(params)) - counter = counter + 1 - end + counter = counter + executeNodeActions(node, params) end - return counter > 0 and counter + return counter end local function addActionToNode(self, node, action) @@ -83,7 +93,9 @@ function beholder:stopObserving(id) end function beholder:trigger(...) - return executeNodeActions(self, {...}) + local event = {...} + local counter = (#event == 0) and executeAllActions(self._root) or executeEventActions(self._root, event) + return counter > 0 and counter end beholder:reset() diff --git a/spec/acceptance.lua b/spec/acceptance.lua index 6851725..37f6d3c 100644 --- a/spec/acceptance.lua +++ b/spec/acceptance.lua @@ -76,5 +76,24 @@ describe("Acceptance", function() assert_false(escapePressed) end) + test("nil events", function() + local counter = 0 + + local id = beholder:observe(function(_, x) counter = counter + x end) + + beholder:trigger("FOO", 1) + beholder:trigger("BAR", 2) + + assert_equal(3, counter) + + beholder:stopObserving(id) + + beholder:observe("BAZ", function() counter = counter + 1 end) + beholder:trigger() + + assert_equal(4, counter) + + end) + end) diff --git a/spec/unit.lua b/spec/unit.lua index c0001a5..d0d67c8 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -29,6 +29,14 @@ describe("Unit", function() beholder:trigger("KEYPRESS", "start") assert_equal(counter, 1) end) + + it("observes all events with the nil event", function() + local counter = 0 + beholder:observe(function(_,x) counter = counter + x end) + beholder:trigger("FOO", 1) + beholder:trigger("BAR", 2) + assert_equal(3, counter) + end) end) describe(":stopObserving", function() @@ -104,6 +112,16 @@ describe("Unit", function() assert_equal(2, beholder:trigger("X")) end) + it("triggers any observation with the nil event", function() + local counter = 0 + beholder:observe("X", function() counter = counter + 1 end) + beholder:observe("Y", 1, function() counter = counter + 2 end) + + beholder:trigger() + + assert_equal(3, counter) + end) + end)