added params to trigger

This commit is contained in:
Enrique García 2011-10-26 00:31:33 +02:00
parent a061a2b898
commit e9a3d1a113
3 changed files with 30 additions and 2 deletions

View File

@ -25,12 +25,13 @@ end
function beholder:stopObserving(id)
local event = self._ids[id]
self._actions[event][id] = nil
self._ids[id]=nil
end
function beholder:trigger(event)
function beholder:trigger(event,...)
local actions = self._actions[event] or {}
for _,action in pairs(actions) do
action()
action(...)
end
end

View File

@ -53,4 +53,19 @@ describe("Acceptance", function()
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)
end)

View File

@ -50,6 +50,18 @@ describe("Unit", function()
assert_equal(counter2, 2)
end)
it("passes parameters to the actions", 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)
end)