Skip to content

Commit

Permalink
Simplifies internal Emitter cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
RiskoZoSlovenska committed Jan 10, 2022
1 parent 14d58e3 commit 4736f7e
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions libs/utils/Emitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ local meta = {
end
}

local function mark(listeners, i)
listeners[i] = false
listeners.marked = true
end

local function clean(listeners)
for i = #listeners, 1, -1 do
if not listeners[i] then
remove(listeners, i)
end
end
listeners.marked = nil
end

local once = setmetatable({}, {__mode = "k"})
Expand All @@ -56,16 +50,18 @@ end

function Emitter:emit(eventName, ...)
local listeners = self._listeners[checkType('string', eventName)]
local needsCleaning = false
for i = 1, #listeners do
local listener = listeners[i]
if listener then
if once[listener] then
mark(listeners, i)
listeners[i] = false
needsCleaning = true
end
listener:fire(...)
end
end
if listeners.marked then
if needsCleaning then
clean(listeners)
end
end
Expand Down

0 comments on commit 4736f7e

Please sign in to comment.