1
0
mirror of https://github.com/minetest/minetest.git synced 2024-11-16 15:30:27 +01:00
minetest/builtin/common/async_event.lua

41 lines
885 B
Lua
Raw Normal View History

2014-04-27 23:55:49 +02: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-28 03:02:48 +02:00
if core.register_globalstep then
core.register_globalstep(function(dtime)
2014-04-27 23:55:49 +02:00
for i, job in ipairs(core.get_finished_jobs()) do
handle_job(job.jobid, job.retval)
end
end)
2014-04-28 03:02:48 +02:00
else
core.async_event_handler = handle_job
2014-04-27 23:55:49 +02: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