Module:UtilsPackage: Difference between revisions
Jump to navigation
Jump to search
PhantomCaleb (talk | contribs) No edit summary |
TriforceTony (talk | contribs) m (1 revision imported: Author request.) |
||
(7 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
local p = {} | 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) | function p.submodules(submodules, sectionHeadings) | ||
local module = {} | local module = {} | ||
Line 9: | Line 10: | ||
local submodule = require(submodulePage) | local submodule = require(submodulePage) | ||
for exportKey, export in pairs(submodule) do | for exportKey, export in pairs(submodule) do | ||
if | 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)) | mw.addWarning(string.format("Module conflict: <code>%s</code> is exported by more than one submodule.", exportKey)) | ||
end | end | ||
Line 21: | Line 20: | ||
} | } | ||
end | end | ||
module.Documentation = { sections = moduleDoc } | module.Documentation = function() return { sections = moduleDoc } end | ||
return module | 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 | end | ||
return p | return p |
Latest revision as of 01:41, 22 January 2023
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