From 159379980ec8c79608b1adada4b66b1d8c016e4a Mon Sep 17 00:00:00 2001 From: wm4 Date: Thu, 10 May 2018 15:26:27 +0200 Subject: lua: expose async commands Might be useful for some. --- player/lua/defaults.lua | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'player/lua/defaults.lua') diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index d5bb194c50..6f5a9c4b6c 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -528,6 +528,27 @@ function mp.add_hook(name, pri, cb) mp.raw_hook_add(id, name, pri - 50) end +local async_call_table = {} +local async_next_id = 1 + +function mp.command_native_async(node, cb) + local id = async_next_id + async_next_id = async_next_id + 1 + async_call_table[id] = cb + mp.raw_command_native_async(id, node) +end + +mp.register_event("command-reply", function(ev) + local id = tonumber(ev.id) + cb = async_call_table[id] + async_call_table[id] = nil + if ev.error then + cb(false, nil, ev.error) + else + cb(true, ev.result, nil) + end +end) + local mp_utils = package.loaded["mp.utils"] function mp_utils.format_table(t, set) -- cgit v1.2.3