mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
changed behaviour of trigger(). Added triggerAll().
This commit is contained in:
parent
2daff1538b
commit
1565149a8b
@ -94,14 +94,25 @@ Similarly, you can add an action that will be triggered for any player detection
|
||||
|
||||
<pre>Beholder:observe('PLAYERDETECTION', function(player,x,y) print(player.no," detected at ",x,y)</pre>
|
||||
|
||||
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:
|
||||
|
||||
<pre>Beholder:observe(function(...) log("Event triggered", ...) end)</pre>
|
||||
|
||||
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:
|
||||
|
||||
<pre>Beholder:trigger()</pre>
|
||||
|
||||
h1. Triggering all callbacks
|
||||
|
||||
You can use the @triggerAll@ method to trigger all observed events (this will be useful mostly for debugging):
|
||||
|
||||
<pre>Beholder:triggerAll(...)</pre>
|
||||
|
||||
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
|
||||
|
19
beholder.lua
19
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()
|
||||
|
@ -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)
|
||||
|
||||
|
@ -116,20 +116,38 @@ 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)
|
||||
|
||||
|
||||
describe(":reset", function()
|
||||
|
||||
end)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user