From eb0c07e3fcb989a549d2d0c10e461c2dcb11d2ac Mon Sep 17 00:00:00 2001 From: Beniamin Kalinowski Date: Sat, 20 Jan 2018 12:33:49 +0900 Subject: [PATCH 1/3] Setting up initial values for a cache. --- init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 473e381..a909dec 100644 --- a/init.lua +++ b/init.lua @@ -79,8 +79,8 @@ local function update(widget, reg, disablecache) -- Check for cached output newer than the last update local c = widget_cache[reg.wtype] - if c and c.time and c.data and t < c.time+reg.timer and not disablecache then return update_value(format_data(c.data)) + if c and t < c.time + reg.timer and not disablecache then elseif reg.wtype then if type(reg.wtype) == "table" and reg.wtype.async then if not reg.lock then @@ -238,7 +238,7 @@ end function vicious.cache(wtype) if wtype ~= nil then if widget_cache[wtype] == nil then - widget_cache[wtype] = {} + widget_cache[wtype] = { data = nil, time = 0 } end end end From 1f6359e3065ebbbbadf38f196b2ac5a27aff3aa4 Mon Sep 17 00:00:00 2001 From: Beniamin Kalinowski Date: Sat, 20 Jan 2018 14:05:21 +0900 Subject: [PATCH 2/3] More concise default timer settings. --- init.lua | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/init.lua b/init.lua index a909dec..ecee309 100644 --- a/init.lua +++ b/init.lua @@ -158,26 +158,20 @@ end -- {{{ Global functions -- {{{ Register a widget function vicious.register(widget, wtype, format, timer, warg) - local widget = widget local reg = { -- Set properties wtype = wtype, lock = false, format = format, - timer = timer, + timer = timer or 2, warg = warg, widget = widget, } -- Set functions - reg.update = function () + function reg.update() update(widget, reg) end - -- Default to 2s timer - if reg.timer == nil then - reg.timer = 2 - end - -- Register a reg object regregister(reg) From 13a28d548505caa8a13417a1c7f22dbedc4ea300 Mon Sep 17 00:00:00 2001 From: Beniamin Kalinowski Date: Sat, 20 Jan 2018 14:16:31 +0900 Subject: [PATCH 3/3] Caching worker data instead of formatted values. For each of the wtype formatted data was being cached instead of the data returned from the worker. This fix caches the returned values. --- init.lua | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/init.lua b/init.lua index ecee309..4e32213 100644 --- a/init.lua +++ b/init.lua @@ -61,16 +61,20 @@ local function update(widget, reg, disablecache) return ret or data end - local function update_value(data, t, cache) + local function update_value(data) + local fmtd_data = format_data(data) if widget.add_value ~= nil then - widget:add_value(tonumber(data) and tonumber(data)/100) + widget:add_value(tonumber(fmtd_data) and tonumber(fmtd_data)/100) elseif widget.set_value ~= nil then - widget:set_value(tonumber(data) and tonumber(data)/100) + widget:set_value(tonumber(fmtd_data) and tonumber(fmtd_data)/100) elseif widget.set_markup ~= nil then - widget:set_markup(data) + widget:set_markup(fmtd_data) else - widget.text = data + widget.text = fmtd_data end + end + + local function update_cache(data, t, cache) -- Update cache if t and cache then cache.time, cache.data = t, data @@ -79,8 +83,8 @@ local function update(widget, reg, disablecache) -- Check for cached output newer than the last update local c = widget_cache[reg.wtype] - return update_value(format_data(c.data)) if c and t < c.time + reg.timer and not disablecache then + update_value(c.data) elseif reg.wtype then if type(reg.wtype) == "table" and reg.wtype.async then if not reg.lock then @@ -88,12 +92,15 @@ local function update(widget, reg, disablecache) return reg.wtype.async(reg.format, reg.warg, function(data) - update_value(format_data(data), t, c) + update_cache(data, t, c) + update_value(data) reg.lock=false end) end else - return update_value(format_data(reg.wtype(reg.format, reg.warg)), t, c) + local data = reg.wtype(reg.format, reg.warg) + update_cache(data, t, c) + update_value(data) end end end