summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--DOCS/man/en/lua.rst5
-rw-r--r--player/lua/defaults.lua26
2 files changed, 31 insertions, 0 deletions
diff --git a/DOCS/man/en/lua.rst b/DOCS/man/en/lua.rst
index 2cf485ba35..34d1d0e8c3 100644
--- a/DOCS/man/en/lua.rst
+++ b/DOCS/man/en/lua.rst
@@ -235,6 +235,11 @@ The ``mp`` module is preloaded, although it can be loaded manually with
See `Events`_ and `List of events`_ for details.
+``mp.unregister_event(fn)``
+ Undo ``mp.register_event(..., fn)``. This removes all event handlers that
+ are equal to the ``fn`` parameter. This uses normal Lua ``==`` comparison,
+ so be careful when dealing with closures.
+
``mp.add_timeout(seconds, fn)``
Call the given function fn when the given number of seconds has elapsed.
Note that the number of seconds can be fractional. As of now, the timer
diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua
index 557a0ff4a7..a17e80a20f 100644
--- a/player/lua/defaults.lua
+++ b/player/lua/defaults.lua
@@ -235,6 +235,32 @@ function mp.register_event(name, cb)
return mp.request_event(name, true)
end
+function mp.unregister_event(cb)
+ for name, sub in pairs(event_handlers) do
+ local found = false
+ for i, e in ipairs(sub) do
+ if e == cb then
+ found = true
+ break
+ end
+ end
+ if found then
+ -- create a new array, just in case this function was called
+ -- from an event handler
+ local new = {}
+ for i = 1, #sub do
+ if sub[i] ~= cb then
+ new[#new + 1] = sub[i]
+ end
+ end
+ event_handlers[name] = new
+ if #new == 0 then
+ mp.request_event(name, false)
+ end
+ end
+ end
+end
+
-- default handlers
mp.register_event("shutdown", function() mp.keep_running = false end)
mp.register_event("script-input-dispatch", script_dispatch)