Anonymous
×
Create a new article
Write your page title here:
We currently have 97 articles on The Dream Shrine. Type your article name above or click on one of the titles below and start writing!



The Dream Shrine
Revision as of 09:49, 6 August 2024 by MagicPixieDreamGirl (talk | contribs) (Created page with "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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:UtilsPackage/doc

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