async_event.lua 699 B

1234567891011121314151617181920212223242526272829303132
  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. core.async_event_handler = handle_job
  9. function core.handle_async(func, parameter, callback)
  10. -- Serialize function
  11. local serialized_func = string.dump(func)
  12. assert(serialized_func ~= nil)
  13. -- Serialize parameters
  14. local serialized_param = core.serialize(parameter)
  15. if serialized_param == nil then
  16. return false
  17. end
  18. local jobid = core.do_async_callback(serialized_func, serialized_param)
  19. core.async_jobs[jobid] = callback
  20. return true
  21. end