feat: integrate ts2lua-unreal v0.2.1 and UnLua TypeScript (.d.ts) generator
This commit is contained in:
1 parent
ebce41f347
commit
ee2e62bb34
282 files changed
+141794
-261
No files matched your search
@@ -1,94 +1,101 @@
|
||||
---@class Framework.EventBus
|
||||
---Lightweight publish/subscribe event bus for decoupled Lua game systems.
|
||||
local EventBus = {}
|
||||
|
||||
local ____exports = {}
|
||||
local Listeners = {}
|
||||
|
||||
---@param eventName string
|
||||
---@param callback function
|
||||
---@param owner any|nil
|
||||
function EventBus.On(eventName, callback, owner)
|
||||
assert(type(eventName) == "string", "EventBus.On: eventName must be a string")
|
||||
assert(type(callback) == "function", "EventBus.On: callback must be a function")
|
||||
|
||||
function ____exports.On(eventName, callback, owner)
|
||||
assert(
|
||||
type(eventName) == "string",
|
||||
"EventBus.On: eventName must be a string"
|
||||
)
|
||||
assert(
|
||||
type(callback) == "function",
|
||||
"EventBus.On: callback must be a function"
|
||||
)
|
||||
local list = Listeners[eventName]
|
||||
if not list then
|
||||
list = {}
|
||||
Listeners[eventName] = list
|
||||
end
|
||||
|
||||
for _, entry in ipairs(list) do
|
||||
if entry.callback == callback and entry.owner == owner then
|
||||
return
|
||||
do
|
||||
local i = 0
|
||||
while i < #list do
|
||||
local entry = list[i + 1]
|
||||
if entry.callback == callback and entry.owner == owner then
|
||||
return
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
list[#list + 1] = {
|
||||
callback = callback,
|
||||
owner = owner,
|
||||
}
|
||||
list[#list + 1] = {callback = callback, owner = owner}
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
---@param callback function
|
||||
---@param owner any|nil
|
||||
function EventBus.Off(eventName, callback, owner)
|
||||
function ____exports.Off(eventName, callback, owner)
|
||||
local list = Listeners[eventName]
|
||||
if not list then
|
||||
return
|
||||
end
|
||||
|
||||
for i = #list, 1, -1 do
|
||||
local entry = list[i]
|
||||
if entry.callback == callback and (owner == nil or entry.owner == owner) then
|
||||
table.remove(list, i)
|
||||
do
|
||||
local i = #list - 1
|
||||
while i >= 0 do
|
||||
local entry = list[i + 1]
|
||||
if entry.callback == callback and (owner == nil or entry.owner == owner) then
|
||||
table.remove(list, i + 1)
|
||||
end
|
||||
i = i - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param owner any
|
||||
function EventBus.OffAllForOwner(owner)
|
||||
function ____exports.OffAllForOwner(owner)
|
||||
if owner == nil then
|
||||
return
|
||||
end
|
||||
for _, list in pairs(Listeners) do
|
||||
for i = #list, 1, -1 do
|
||||
if list[i].owner == owner then
|
||||
table.remove(list, i)
|
||||
if list ~= nil then
|
||||
do
|
||||
local i = #list - 1
|
||||
while i >= 0 do
|
||||
if list[i + 1].owner == owner then
|
||||
table.remove(list, i + 1)
|
||||
end
|
||||
i = i - 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param eventName string
|
||||
function EventBus.Emit(eventName, ...)
|
||||
function ____exports.Emit(eventName, ...)
|
||||
local list = Listeners[eventName]
|
||||
if not list then
|
||||
return
|
||||
end
|
||||
|
||||
-- Snapshot to allow listeners to unsubscribe during dispatch
|
||||
local snapshot = {}
|
||||
for i = 1, #list do
|
||||
snapshot[i] = list[i]
|
||||
end
|
||||
|
||||
for i = 1, #snapshot do
|
||||
local entry = snapshot[i]
|
||||
local ok, err
|
||||
if entry.owner ~= nil then
|
||||
ok, err = pcall(entry.callback, entry.owner, ...)
|
||||
else
|
||||
ok, err = pcall(entry.callback, ...)
|
||||
do
|
||||
local i = 0
|
||||
while i < #list do
|
||||
snapshot[i + 1] = list[i + 1]
|
||||
i = i + 1
|
||||
end
|
||||
if not ok then
|
||||
print(string.format("[UnrealLuaDemo][ERROR] EventBus(%s): %s", eventName, tostring(err)))
|
||||
end
|
||||
do
|
||||
local i = 0
|
||||
while i < #snapshot do
|
||||
local entry = snapshot[i + 1]
|
||||
local ok
|
||||
local err
|
||||
if entry.owner ~= nil then
|
||||
ok, err = pcall(entry.callback, entry.owner, ...)
|
||||
else
|
||||
ok, err = pcall(entry.callback, ...)
|
||||
end
|
||||
if not ok then
|
||||
print(string.format(
|
||||
"[UnrealLuaDemo][ERROR] EventBus(%s): %s",
|
||||
eventName,
|
||||
tostring(err)
|
||||
))
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function EventBus.Clear()
|
||||
function ____exports.Clear()
|
||||
Listeners = {}
|
||||
end
|
||||
|
||||
return EventBus
|
||||
return ____exports
|
||||
Reference in new issue
Block a user