Skip to content
Snippets Groups Projects
Commit 435ded0c authored by Nils Schneider's avatar Nils Schneider
Browse files

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.
parent b88c9f78
No related branches found
No related tags found
No related merge requests found
......@@ -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)
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
ret[entry] = val
ret[k] = val
else
io.stderr:write(val, '\n')
end
end
end
return ret
return ret
end
end
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment