1
mirror of https://github.com/mpv-player/mpv synced 2024-07-11 23:47:56 +02:00

lua: make register_event() not overwrite previous event handler

Instead, chain them.

Note that there's no logic to prevent the other event handlers to be run
from an event handler (like it's popular in GUI toolkits), because I
think that's not very useful for this purpose.
This commit is contained in:
wm4 2014-02-14 13:48:08 +01:00
parent 99e38aee9a
commit 414c4f9322
2 changed files with 15 additions and 4 deletions

View File

@ -100,6 +100,10 @@ The ``mp`` module is preloaded, although it can be loaded manually with
associated, the ``error`` field is set to a string describing the error,
on success it's not set.
If multiple functions are registered for the same event, they are run in
registration order, which the first registered function running before all
the other ones.
Returns true if such an event exists, false otherwise.
See `Events`_ and `List of events`_ for details.

View File

@ -135,7 +135,12 @@ mp.keep_running = true
local event_handlers = {}
function mp.register_event(name, cb)
event_handlers[name] = cb
local list = event_handlers[name]
if not list then
list = {}
event_handlers[name] = list
end
list[#list + 1] = cb
return mp.request_event(name, true)
end
@ -180,9 +185,11 @@ _G.mp_event_loop = function()
mp.suspend()
more_events = (e.event ~= "none")
if more_events then
local handler = event_handlers[e.event]
if handler then
handler(e)
local handlers = event_handlers[e.event]
if handlers then
for _, handler in ipairs(handlers) do
handler(e)
end
end
end
end