gluon-announce: build a tree of functions first

collect_dir() will now pre-load all files and return a function that will
collect all information.
This commit is contained in:
Nils Schneider 2015-08-31 19:56:21 +02:00
parent b88c9f7877
commit 435ded0c60
2 changed files with 26 additions and 9 deletions

View File

@ -10,24 +10,36 @@ local function collect_entry(entry)
if fs.stat(entry, 'type') == 'dir' then
return collect_dir(entry)
else
return setfenv(loadfile(entry), _M)()
return loadfile(entry)
end
end
function collect_dir(dir)
local ret = { [{}] = true }
local fns = {}
for entry in fs.dir(dir) do
if entry:sub(1, 1) ~= '.' then
local ok, val = pcall(collect_entry, dir .. '/' .. entry)
if ok then
ret[entry] = val
local fn, err = collect_entry(dir .. '/' .. entry)
if fn then
fns[entry] = fn
else
io.stderr:write(val, '\n')
io.stderr:write(err, '\n')
end
end
end
return ret
end
return function ()
local ret = { [{}] = true }
for k, v in pairs(fns) do
local ok, val = pcall(setfenv(v, _M))
if ok then
ret[k] = val
else
io.stderr:write(val, '\n')
end
end
return ret
end
end

View File

@ -2,9 +2,14 @@ local announce = require 'gluon.announce'
local deflate = require 'deflate'
local json = require 'luci.jsonc'
local memoize = {}
local function collect(type)
return announce.collect_dir('/lib/gluon/announce/' .. type .. '.d')
if not memoize[type] then
memoize[type] = announce.collect_dir('/lib/gluon/announce/' .. type .. '.d')
end
return memoize[type]()
end