eventable: Fix unhooking of handlers
This commit is contained in:
@@ -39,19 +39,23 @@ end)()
|
||||
|
||||
local function do_hook(event_map, event, handler)
|
||||
log("hook:", event, handler)
|
||||
if not handler then
|
||||
event_map[event] = nil
|
||||
else
|
||||
if handler then
|
||||
if not event_map[event] then
|
||||
event_map[event] = {}
|
||||
end
|
||||
local handlers = event_map[event]
|
||||
handlers[#handlers + 1] = handler
|
||||
else
|
||||
return event_map[event]
|
||||
end
|
||||
end
|
||||
|
||||
local function do_unhook(event_map, event, handler)
|
||||
log("unhook:", event, handler)
|
||||
if handler == nil then
|
||||
event_map[event] = nil
|
||||
return
|
||||
end
|
||||
local old_handlers = event_map[event]
|
||||
if old_handlers then
|
||||
local handlers = {}
|
||||
|
||||
@@ -78,6 +78,42 @@ describe("matrix.eventable.functions()", function ()
|
||||
assert.spy(h1).was_called(2)
|
||||
assert.spy(h2).was_called(1)
|
||||
end)
|
||||
|
||||
it("allows unhooking all handlers at once", function ()
|
||||
local fire, hook, unhook = assert(eventable.functions())
|
||||
|
||||
local h1 = spy.new(function () end)
|
||||
local h2 = spy.new(function () end)
|
||||
hook("foo", h1)
|
||||
hook("foo", h2)
|
||||
fire("foo")
|
||||
assert.spy(h1).was_called(1)
|
||||
assert.spy(h2).was_called(1)
|
||||
|
||||
unhook("foo")
|
||||
fire("foo")
|
||||
assert.spy(h1).was_called(1)
|
||||
assert.spy(h2).was_called(1)
|
||||
end)
|
||||
|
||||
it("allows retrieving the list of handlers", function ()
|
||||
local fire, hook = assert(eventable.functions())
|
||||
local h = function () end
|
||||
hook("foo", h)
|
||||
assert.same({ h }, hook("foo"))
|
||||
hook("foo", h)
|
||||
assert.same({ h, h }, hook("foo"))
|
||||
end)
|
||||
|
||||
it("unhooks multiple instances of the same handler", function ()
|
||||
local fire, hook, unhook = assert(eventable.functions())
|
||||
local h = function () end
|
||||
hook("foo", h)
|
||||
hook("foo", h)
|
||||
assert.same({ h, h }, hook("foo"))
|
||||
unhook("foo", h)
|
||||
assert.same({}, hook("foo"))
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("matrix.eventable.object()", function ()
|
||||
|
||||
Reference in New Issue
Block a user