You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

78 lines
1.8 KiB

local Modules = {}
function Modules:new(obj)
obj = obj or {}
setmetatable(obj, self)
self.__index = self
obj:initialize()
return obj
end
function Modules:initialize()
self.modules = {}
end
function Modules:add(name, config)
local module = require('module/'..name)
self.modules[name] = { instance = module:new(), config = config }
end
function Modules:addInstance(name, instance, config)
self.modules[name] = { instance = instance, config = config }
end
function Modules:has(name)
return self.modules[name] ~= nil
end
function Modules:get(name)
if (self.modules[name] == nil) then
print("MODULE NOT LOADED: "..name)
end
return self.modules[name]['instance']
end
function Modules:configure()
for name, module in pairs(self.modules) do
module.instance:configure(module.config)
end
end
function Modules:call(method)
orders = {}
for name, module in pairs(self.modules) do
if (module.config ~= nil and module.config.order ~= nil) then
table.insert(orders, module.config.order)
end
end
table.sort(orders)
-- If the plugin modifies the module list (by, e.g., registering new modules
-- like the plugin module does) then we hit some non-deterministic behaviour
-- as far as loop ordering. So just track which modules we've already called
-- to make sure they're not called twice.
run = {}
for idx,order in ipairs(orders) do
for name, module in pairs(self.modules) do
if (module.config ~= nil and module.config.order ~= nil and module.config.order == order) then
if (run[name] == nil) then
module['instance'][method](module['instance'])
run[name] = true
end
end
end
end
for name, module in pairs(self.modules) do
if (module.config == nil or module.config.order == nil) then
if (run[name] == nil) then
module['instance'][method](module['instance'])
run[name] = true
end
end
end
end
return Modules