1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00
luanti/builtin/game/async.lua

32 lines
825 B
Lua
Raw Normal View History

core.async_jobs = {}
function core.async_event_handler(jobid, retval)
local callback = core.async_jobs[jobid]
assert(type(callback) == "function")
callback(unpack(retval, 1, retval.n))
core.async_jobs[jobid] = nil
end
local job_metatable = {__index = {}}
function job_metatable.__index:cancel()
local cancelled = core.cancel_async_callback(self.id)
if cancelled then
core.async_jobs[self.id] = nil
end
return cancelled
end
function core.handle_async(func, callback, ...)
assert(type(func) == "function" and type(callback) == "function",
2024-10-28 19:40:18 +01:00
"Invalid core.handle_async invocation")
local args = {n = select("#", ...), ...}
local mod_origin = core.get_last_run_mod()
local id = core.do_async_callback(func, args, mod_origin)
core.async_jobs[id] = callback
return setmetatable({id = id}, job_metatable)
end