From 275391f4b143ea1dc99e5d043b75efcd194c95cb Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 20 May 2025 02:40:42 -0700 Subject: process later download requests first --- iptv-menu-dl.lua | 33 ++++++++++++++++++++++++++++++++- main.lua | 2 +- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/iptv-menu-dl.lua b/iptv-menu-dl.lua index 9ff4650..a207c91 100644 --- a/iptv-menu-dl.lua +++ b/iptv-menu-dl.lua @@ -2,7 +2,9 @@ local utils = require('mp.utils') -mp.register_script_message('download-image', function(target, name, url, path) +local queue = {} + +function download(target, name, url, path) if utils.file_info(path) then return end @@ -14,4 +16,33 @@ mp.register_script_message('download-image', function(target, name, url, path) if ret == 0 then mp.commandv('script-message-to', target, name, url, path) end +end + +function process_next() + download(unpack(table.remove(queue))) + + if #queue > 0 then + mp.add_timeout(0, process_next) + end +end + +mp.register_script_message('download-file', function(...) + queue[#queue+1] = {...} + + -- we want the ability for a later request to be downloaded before + -- earlier ones, e.g., to prioritise the image for the current menu. + -- + -- using a small timeout between receiving a message and processing it + -- allows for multiple pending messages to be received before any are + -- processed. however, immediately scheduling a timer for each request + -- does not work, as all elapsed timeouts are executed before any new + -- messages are received. + -- + -- the workaround is to schedule a timeout only when adding to an empty + -- queue and have each callback schedule an additional timeout if the + -- queue is not empty after completion. this effectively results in new + -- messages being received after each download. + if #queue == 1 then + mp.add_timeout(0, process_next) + end end) diff --git a/main.lua b/main.lua index 396e300..d50f5f3 100644 --- a/main.lua +++ b/main.lua @@ -139,7 +139,7 @@ local function get_image_path(url, dl) if dl then mp.commandv('script-message-to', - 'iptv_menu_dl', 'download-image', + 'iptv_menu_dl', 'download-file', script_name, 'downloaded-image', -- callback url, path) return path -- cgit v1.2.3-70-g09d2