Files
chuanqi-server-instance/LogicServer/data/functions/Common/AsyncWorkDispatcher.lua
2024-12-16 20:45:03 +08:00

31 lines
1.0 KiB
Lua
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

module("AsyncWorkDispatcher", package.seeall)
local CallBackList = {}
-- @brief 添加异步工作
-- @param asyncWork 异步工作内容 => { 'function', arg1, arg2 ... }
-- @param callback 完成回调 格式: callback(paramPack,retParam...) => paramPack为传入参数包retParam为异步工作返回值
-- @param paramPack 回调参数包
-- @note asyncWork.function 在异步线程中调用,且应函数应定义在 AsyncWorkerFunction.txt
function Add(asyncWork,callback,paramPack)
local cbid = System.addAsyncWorker(asyncWork)
if cbid ~= nil and cbid ~= 0 and callback ~= nil then
CallBackList[cbid] = {callback, paramPack}
end
end
--------------------------------------------------------------------
-- 提供给CPP的回调
--------------------------------------------------------------------
function OnWorkFinish(cbid, empty, ...)
if not cbid then return end
local callback = CallBackList[cbid]
if callback ~= nil then
func = callback[1]
paramPack = callback[2]
func(paramPack, ...)
CallBackList[cbid] = nil
end
end