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

View File

@ -2,9 +2,14 @@ local announce = require 'gluon.announce'
local deflate = require 'deflate' local deflate = require 'deflate'
local json = require 'luci.jsonc' local json = require 'luci.jsonc'
local memoize = {}
local function collect(type) 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 end