aboutsummaryrefslogtreecommitdiff
path: root/builtin/mainmenu/async_event.lua
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2020-05-15 16:38:19 +0200
committersfan5 <sfan5@live.de>2020-05-17 21:37:01 +0200
commit11723cffe011e99c1092388958fb1a047ab1c09c (patch)
tree0a38f434c15893ba01bfa5ca80708780bcfead60 /builtin/mainmenu/async_event.lua
parent52430d34d33f0ae3ff188a009307bf0a86048dfe (diff)
downloadminetest-11723cffe011e99c1092388958fb1a047ab1c09c.tar.gz
minetest-11723cffe011e99c1092388958fb1a047ab1c09c.tar.bz2
minetest-11723cffe011e99c1092388958fb1a047ab1c09c.zip
builtin: Move common/async_event.lua to mainmenu/async_event.lua
It only works in the context of the mainmenu.
Diffstat (limited to 'builtin/mainmenu/async_event.lua')
-rw-r--r--builtin/mainmenu/async_event.lua32
1 files changed, 32 insertions, 0 deletions
diff --git a/builtin/mainmenu/async_event.lua b/builtin/mainmenu/async_event.lua
new file mode 100644
index 000000000..04bfb78d6
--- /dev/null
+++ b/builtin/mainmenu/async_event.lua
@@ -0,0 +1,32 @@
+
+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
+
+core.async_event_handler = handle_job
+
+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
+