Minetest-WorldEditAdditions/worldeditadditions_core/utils/EventEmitter.lua

87 lines
3.8 KiB
Lua
Raw Permalink Normal View History

local wea_c = worldeditadditions_core
2022-09-18 15:19:52 +00:00
--- Event manager object.
-- @class worldeditadditions_core.EventEmitter
2022-09-18 14:57:23 +00:00
local EventEmitter = {}
EventEmitter.__index = EventEmitter
EventEmitter.__name = "EventEmitter" -- A hack to allow identification in wea.inspect
2022-09-18 15:19:52 +00:00
--- Create a new event emitter.
-- @param tbl table|nil Optional. A table to base the new EventEmitter object on. EventEmitter will attach ann instance of itself to this object. The 'events' property on the object MUST NOT be set. IT WILL BE OVERWRITTEN!
-- @returns table The new EventEmitter object. If a table is passed in, a new table is NOT created.
2022-09-18 15:06:23 +00:00
function EventEmitter.new(tbl)
if not tbl then tbl = {} end
tbl.events = { }
tbl.debug = false
2022-09-18 15:06:23 +00:00
setmetatable(tbl, EventEmitter)
return tbl
2022-09-18 14:57:23 +00:00
end
2022-09-18 15:19:52 +00:00
--- Add a new listener to the given named event.
-- @param this EventEmitter The EventEmitter instance to add the listener to.
-- @param event_name string The name of the event to listen on.
-- @param func function The callback function to call when the event is emitted. Will be passed a single argument - the object passed as the second argument to .emit().
2022-09-18 15:19:52 +00:00
-- @returns number The number of listeners attached to that event.
2022-09-18 14:57:23 +00:00
function EventEmitter.addEventListener(this, event_name, func)
if this.events[event_name] == nil then this.events[event_name] = {} end
table.insert(this.events[event_name], func)
2022-09-18 15:19:52 +00:00
local count = #this.events[event_name]
if count > 10 then
minetest.log("warning", count.." listeners registered for event "..event_name..", possible leak? You MUST remove listeners when you're done with them!")
end
return count
2022-09-18 14:57:23 +00:00
end
function EventEmitter.on(this, event_name, func)
return EventEmitter.addEventListener(this, event_name, func)
end
2022-09-18 15:19:52 +00:00
--- Remove an existing listener from the given named event.
-- @param this EventEmitter The EventEmitter instance to remove the listener from.
-- @param event_name string The name of the event to remove from.
-- @param func function The callback function to remove from the list of listeners for the given named event.
-- @return bool Whether it was actually removed or not. False means it failed to find the given listener function.
2022-09-18 14:57:23 +00:00
function EventEmitter.removeEventListener(this, event_name, func)
if this.events[event_name] == nil then return false end
for index, next_func in ipairs(this.events[event_name]) do
if next_func == func then
table.remove(this.events[event_name], index)
break
end
end
2022-09-18 15:19:52 +00:00
return true
2022-09-18 14:57:23 +00:00
end
function EventEmitter.off(this, event_name, func)
return EventEmitter.removeEventListener(this, event_name, func)
end
2022-09-18 15:19:52 +00:00
--- Emit an event.
-- @param this EventEmitter The EventEmitter instance to emit the event from.
-- @param event_name string The name of the event to emit.
-- @param args table|any The argument(s) to pass to listener functions. It is strongly advised you pass a table here.
2022-09-18 14:57:23 +00:00
function EventEmitter.emit(this, event_name, args)
2023-07-31 21:06:44 +00:00
if this.debug then
2023-11-28 00:33:27 +00:00
local listeners = 0
2023-07-31 21:06:44 +00:00
if this.events[event_name] ~= nil then listeners = #this.events[event_name] end
print("DEBUG:EventEmitter emit", event_name, "listeners", listeners, "args", wea_c.inspect(args))
end
2022-09-18 14:57:23 +00:00
if this.events[event_name] == nil then return end
2022-09-18 14:57:23 +00:00
for index,next_func in ipairs(this.events[event_name]) do
2022-09-18 15:19:52 +00:00
next_func(args)
2022-09-18 14:57:23 +00:00
end
-- TODO: Monitor execution time, log if it's slow? We'll need to move WEA utils over to wea_c first though...
end
2022-09-18 15:19:52 +00:00
--- Clear all listeners from a given event name.
-- @param this EventEmitter The EventEmitter instance to clear listeners from.
-- @param event_name string The name of the event to clear the listeners from.
2022-09-18 14:57:23 +00:00
function EventEmitter.clear(this, event_name)
this.events[event_name] = nil
end
2022-09-18 15:19:52 +00:00
--- Clear all listeners.
-- @param this EventEmitter The EventEmitter instance to clear listeners from.
function EventEmitter.clear_all(this, event_name)
this.events = {}
end
2022-09-18 14:57:23 +00:00
return EventEmitter