summaryrefslogtreecommitdiffstats
path: root/player/lua
diff options
context:
space:
mode:
authorGuido Cella <guido@guidocella.xyz>2024-01-14 08:00:08 +0100
committerDudemanguy <random342@airmail.cc>2024-01-20 16:09:34 +0000
commitde0849404b65876f0f8fe934c4b74f497bf4e29b (patch)
treed1d8d91b08efa1d88e8b5697f5e8b088012a0404 /player/lua
parentc209d4f73bc69778a31cf1db975379be91a7f26b (diff)
downloadmpv-de0849404b65876f0f8fe934c4b74f497bf4e29b.tar.bz2
mpv-de0849404b65876f0f8fe934c4b74f497bf4e29b.tar.xz
scripting: don't observe properties with type nil
mp.observe_property('foo', nil, ...) calls the handler at least 2 times on each playlist change even when the property doesn't change. This is dangerous because if you haven't read observe_property's documentation in a long time this is easy to forget, and you can end up using it for handlers that are computationally expensive or that cause unintended side effects. Therefore, this commit discourages its use more explicitly in the documentation, and replaces its usages in scripts. For console.lua, observing focused with type nil leads to calling mp.osd_message('') when changing file while playing in the terminal with the console disabled. I don't notice issues from this, but it's safer to avoid it. For playlist and track-list this doesn't really matter since they trigger multiple changes on each new file anyway, but changing it can avoid encouraging people to imitate the code. One usage of none in stats.lua is kept because according to b9084dfd47 it is a hack to replicate the deprecated tick event.
Diffstat (limited to 'player/lua')
-rw-r--r--player/lua/console.lua2
-rw-r--r--player/lua/osc.lua6
2 files changed, 4 insertions, 4 deletions
diff --git a/player/lua/console.lua b/player/lua/console.lua
index 941f1a114e..4625c4ce3c 100644
--- a/player/lua/console.lua
+++ b/player/lua/console.lua
@@ -1470,7 +1470,7 @@ end)
mp.observe_property('osd-width', 'native', update)
mp.observe_property('osd-height', 'native', update)
mp.observe_property('display-hidpi-scale', 'native', update)
-mp.observe_property('focused', nil, update)
+mp.observe_property('focused', 'native', update)
-- Enable log messages. In silent mode, mpv will queue log messages in a buffer
-- until enable_messages is called again without the silent: prefix.
diff --git a/player/lua/osc.lua b/player/lua/osc.lua
index 414066313d..7221de9867 100644
--- a/player/lua/osc.lua
+++ b/player/lua/osc.lua
@@ -2734,7 +2734,7 @@ function update_duration_watch()
if want_watch ~= duration_watched then
if want_watch then
- mp.observe_property("duration", nil, on_duration)
+ mp.observe_property("duration", "native", on_duration)
else
mp.unobserve_property(on_duration)
end
@@ -2747,8 +2747,8 @@ update_duration_watch()
mp.register_event("shutdown", shutdown)
mp.register_event("start-file", request_init)
-mp.observe_property("track-list", nil, request_init)
-mp.observe_property("playlist", nil, request_init)
+mp.observe_property("track-list", "native", request_init)
+mp.observe_property("playlist", "native", request_init)
mp.observe_property("chapter-list", "native", function(_, list)
list = list or {} -- safety, shouldn't return nil
table.sort(list, function(a, b) return a.time < b.time end)