2013-11-26 12:15:31 -05:00
|
|
|
local tbl = engine or minetest
|
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
local SCRIPTDIR = SCRIPTDIR or tbl.get_scriptdir()
|
|
|
|
minetest = tbl
|
|
|
|
dofile(SCRIPTDIR .. DIR_DELIM .. "serialize.lua")
|
|
|
|
|
2013-11-26 12:15:31 -05:00
|
|
|
tbl.async_jobs = {}
|
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
local function handle_job(jobid, serialized_retval)
|
|
|
|
local retval = tbl.deserialize(serialized_retval)
|
|
|
|
assert(type(tbl.async_jobs[jobid]) == "function")
|
|
|
|
tbl.async_jobs[jobid](retval)
|
|
|
|
tbl.async_jobs[jobid] = nil
|
|
|
|
end
|
2013-11-26 12:15:31 -05:00
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
if engine ~= nil then
|
|
|
|
tbl.async_event_handler = handle_job
|
2013-11-26 12:15:31 -05:00
|
|
|
else
|
2014-04-15 13:41:07 -04:00
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
for i, job in ipairs(tbl.get_finished_jobs()) do
|
|
|
|
handle_job(job.jobid, job.retval)
|
|
|
|
end
|
|
|
|
end)
|
2013-11-26 12:15:31 -05:00
|
|
|
end
|
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
function tbl.handle_async(func, parameter, callback)
|
|
|
|
-- Serialize function
|
|
|
|
local serialized_func = string.dump(func)
|
2013-11-26 12:15:31 -05:00
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
assert(serialized_func ~= nil)
|
2013-11-26 12:15:31 -05:00
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
-- Serialize parameters
|
|
|
|
local serialized_param = tbl.serialize(parameter)
|
2013-11-26 12:15:31 -05:00
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
if serialized_param == nil then
|
2013-11-26 12:15:31 -05:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2014-04-15 13:41:07 -04:00
|
|
|
local jobid = tbl.do_async_callback(serialized_func, serialized_param)
|
2013-11-26 12:15:31 -05:00
|
|
|
|
|
|
|
tbl.async_jobs[jobid] = callback
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
2014-04-15 13:41:07 -04:00
|
|
|
|