1
0
mirror of https://github.com/moparisthebest/minetest synced 2024-10-31 23:45:05 -04:00
minetest/builtin/common/async_event.lua

41 lines
885 B
Lua
Raw Normal View History

2014-04-27 17:55:49 -04:00
core.async_jobs = {}
local function handle_job(jobid, serialized_retval)
local retval = core.deserialize(serialized_retval)
assert(type(core.async_jobs[jobid]) == "function")
core.async_jobs[jobid](retval)
core.async_jobs[jobid] = nil
end
2014-04-27 21:02:48 -04:00
if core.register_globalstep then
core.register_globalstep(function(dtime)
2014-04-27 17:55:49 -04:00
for i, job in ipairs(core.get_finished_jobs()) do
handle_job(job.jobid, job.retval)
end
end)
2014-04-27 21:02:48 -04:00
else
core.async_event_handler = handle_job
2014-04-27 17:55:49 -04:00
end
function core.handle_async(func, parameter, callback)
-- Serialize function
local serialized_func = string.dump(func)
assert(serialized_func ~= nil)
-- Serialize parameters
local serialized_param = core.serialize(parameter)
if serialized_param == nil then
return false
end
local jobid = core.do_async_callback(serialized_func, serialized_param)
core.async_jobs[jobid] = callback
return true
end