diff --git a/README.textile b/README.textile index 22c57e0..da7514f 100644 --- a/README.textile +++ b/README.textile @@ -94,14 +94,25 @@ Similarly, you can add an action that will be triggered for any player detection
Beholder:observe('PLAYERDETECTION', function(player,x,y) print(player.no," detected at ",x,y)+h1. The nil event + If you want to detect all signals raised (i.e. for logging and debugging) you can do so by observing the "empty" event - simply pass a function to observe, without adding any more params:
Beholder:observe(function(...) log("Event triggered", ...) end)-Similarly, you could use this to trigger all observed events (again, this will be useful mostly for debugging): +If you want to trigger the events attached only to the nil event, you can do so by calling trigger without params:
Beholder:trigger()+h1. Triggering all callbacks + +You can use the @triggerAll@ method to trigger all observed events (this will be useful mostly for debugging): + +
Beholder:triggerAll(...)+ +Note that you can pass parameters to @triggerAll@. These will be passed to all callbacks (make sure that they are prepared for this, or you will get errors). + + h1. Installation If you are going to use it, make sure that you have downloaded and installed "middleclass":https://github.com/kikito/middleclass diff --git a/beholder.lua b/beholder.lua index 2507c4c..0412fd6 100644 --- a/beholder.lua +++ b/beholder.lua @@ -7,6 +7,10 @@ local beholder = {} +local function falseIfZero(n) + return n > 0 and n +end + local function copy(t) local c={} for i=1,#t do c[i]=t[i] end @@ -37,7 +41,6 @@ end local function executeNodeCallbacks(node, params) local counter = 0 - params = params or {} for _,callback in pairs(node.callbacks) do callback(unpack(params)) counter = counter + 1 @@ -45,10 +48,10 @@ local function executeNodeCallbacks(node, params) return counter end -local function executeAllCallbacks(node) - local counter = executeNodeCallbacks(node) +local function executeAllCallbacks(node, params) + local counter = executeNodeCallbacks(node, params) for _,child in pairs(node.children) do - counter = counter + executeAllCallbacks(child) + counter = counter + executeAllCallbacks(child, params) end return counter end @@ -95,9 +98,11 @@ function beholder:stopObserving(id) end function beholder:trigger(...) - local event = {...} - local counter = (#event == 0) and executeAllCallbacks(self._root) or executeEventCallbacks(self._root, event) - return counter > 0 and counter + return falseIfZero( executeEventCallbacks(self._root, {...}) ) +end + +function beholder:triggerAll(...) + return falseIfZero( executeAllCallbacks(self._root, {...}) ) end beholder:reset() diff --git a/spec/acceptance.lua b/spec/acceptance.lua index 37f6d3c..4630c0c 100644 --- a/spec/acceptance.lua +++ b/spec/acceptance.lua @@ -88,10 +88,29 @@ describe("Acceptance", function() beholder:stopObserving(id) - beholder:observe("BAZ", function() counter = counter + 1 end) + beholder:observe(function() counter = 10 end) + beholder:trigger() - assert_equal(4, counter) + assert_equal(10, counter) + end) + + test("triggering all events", function() + local even = 0 + local uneven = 1 + + beholder:observe("EVEN", function(x) even = even + 2*x end) + beholder:observe("UNEVEN", function(x) uneven = uneven + 2*x end) + + beholder:triggerAll(1) + + assert_equal(even, 2) + assert_equal(uneven, 3) + + beholder:triggerAll(2) + + assert_equal(even, 6) + assert_equal(uneven, 7) end) diff --git a/spec/unit.lua b/spec/unit.lua index 1102508..b4350bd 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -116,22 +116,40 @@ describe("Unit", function() assert_equal(2, beholder:trigger("X")) end) - it("triggers any observation with the nil event", function() + it("triggers callbacks within the nil event only", function() local counter = 0 - beholder:observe("X", function() counter = counter + 1 end) - beholder:observe("Y", 1, function() counter = counter + 2 end) + beholder:observe("X", function() counter = counter + 10 end) + beholder:observe(function() counter = counter + 5 end) beholder:trigger() - assert_equal(3, counter) + assert_equal(5, counter) + end) + end) + + describe(":triggerAll", function() + it("calls all registered callbacks", function() + local counter = 0 + beholder:observe("X", function() counter = counter + 1 end) + beholder:triggerAll() + assert_equal(1, counter) + end) + it("passes parameters to callbacks", function() + local counter = 0 + beholder:observe(function(x) counter = counter + x end) + beholder:triggerAll(2) + assert_equal(2, counter) + end) + it("returns false if no actions where found", function() + assert_false(beholder:triggerAll()) + end) + it("returns the number of actions executed", function() + beholder:observe("X", function() end) + beholder:observe("Y", function() end) + assert_equal(2, beholder:triggerAll()) end) end) - describe(":reset", function() - - end) - - end)