mirror of https://github.com/mpv-player/mpv
lua: add mp.unregister_event() function
Someone requested this... I think.
This commit is contained in:
parent
84ab860031
commit
3207366daa
|
@ -235,6 +235,11 @@ The ``mp`` module is preloaded, although it can be loaded manually with
|
||||||
|
|
||||||
See `Events`_ and `List of events`_ for details.
|
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)``
|
``mp.add_timeout(seconds, fn)``
|
||||||
Call the given function fn when the given number of seconds has elapsed.
|
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
|
Note that the number of seconds can be fractional. As of now, the timer
|
||||||
|
|
|
@ -235,6 +235,32 @@ function mp.register_event(name, cb)
|
||||||
return mp.request_event(name, true)
|
return mp.request_event(name, true)
|
||||||
end
|
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
|
-- default handlers
|
||||||
mp.register_event("shutdown", function() mp.keep_running = false end)
|
mp.register_event("shutdown", function() mp.keep_running = false end)
|
||||||
mp.register_event("script-input-dispatch", script_dispatch)
|
mp.register_event("script-input-dispatch", script_dispatch)
|
||||||
|
|
Loading…
Reference in New Issue