Fix luacheck issues and add .luacheckrc (#72)

Uses `reg.timeout` for clarity (triggered via "shadowing upvalue timer
on line 13"), but keeps `reg.timer` for backward compatibility.
This commit is contained in:
Daniel Hahler 2019-01-23 10:00:38 +01:00 committed by GitHub
parent 6da3c9304a
commit 699760651b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 19 deletions

9
.luacheckrc Normal file
View File

@ -0,0 +1,9 @@
-- Only allow symbols available in all Lua versions
std = "min"
-- Global objects defined by the C code
read_globals = {
"timer", -- deprecated, but used in older versions.
}
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80

View File

@ -46,8 +46,7 @@ local function update(widget, reg, disablecache)
return return
end end
local t = os.time() local update_time = os.time()
local data = {}
local function format_data(data) local function format_data(data)
local ret local ret
@ -100,7 +99,7 @@ local function update(widget, reg, disablecache)
-- Check for cached output newer than the last update -- Check for cached output newer than the last update
local c = widget_cache[reg.wtype] local c = widget_cache[reg.wtype]
if c and t < c.time + reg.timer and not disablecache then if c and update_time < c.time + reg.timeout and not disablecache then
update_value(c.data) update_value(c.data)
elseif reg.wtype then elseif reg.wtype then
if type(reg.wtype) == "table" and reg.wtype.async then if type(reg.wtype) == "table" and reg.wtype.async then
@ -109,14 +108,14 @@ local function update(widget, reg, disablecache)
return reg.wtype.async(reg.format, return reg.wtype.async(reg.format,
reg.warg, reg.warg,
function(data) function(data)
update_cache(data, t, c) update_cache(data, update_time, c)
update_value(data) update_value(data)
reg.lock=false reg.lock=false
end) end)
end end
else else
local data = reg.wtype(reg.format, reg.warg) local data = reg.wtype(reg.format, reg.warg)
update_cache(data, t, c) update_cache(data, update_time, c)
update_value(data) update_value(data)
end end
end end
@ -153,18 +152,18 @@ local function regregister(reg)
end end
-- Start the timer -- Start the timer
if reg.timer > 0 then if reg.timeout > 0 then
local tm = timers[reg.timer] and timers[reg.timer].timer local tm = timers[reg.timeout] and timers[reg.timeout].timer
tm = tm or timer({ timeout = reg.timer }) tm = tm or timer({ timeout = reg.timeout })
if tm.connect_signal then if tm.connect_signal then
tm:connect_signal("timeout", reg.update) tm:connect_signal("timeout", reg.update)
else else
tm:add_signal("timeout", reg.update) tm:add_signal("timeout", reg.update)
end end
if not timers[reg.timer] then if not timers[reg.timeout] then
timers[reg.timer] = { timer = tm, refs = 1 } timers[reg.timeout] = { timer = tm, refs = 1 }
else else
timers[reg.timer].refs = timers[reg.timer].refs + 1 timers[reg.timeout].refs = timers[reg.timeout].refs + 1
end end
if not tm.started then if not tm.started then
tm:start() tm:start()
@ -181,16 +180,18 @@ end
-- {{{ Global functions -- {{{ Global functions
-- {{{ Register a widget -- {{{ Register a widget
function vicious.register(widget, wtype, format, timer, warg) function vicious.register(widget, wtype, format, timeout, warg)
local reg = { local reg = {
-- Set properties -- Set properties
wtype = wtype, wtype = wtype,
lock = false, lock = false,
format = format, format = format,
timer = timer or 2, timeout = timeout or 2,
warg = warg, warg = warg,
widget = widget, widget = widget,
} }
reg.timer = timeout -- For backward compatibility.
-- Set functions -- Set functions
function reg.update() function reg.update()
update(widget, reg) update(widget, reg)
@ -235,7 +236,7 @@ function vicious.unregister(widget, keep, reg)
end end
-- Disconnect from timer -- Disconnect from timer
local tm = timers[reg.timer] local tm = timers[reg.timeout]
if tm.timer.disconnect_signal then if tm.timer.disconnect_signal then
tm.timer:disconnect_signal("timeout", reg.update) tm.timer:disconnect_signal("timeout", reg.update)
else else