2
0

async_event.lua 885 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. core.async_jobs = {}
  2. local function handle_job(jobid, serialized_retval)
  3. local retval = core.deserialize(serialized_retval)
  4. assert(type(core.async_jobs[jobid]) == "function")
  5. core.async_jobs[jobid](retval)
  6. core.async_jobs[jobid] = nil
  7. end
  8. if core.register_globalstep then
  9. core.register_globalstep(function(dtime)
  10. for i, job in ipairs(core.get_finished_jobs()) do
  11. handle_job(job.jobid, job.retval)
  12. end
  13. end)
  14. else
  15. core.async_event_handler = handle_job
  16. end
  17. function core.handle_async(func, parameter, callback)
  18. -- Serialize function
  19. local serialized_func = string.dump(func)
  20. assert(serialized_func ~= nil)
  21. -- Serialize parameters
  22. local serialized_param = core.serialize(parameter)
  23. if serialized_param == nil then
  24. return false
  25. end
  26. local jobid = core.do_async_callback(serialized_func, serialized_param)
  27. core.async_jobs[jobid] = callback
  28. return true
  29. end