EventEmitter: add debug printing (disabled by default)

This commit is contained in:
Starbeamrainbowlabs 2022-09-25 15:03:16 +01:00
parent 9806828760
commit 9a4b4beb76
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -1,4 +1,4 @@
local wea_c = worldeditadditions_core
--- Event manager object. --- Event manager object.
-- @class worldeditadditions_core.EventEmitter -- @class worldeditadditions_core.EventEmitter
local EventEmitter = {} local EventEmitter = {}
@ -10,7 +10,8 @@ EventEmitter.__name = "EventEmitter" -- A hack to allow identification in wea.in
-- @returns table The new EventEmitter object. If a table is passed in, a new table is NOT created. -- @returns table The new EventEmitter object. If a table is passed in, a new table is NOT created.
function EventEmitter.new(tbl) function EventEmitter.new(tbl)
if not tbl then tbl = {} end if not tbl then tbl = {} end
tbl.events = {} tbl.events = { }
tbl.debug = false
setmetatable(tbl, EventEmitter) setmetatable(tbl, EventEmitter)
return tbl return tbl
end end
@ -58,6 +59,8 @@ end
-- @param args table|any The argument(s) to pass to listener functions. It is strongly advised you pass a table here. -- @param args table|any The argument(s) to pass to listener functions. It is strongly advised you pass a table here.
function EventEmitter.emit(this, event_name, args) function EventEmitter.emit(this, event_name, args)
if this.events[event_name] == nil then return end if this.events[event_name] == nil then return end
if this.debug then print("DEBUG:EventEmitter emit", event_name, "args", wea_c.inspect(args)) end
for index,next_func in ipairs(this.events[event_name]) do for index,next_func in ipairs(this.events[event_name]) do
next_func(args) next_func(args)
end end