@@ -2,6 +2,9 @@ local Thumbnailer = {
22 cache_directory = thumbnailer_options .cache_directory ,
33
44 state = {
5+ -- Used to make sure updates sent to us by workers correspond to the
6+ -- current state (the video hasn't changed)
7+ id = 0 ,
58 ready = false ,
69 available = false ,
710 enabled = false ,
@@ -33,7 +36,10 @@ local Thumbnailer = {
3336}
3437
3538function Thumbnailer :clear_state ()
39+ local prev_state_id = self .state .id
40+
3641 clear_table (self .state )
42+ self .state .id = prev_state_id + 1
3743 self .state .ready = false
3844 self .state .available = false
3945 self .state .finished_thumbnails = 0
@@ -46,7 +52,11 @@ function Thumbnailer:on_file_loaded()
4652 self :clear_state ()
4753end
4854
49- function Thumbnailer :on_thumb_ready (index )
55+ function Thumbnailer :on_thumb_ready (state_id , index )
56+ if self .state .id ~= state_id then
57+ return
58+ end
59+
5060 self .state .thumbnails [index ] = 1
5161
5262 -- Full recount instead of a naive increment (let's be safe!)
@@ -58,7 +68,11 @@ function Thumbnailer:on_thumb_ready(index)
5868 end
5969end
6070
61- function Thumbnailer :on_thumb_progress (index )
71+ function Thumbnailer :on_thumb_progress (state_id , index )
72+ if self .state .id ~= state_id then
73+ return
74+ end
75+
6276 self .state .thumbnails [index ] = math.max (self .state .thumbnails [index ], 0 )
6377end
6478
@@ -259,11 +273,11 @@ end
259273function Thumbnailer :register_client ()
260274 self .worker_register_timeout = mp .get_time () + 2
261275
262- mp .register_script_message (" mpv_thumbnail_script-ready" , function (index , path )
263- self :on_thumb_ready (tonumber (index ), path )
276+ mp .register_script_message (" mpv_thumbnail_script-ready" , function (state_id , index , path )
277+ self :on_thumb_ready (tonumber (state_id ), tonumber ( index ), path )
264278 end )
265- mp .register_script_message (" mpv_thumbnail_script-progress" , function (index , path )
266- self :on_thumb_progress (tonumber (index ), path )
279+ mp .register_script_message (" mpv_thumbnail_script-progress" , function (state_id , index , path )
280+ self :on_thumb_progress (tonumber (state_id ), tonumber ( index ), path )
267281 end )
268282
269283 mp .register_script_message (" mpv_thumbnail_script-worker" , function (worker_name )
@@ -371,7 +385,7 @@ function Thumbnailer:start_worker_jobs()
371385 return
372386 end
373387
374- local worker_list = {}
388+ local worker_list = { state_id = self . state . id }
375389 for worker_name in pairs (self .workers ) do table.insert (worker_list , worker_name ) end
376390
377391 local worker_count = # worker_list
0 commit comments