@@ -49,38 +49,52 @@ local function update(widget, reg, disablecache)
4949 local t = os.time ()
5050 local data = {}
5151
52- -- Check for chached output newer than the last update
53- if widget_cache [reg .wtype ] ~= nil then
54- local c = widget_cache [reg .wtype ]
55-
56- if (c .time == nil or c .time <= t - reg .timer ) or disablecache then
57- c .time , c .data = t , reg .wtype (reg .format , reg .warg )
52+ local function format_data (data )
53+ local ret
54+ if type (data ) == " table" then
55+ if type (reg .format ) == " string" then
56+ ret = helpers .format (reg .format , data )
57+ elseif type (reg .format ) == " function" then
58+ ret = reg .format (widget , data )
59+ end
5860 end
59-
60- data = c .data
61- else
62- data = reg .wtype and reg .wtype (reg .format , reg .warg )
61+ return ret or data
6362 end
6463
65- if type (data ) == " table" then
66- if type (reg .format ) == " string" then
67- data = helpers .format (reg .format , data )
68- elseif type (reg .format ) == " function" then
69- data = reg .format (widget , data )
64+ local function update_value (data , t , cache )
65+ if widget .add_value ~= nil then
66+ widget :add_value (tonumber (data ) and tonumber (data )/ 100 )
67+ elseif widget .set_value ~= nil then
68+ widget :set_value (tonumber (data ) and tonumber (data )/ 100 )
69+ elseif widget .set_markup ~= nil then
70+ widget :set_markup (data )
71+ else
72+ widget .text = data
73+ end
74+ -- Update cache
75+ if t and cache then
76+ cache .time , cache .data = t , data
7077 end
7178 end
72-
73- if widget .add_value ~= nil then
74- widget :add_value (tonumber (data ) and tonumber (data )/ 100 )
75- elseif widget .set_value ~= nil then
76- widget :set_value (tonumber (data ) and tonumber (data )/ 100 )
77- elseif widget .set_markup ~= nil then
78- widget :set_markup (data )
79- else
80- widget .text = data
79+
80+ -- Check for cached output newer than the last update
81+ local c = widget_cache [reg .wtype ]
82+ if c and c .time and c .data and t < c .time + reg .timer and not disablecache then
83+ return update_value (format_data (c .data ))
84+ elseif reg .wtype then
85+ if reg .wtype .async then
86+ if not reg .lock then
87+ reg .lock = true
88+ return reg .wtype .async (reg .warg ,
89+ function (data )
90+ update_value (format_data (data ), t , c )
91+ reg .lock = false
92+ end )
93+ end
94+ else
95+ return update_value (format_data (reg .wtype (nil , reg .warg )), t , c )
96+ end
8197 end
82-
83- return data
8498end
8599-- }}}
86100
@@ -147,6 +161,7 @@ function vicious.register(widget, wtype, format, timer, warg)
147161 local reg = {
148162 -- Set properties
149163 wtype = wtype ,
164+ lock = false ,
150165 format = format ,
151166 timer = timer ,
152167 warg = warg ,
0 commit comments