မဝ်ဂျူ:utilities/require when needed
မံက်ပြာကတ်
A helper function which can be used in place of require
if it's not certain that the module in question will be needed. If the module has already been loaded, then it is simply returned; if it hasn't, then it won't be loaded until it is first used.
This is generally useful when defining variables at the start of a module.
local loaded = package.loaded
local require = require
local setmetatable = setmetatable
return function(text, key)
local module = loaded[text]
if module then
return key and module[key] or module
end
local mt = {}
function mt:__index(k)
module = module or key and require(text)[key] or require(text)
return module[k]
end
function mt:__call(...)
module = module or key and require(text)[key] or require(text)
return module(...)
end
return setmetatable({}, mt)
end