diff options
author | wm4 <wm4@nowhere> | 2020-08-05 22:58:19 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2020-08-05 23:19:41 +0200 |
commit | d0ab562b1fb22490799f42f3f90b61f01b593bab (patch) | |
tree | 9aa3131e9a561983ad8934881d3e6975783eaf22 /player | |
parent | 13d354e46d27fd0c433880839abcf9096dbcbc2f (diff) | |
download | mpv-d0ab562b1fb22490799f42f3f90b61f01b593bab.tar.bz2 mpv-d0ab562b1fb22490799f42f3f90b61f01b593bab.tar.xz |
lua: make hook processing more flexible
This can now opt to not continue a hook after the hook callback returns.
This makes it easier for scripts, and may make it unnecessary to run
reentrant event loops etc. for scripts that want to wait before
continuing while still running the event loop.
Diffstat (limited to 'player')
-rw-r--r-- | player/lua/defaults.lua | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 72920cea67..95f8952f5d 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -547,12 +547,35 @@ end local hook_table = {} +local hook_mt = {} +hook_mt.__index = hook_mt + +function hook_mt.cont(t) + if t._id == nil then + mp.msg.error("hook already continued") + else + mp.raw_hook_continue(t._id) + t._id = nil + end +end + +function hook_mt.defer(t) + t._defer = true +end + mp.register_event("hook", function(ev) local fn = hook_table[tonumber(ev.id)] + local hookobj = { + _id = ev.hook_id, + _defer = false, + } + setmetatable(hookobj, hook_mt) if fn then - fn() + fn(hookobj) + end + if (not hookobj._defer) and hookobj._id ~= nil then + hookobj:cont() end - mp.raw_hook_continue(ev.hook_id) end) function mp.add_hook(name, pri, cb) |