local ____lualib = require("lualib_bundle") local __TS__New = ____lualib.__TS__New local ____exports = __module.env local M__share_event_events = __MODULE__.Import("share.event.events", __module) local EvEmitter = M__share_event_events.EvEmitter ____exports.EventListenerContainer = __TS__DefineClass("EventListenerContainer", __module) local EventListenerContainer = ____exports.EventListenerContainer EventListenerContainer.name = "EventListenerContainer" function EventListenerContainer.prototype.____constructor(self) end function EventListenerContainer.prototype.GetAllEventListenerTable(self) return rawget(self, "__events__") end function EventListenerContainer.prototype.EnableAllEventListeners(self) local tbl = self:GetAllEventListenerTable() if tbl ~= nil then for _, listener in pairs(tbl) do if listener ~= nil then listener:Enable() end end end end function EventListenerContainer.prototype.DisableAllEventListeners(self) local tbl = self:GetAllEventListenerTable() if tbl ~= nil then for _, listener in pairs(tbl) do if listener ~= nil then listener:Disable() end end end end function EventListenerContainer.prototype.OffAllEventListeners(self) local tbl = self:GetAllEventListenerTable() if tbl ~= nil then local keys = {} for ekey in pairs(tbl) do keys[#keys + 1] = ekey end do local i = 0 while i < #keys do local key = keys[i + 1] local listener = tbl[key] if listener ~= nil then listener:Off() tbl[key] = nil end i = i + 1 end end end end function EventListenerContainer.prototype.GetActiveEventListenerCount(self) local tbl = self:GetAllEventListenerTable() if tbl == nil then return 0 end local count = 0 for _, listener in pairs(tbl) do if listener ~= nil and listener:IsActive() then count = count + 1 end end return count end function EventListenerContainer.prototype.Dispose(self) self:OffAllEventListeners() end ____exports.EventHub = __TS__DefineClass("EventHub", __module) local EventHub = ____exports.EventHub EventHub.name = "EventHub" function EventHub.prototype.____constructor(self, name, options) self.emit = {} self.on = {} self.disposed = false self.ev = __TS__New(EvEmitter, name) if options ~= nil then self.preEmit = options.preEmit self.asyncEmit = options.asyncEmit end self:SetupEmit() self:SetupOn() end function EventHub.prototype.Dispose(self) if self.disposed then return end self.disposed = true self.ev:Dispose() end function EventHub.prototype.IsDisposed(self) return self.disposed end function EventHub.prototype.HasOnEvent(self, name) return self.ev:HasOnEvent(name) end function EventHub.prototype.GetListenerCount(self, name) return self.ev:GetListenerCount(name) end function EventHub.prototype.Emit(self, name, ...) if self.disposed then return end if self.preEmit ~= nil then self.preEmit(name) end self.ev:Emit(name, ...) end function EventHub.prototype.EmitWithArgs(self, name, packed) if self.disposed then return end if self.preEmit ~= nil then self.preEmit(name) end self.ev:Emit( name, table.unpack(packed, 1, packed.n) ) end function EventHub.prototype.SetupEmit(self) local hub = self local mt = { __name = "EventHubEmit", __index = function(tbl, name) local function dispatch(...) if hub.disposed then return end if hub.asyncEmit ~= nil then local packed = table.pack(...) hub.asyncEmit( name, function() hub:EmitWithArgs(name, packed) end ) else hub:Emit(name, ...) end end rawset(tbl, name, dispatch) return dispatch end } setmetatable(self.emit, mt) end function EventHub.prototype.SetupOn(self) local ev = self.ev local mt = { __name = "EventHubOn", __index = function(tbl, name) local function subscribe(handle, ctx) return ev:On(name, handle, ctx) end rawset(tbl, name, subscribe) return subscribe end } setmetatable(self.on, mt) end function __module.UpdateImports() EvEmitter = M__share_event_events.EvEmitter end