Module:UtilsPackage: Difference between revisions
Jump to navigation
Jump to search
Fandom>PhantomCaleb No edit summary |
PhantomCaleb (talk | contribs) No edit summary |
(No difference)
|
Revision as of 06:25, 17 October 2022
local p = {}
-- Combine several submodules together and present them as one module
-- Useful when a module is too large to keep all on one page but should still be presented as a unified API to consumers
-- See [[Module:UtilsLayout]] for example
function p.submodules(submodules, sectionHeadings)
local module = {}
local moduleDoc = {}
for i, submodulePage in ipairs(submodules) do
local submodule = require(submodulePage)
for exportKey, export in pairs(submodule) do
if module[exportKey] and exportKey ~= "Documentation" and exportKey ~= "Schemas" then
mw.addWarning(string.format("Module conflict: <code>%s</code> is exported by more than one submodule.", exportKey))
end
module[exportKey] = export
end
moduleDoc[i] = {
heading = sectionHeadings and sectionHeadings[i],
section = submodulePage,
}
end
module.Documentation = function() return { sections = moduleDoc } end
return module
end
-- Load a module only when needed
-- Useful when a module is only needed for certain edge cases
-- Lazy loading prevents the module from appearing in Special:Whatlinkshere when unused
-- It may also be helpful for performance
-- @returns a function which, when called, returns the module, or nil if it doesn't exist
function p.lazyLoad(moduleName)
local module
local moduleExists = true
return function()
if not module and moduleExists then
local moduleLoaded, loadedModule = pcall(function() return require(moduleName) end)
if moduleLoaded then
module = loadedModule
else
moduleExists = false
end
end
return module
end
end
return p