BlockColor-Engine/builtin/common/async_event.lua

41 lines
885 B
Lua
Raw Normal View History

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