282 lines
7.9 KiB
Lua
282 lines
7.9 KiB
Lua
local ____lualib = require("lualib_bundle")
|
|||
|
|
local __TS__New = ____lualib.__TS__New
|
||
|
|
local ____exports = __module.env
|
||
|
|
local M__share_base_traceback = __MODULE__.Import("share.base.traceback", __module)
|
||
|
|
local tbpcall = M__share_base_traceback.tbpcall
|
||
|
|
local EMPTY_SLOT = -999999
|
||
|
|
---
|
||
|
|
-- @module_data
|
||
|
|
local nextEmitterId = 0
|
||
|
|
---
|
||
|
|
-- @module_data
|
||
|
|
local globalOwnerFilter = nil
|
||
|
|
---
|
||
|
|
-- @noSelf
|
||
|
|
local function AllocateEmitterId()
|
||
|
|
nextEmitterId = nextEmitterId + 1
|
||
|
|
return nextEmitterId
|
||
|
|
end
|
||
|
|
---
|
||
|
|
-- @noSelf
|
||
|
|
local function IsOwnerDisposed(owner)
|
||
|
|
if owner == nil then
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
if globalOwnerFilter ~= nil and globalOwnerFilter(owner) then
|
||
|
|
return true
|
||
|
|
end
|
||
|
|
local checkFn = owner.IsDisposed
|
||
|
|
if checkFn ~= nil and type(checkFn) == "function" then
|
||
|
|
return checkFn(owner) == true
|
||
|
|
end
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
---
|
||
|
|
-- @noSelf
|
||
|
|
function ____exports.SetGlobalEventFilter(filter)
|
||
|
|
globalOwnerFilter = filter
|
||
|
|
end
|
||
|
|
____exports.EvListener = __TS__DefineClass("EvListener", __module)
|
||
|
|
local EvListener = ____exports.EvListener
|
||
|
|
EvListener.name = "EvListener"
|
||
|
|
function EvListener.prototype.____constructor(self, channel, callable, target, ekey)
|
||
|
|
self.enabled = true
|
||
|
|
self.channel = channel
|
||
|
|
self.callable = callable
|
||
|
|
self.target = target
|
||
|
|
self.ekey = ekey
|
||
|
|
self.enabled = true
|
||
|
|
end
|
||
|
|
function EvListener.prototype.Off(self)
|
||
|
|
if self.channel == nil then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
local ch = self.channel
|
||
|
|
self.channel = nil
|
||
|
|
ch:Off(self)
|
||
|
|
local owner = self.target
|
||
|
|
local key = self.ekey
|
||
|
|
if owner ~= nil and key ~= nil then
|
||
|
|
local ownerEvents = rawget(owner, "__events__")
|
||
|
|
if ownerEvents ~= nil and ownerEvents[key] == self then
|
||
|
|
ownerEvents[key] = nil
|
||
|
|
end
|
||
|
|
end
|
||
|
|
self.callable = nil
|
||
|
|
self.target = nil
|
||
|
|
self.ekey = nil
|
||
|
|
self.enabled = false
|
||
|
|
end
|
||
|
|
function EvListener.prototype.Enable(self)
|
||
|
|
self.enabled = true
|
||
|
|
end
|
||
|
|
function EvListener.prototype.Disable(self)
|
||
|
|
self.enabled = false
|
||
|
|
end
|
||
|
|
function EvListener.prototype.IsActive(self)
|
||
|
|
return self.channel ~= nil and self.enabled
|
||
|
|
end
|
||
|
|
function EvListener.prototype.Dispose(self)
|
||
|
|
self:Off()
|
||
|
|
end
|
||
|
|
____exports.EvChannel = __TS__DefineClass("EvChannel", __module)
|
||
|
|
local EvChannel = ____exports.EvChannel
|
||
|
|
EvChannel.name = "EvChannel"
|
||
|
|
function EvChannel.prototype.____constructor(self, emitter, event)
|
||
|
|
self.listeners = {}
|
||
|
|
self.liveCount = 0
|
||
|
|
self.indexes = {}
|
||
|
|
self.dispatchDepth = 0
|
||
|
|
self.emitter = emitter
|
||
|
|
self.event = event
|
||
|
|
end
|
||
|
|
function EvChannel.prototype.On(self, callable, target, ekey)
|
||
|
|
assert(
|
||
|
|
type(callable) == "function",
|
||
|
|
"EvChannel.On: callable must be a function"
|
||
|
|
)
|
||
|
|
local listener = __TS__New(
|
||
|
|
____exports.EvListener,
|
||
|
|
self,
|
||
|
|
callable,
|
||
|
|
target,
|
||
|
|
ekey
|
||
|
|
)
|
||
|
|
local idx = #self.listeners
|
||
|
|
self.listeners[idx + 1] = listener
|
||
|
|
self.liveCount = self.liveCount + 1
|
||
|
|
self.indexes[listener] = idx
|
||
|
|
return listener
|
||
|
|
end
|
||
|
|
function EvChannel.prototype.Off(self, listener)
|
||
|
|
local idx = self.indexes[listener]
|
||
|
|
if idx == nil then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
self.listeners[idx + 1] = EMPTY_SLOT
|
||
|
|
self.liveCount = self.liveCount - 1
|
||
|
|
self.indexes[listener] = nil
|
||
|
|
if self.dispatchDepth == 0 then
|
||
|
|
self:TryPurge()
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function EvChannel.prototype.TryPurge(self)
|
||
|
|
if #self.listeners - self.liveCount <= 4 then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
local compacted = {}
|
||
|
|
local newIndexes = {}
|
||
|
|
do
|
||
|
|
local i = 0
|
||
|
|
while i < #self.listeners do
|
||
|
|
local item = self.listeners[i + 1]
|
||
|
|
if item ~= EMPTY_SLOT then
|
||
|
|
local newIdx = #compacted
|
||
|
|
compacted[newIdx + 1] = item
|
||
|
|
newIndexes[item] = newIdx
|
||
|
|
end
|
||
|
|
i = i + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
self.listeners = compacted
|
||
|
|
self.liveCount = #compacted
|
||
|
|
self.indexes = newIndexes
|
||
|
|
end
|
||
|
|
function EvChannel.prototype.Emit(self, unsafeCall, ...)
|
||
|
|
self.dispatchDepth = self.dispatchDepth + 1
|
||
|
|
local list = self.listeners
|
||
|
|
do
|
||
|
|
local i = 0
|
||
|
|
while i < #list do
|
||
|
|
local listener = list[i + 1]
|
||
|
|
if listener ~= EMPTY_SLOT and listener.enabled then
|
||
|
|
local owner = listener.target
|
||
|
|
if not IsOwnerDisposed(owner) then
|
||
|
|
local callable = listener.callable
|
||
|
|
if callable ~= nil then
|
||
|
|
if unsafeCall then
|
||
|
|
callable(...)
|
||
|
|
else
|
||
|
|
tbpcall(callable, ...)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
i = i + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
self.dispatchDepth = self.dispatchDepth - 1
|
||
|
|
if self.dispatchDepth == 0 then
|
||
|
|
self:TryPurge()
|
||
|
|
end
|
||
|
|
end
|
||
|
|
____exports.EvEmitter = __TS__DefineClass("EvEmitter", __module)
|
||
|
|
local EvEmitter = ____exports.EvEmitter
|
||
|
|
EvEmitter.name = "EvEmitter"
|
||
|
|
function EvEmitter.prototype.____constructor(self, name)
|
||
|
|
self.channels = {}
|
||
|
|
self.enableUnsafeCall = false
|
||
|
|
self.name = name
|
||
|
|
self.id = AllocateEmitterId()
|
||
|
|
self.key = (name .. ":") .. tostring(self.id)
|
||
|
|
end
|
||
|
|
function EvEmitter.prototype.On(self, event, callable, target)
|
||
|
|
assert(
|
||
|
|
type(target) == "table",
|
||
|
|
"EvEmitter.On: owner(target) must be a table/object"
|
||
|
|
)
|
||
|
|
assert(
|
||
|
|
type(callable) == "function",
|
||
|
|
"EvEmitter.On: callable must be a function"
|
||
|
|
)
|
||
|
|
local ekey = (self.key .. ":") .. event
|
||
|
|
local targetEvents = rawget(target, "__events__")
|
||
|
|
if targetEvents == nil then
|
||
|
|
targetEvents = {}
|
||
|
|
setmetatable(targetEvents, {__mode = "v"})
|
||
|
|
rawset(target, "__events__", targetEvents)
|
||
|
|
end
|
||
|
|
local existing = targetEvents[ekey]
|
||
|
|
if existing ~= nil then
|
||
|
|
existing:Off()
|
||
|
|
end
|
||
|
|
local channel = self.channels[event]
|
||
|
|
if channel == nil then
|
||
|
|
channel = __TS__New(____exports.EvChannel, self, event)
|
||
|
|
self.channels[event] = channel
|
||
|
|
end
|
||
|
|
local listener = channel:On(callable, target, ekey)
|
||
|
|
targetEvents[ekey] = listener
|
||
|
|
return listener
|
||
|
|
end
|
||
|
|
function EvEmitter.prototype.Emit(self, event, ...)
|
||
|
|
local channel = self.channels[event]
|
||
|
|
if channel == nil then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
channel:Emit(self.enableUnsafeCall, ...)
|
||
|
|
end
|
||
|
|
function EvEmitter.prototype.HasOnEvent(self, event)
|
||
|
|
local channel = self.channels[event]
|
||
|
|
return channel ~= nil and channel.liveCount > 0
|
||
|
|
end
|
||
|
|
function EvEmitter.prototype.GetListenerCount(self, event)
|
||
|
|
if event ~= nil then
|
||
|
|
local ch = self.channels[event]
|
||
|
|
return ch ~= nil and ch.liveCount or 0
|
||
|
|
end
|
||
|
|
local total = 0
|
||
|
|
for _, ch in pairs(self.channels) do
|
||
|
|
if ch ~= nil then
|
||
|
|
total = total + ch.liveCount
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return total
|
||
|
|
end
|
||
|
|
function EvEmitter.prototype.ClearAll(self)
|
||
|
|
for evName, ch in pairs(self.channels) do
|
||
|
|
if ch ~= nil then
|
||
|
|
local list = ch.listeners
|
||
|
|
do
|
||
|
|
local i = 0
|
||
|
|
while i < #list do
|
||
|
|
local listener = list[i + 1]
|
||
|
|
if listener ~= EMPTY_SLOT then
|
||
|
|
listener:Off()
|
||
|
|
end
|
||
|
|
i = i + 1
|
||
|
|
end
|
||
|
|
end
|
||
|
|
self.channels[evName] = nil
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function EvEmitter.prototype.Dispose(self)
|
||
|
|
self:ClearAll()
|
||
|
|
end
|
||
|
|
function __module.StashData(env, to)
|
||
|
|
local ____has = to.__has__
|
||
|
|
if not ____has then
|
||
|
|
____has = {}
|
||
|
|
to.__has__ = ____has
|
||
|
|
end
|
||
|
|
to.nextEmitterId = nextEmitterId
|
||
|
|
____has.nextEmitterId = true
|
||
|
|
to.globalOwnerFilter = globalOwnerFilter
|
||
|
|
____has.globalOwnerFilter = true
|
||
|
|
end
|
||
|
|
function __module.RestoreData(env, from)
|
||
|
|
local ____has = from.__has__
|
||
|
|
if not ____has then
|
||
|
|
return
|
||
|
|
end
|
||
|
|
if ____has.nextEmitterId then
|
||
|
|
nextEmitterId = from.nextEmitterId
|
||
|
|
end
|
||
|
|
if ____has.globalOwnerFilter then
|
||
|
|
globalOwnerFilter = from.globalOwnerFilter
|
||
|
|
end
|
||
|
|
end
|
||
|
|
function __module.UpdateImports()
|
||
|
|
tbpcall = M__share_base_traceback.tbpcall
|
||
|
|
end
|