Skip to content

Commit

Permalink
event emitter: copy list of callbacks before iterating. closes #51
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jan 31, 2024
1 parent 99ed590 commit 022fcf6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion colyseus/eventemitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ function EventEmitter:new(object)
end

function object:emit (event, ...)
for _, listener in ipairs(self:listeners(event)) do
-- copy list before iterating over it
-- (make sure all previously registered callbacks are called, even if some are removed in-between)
local listeners = {}
for i, listener in ipairs(self:listeners(event)) do
listeners[i] = listener
end

for i, listener in ipairs(listeners) do
if "function" == type(listener) then
listener(...)

Expand Down

0 comments on commit 022fcf6

Please sign in to comment.