local msg = {}
-- 存储注册的函数
local registeredFunctions = {}
function msg.reg(key, func)
-- 检查是否已经有函数注册在这个键下
if not registeredFunctions[key] then
registeredFunctions[key] = {}
end
-- 将函数添加到注册列表中
table.insert(registeredFunctions[key], func)
end
function msg.run(key, ...)
-- 检查是否有注册在这个键下的函数
if registeredFunctions[key] then
for _, func in ipairs(registeredFunctions[key]) do
-- 调用每个注册的函数,并输出调用信息
print(key, ...)
func(...)
end
else
print("No functions registered under key:", key)
end
end
return msg
function f(...)
print("ddd", ...)
end
-- 示例注册
msg.reg("run", f)
function b(...)
print("xxx", ...)
end
msg.reg("run", b)
-- 示例调用
msg.run("run", 1, 2, 3)
|