API: regregister() and update() are not exposed

This commit is contained in:
Adrian C. (anrxc) 2010-03-07 02:39:26 +01:00
parent ff050a02a4
commit 95e9a771b9
2 changed files with 90 additions and 89 deletions

4
README
View File

@ -77,8 +77,8 @@ Restart suspended widgets:
- if widget is provided only that widget will be activated - if widget is provided only that widget will be activated
Enable caching for a widget: Enable caching of a widget type:
vicious.cache(widget_type) vicious.cache(type)
- enable caching of values returned by a widget type - enable caching of values returned by a widget type

175
init.lua
View File

@ -50,12 +50,12 @@ require("vicious.date")
module("vicious") module("vicious")
-- {{{ Initialise tables -- {{{ Initialize tables
local timers = {} local timers = {}
local registered = {} local registered = {}
local widget_cache = {} local widget_cache = {}
-- Initialise the function table -- Initialize the function table
widgets = {} widgets = {}
-- }}} -- }}}
@ -72,39 +72,60 @@ for i, w in pairs(_M) do
end end
-- }}} -- }}}
-- {{{ Main functions -- {{{ Local functions
-- {{{ Register a widget -- {{{ Update a widget
function register(widget, wtype, format, timer, warg) local function update(widget, reg, disablecache)
local reg = {} -- Check if there are any equal widgets
local widget = widget if reg == nil then
for w, i in pairs(registered) do
if w == widget then
for _, r in pairs(i) do
update(w, r, disablecache)
end
end
end
-- Set properties return
reg.type = wtype
reg.format = format
reg.timer = timer
reg.warg = warg
reg.widget = widget
-- Update function
reg.update = function ()
update(widget, reg)
end end
-- Default to 2s timer local t = os.time()
if reg.timer == nil then local data = {}
reg.timer = 2
-- Check for chached output newer than the last update
if widget_cache[reg.type] ~= nil then
local c = widget_cache[reg.type]
if (c.time == nil or c.time <= t-reg.timer) or disablecache then
c.time, c.data = t, reg.type(reg.format, reg.warg)
end
data = c.data
else
data = reg.type(reg.format, reg.warg)
end end
-- Register a reg object if type(data) == "table" then
regregister(reg) if type(reg.format) == "string" then
data = helpers.format(reg.format, data)
elseif type(reg.format) == "function" then
data = reg.format(widget, data)
end
end
-- Return a reg object for reuse if widget.add_value ~= nil then
return reg widget:add_value(tonumber(data) / 100)
elseif widget.set_value ~= nil then
widget:set_value(tonumber(data) / 100)
else
widget.text = data
end
return data
end end
-- }}} -- }}}
-- {{{ Register from reg object -- {{{ Register from reg object
function regregister(reg) local function regregister(reg)
if not reg.running then if not reg.running then
if registered[reg.widget] == nil then if registered[reg.widget] == nil then
registered[reg.widget] = {} registered[reg.widget] = {}
@ -147,6 +168,38 @@ function regregister(reg)
end end
end end
-- }}} -- }}}
-- }}}
-- {{{ Exposed functions
-- {{{ Register a widget
function register(widget, wtype, format, timer, warg)
local reg = {}
local widget = widget
-- Set properties
reg.type = wtype
reg.format = format
reg.timer = timer
reg.warg = warg
reg.widget = widget
-- Update function
reg.update = function ()
update(widget, reg)
end
-- Default to 2s timer
if reg.timer == nil then
reg.timer = 2
end
-- Register a reg object
regregister(reg)
-- Return a reg object for reuse
return reg
end
-- }}}
-- {{{ Unregister a widget -- {{{ Unregister a widget
function unregister(widget, keep, reg) function unregister(widget, keep, reg)
@ -184,7 +237,15 @@ function unregister(widget, keep, reg)
end end
-- }}} -- }}}
-- {{{ Suspend vicious -- {{{ Enable caching of a widget type
function cache(type)
if widget_cache[type] == nil then
widget_cache[type] = {}
end
end
-- }}}
-- {{{ Suspend all widgets
function suspend() function suspend()
for w, i in pairs(registered) do for w, i in pairs(registered) do
for _, v in pairs(i) do for _, v in pairs(i) do
@ -194,7 +255,7 @@ function suspend()
end end
-- }}} -- }}}
-- {{{ Activate vicious -- {{{ Activate a widget
function activate(widget) function activate(widget)
for w, i in pairs(registered) do for w, i in pairs(registered) do
if widget == nil or w == widget then if widget == nil or w == widget then
@ -205,64 +266,4 @@ function activate(widget)
end end
end end
-- }}} -- }}}
-- {{{ Enable caching for a widget
function cache(widget)
if widget_cache[widget] == nil then
widget_cache[widget] = {}
end
end
-- }}}
-- {{{ Update a widget
function update(widget, reg, disablecache)
-- Check if there are any equal widgets
if reg == nil then
for w, i in pairs(registered) do
if w == widget then
for _, v in pairs(i) do
update(w, v, disablecache)
end
end
end
return
end
local t = os.time()
local data = {}
-- Do we have output chached for a widget newer than last update
if widget_cache[reg.type] ~= nil then
local c = widget_cache[reg.type]
if c.time == nil or c.time <= t - reg.timer or disablecache then
c.time = t
c.data = reg.type(reg.format, reg.warg)
end
data = c.data
else
data = reg.type(reg.format, reg.warg)
end
if type(data) == "table" then
if type(reg.format) == "string" then
data = helpers.format(reg.format, data)
elseif type(reg.format) == "function" then
data = reg.format(widget, data)
end
end
if widget.add_value ~= nil then
widget:add_value(tonumber(data) / 100)
elseif widget.set_value ~= nil then
widget:set_value(tonumber(data) / 100)
else
widget.text = data
end
return data
end
-- }}}
-- }}} -- }}}