diff options
| author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-16 17:23:32 -0800 |
|---|---|---|
| committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2026-01-16 17:23:32 -0800 |
| commit | b3ec50d92451b99dd08b6030b08854c8cc77524b (patch) | |
| tree | af8c8adc994478de00ce4f3be8601b64a2d0dfdf /cacher.lua | |
| parent | 671e5934a9400b722210d4d243dd83c0755a4fa3 (diff) | |
| download | mpv-iptv-menu-b3ec50d92451b99dd08b6030b08854c8cc77524b.tar.gz mpv-iptv-menu-b3ec50d92451b99dd08b6030b08854c8cc77524b.tar.xz | |
track and flush OSD changes for fewer redraws
Diffstat (limited to 'cacher.lua')
| -rw-r--r-- | cacher.lua | 62 |
1 files changed, 45 insertions, 17 deletions
@@ -6,32 +6,57 @@ local mp_utils = require('mp.utils') local cacher = {} -local function wrap_func(obj, opts, func, func_name) - return function(_, ...) - local fn = table.concat({opts.prefix, func_name, ...}, '.') - local path = mp_utils.join_path(opts.directory, fn) - local f = mp_utils.file_info(path) - local data - - if not f or os.time() - f.mtime > opts.time then - data = func(obj, ...) - if data then - util.write_json_file(path, data) - end +local function exec(obj, opts, call_opts, func, func_name, ...) + local call_opts = call_opts or {} + local fn = table.concat({opts.prefix, func_name, ...}, '.') + local path = mp_utils.join_path(opts.directory, fn) + local f = mp_utils.file_info(path) + local data + local miss = not f or os.time() - f.mtime > opts.time + + if miss then + if call_opts.before_miss then + call_opts.before_miss() end - if f and not data then - print('using cached ' .. fn) - data = util.read_json_file(path) + data = func(obj, ...) + if data then + util.write_json_file(path, data) end + else + if call_opts.before_hit then + call_opts.before_hit() + end + end - return data + if f and not data then + print('using cached ' .. fn) + data = util.read_json_file(path) end + + if miss then + if call_opts.after_miss then + call_opts.after_miss() + end + else + if call_opts.after_hit then + call_opts.after_hit() + end + end + + return data end function cacher.wrap(obj, opts) local proxy = {} + function proxy:with_opts(f, ...) + local n = select('#', ...) + local args = {...} + return exec( + obj, opts, args[n], obj[f], f, unpack(args, 1, n - 1)) + end + setmetatable(proxy, { __index = function(t, k) local v = obj[k] @@ -39,7 +64,10 @@ function cacher.wrap(obj, opts) return v end - local fn = wrap_func(obj, opts, v, k) + local fn = function(_, ...) + return exec(obj, opts, nil, v, k, ...) + end + t[k] = fn return fn |
