diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-05-20 02:40:42 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2025-05-20 02:40:42 -0700 |
commit | 275391f4b143ea1dc99e5d043b75efcd194c95cb (patch) | |
tree | da16049a9648bcf2165e3b88b9fc6ae91962a6a1 | |
parent | 8eadbf9ec3174dd495a74cac233deb2c4a4aef89 (diff) | |
download | mpv-iptv-menu-275391f4b143ea1dc99e5d043b75efcd194c95cb.tar.gz mpv-iptv-menu-275391f4b143ea1dc99e5d043b75efcd194c95cb.tar.xz |
process later download requests first
-rw-r--r-- | iptv-menu-dl.lua | 33 | ||||
-rw-r--r-- | 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) @@ -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 |