پرش به محتوا

پودمان:load

از ویکی‌واژه

توضیحات این پودمان می‌تواند در پودمان:load/توضیحات قرار گیرد.

local export = {}

local load_data = mw.loadData
local loaded = package.loaded
local main_loader = package.loaders[2]
local require = require
local setmetatable = setmetatable

local loaders, loaded_data, mt

local function get_mt()
	mt, get_mt = {__mode = "kv"}, nil
	return mt
end

-- main_loader returns a loader function if the module exists, or nil if it doesn't.
local function get_loader(modname)
	if loaders == nil then
		local loader = main_loader(modname)
		loaders = setmetatable({[modname] = loader or false}, mt or get_mt())
		return loader
	end
	local loader = loaders[modname]
	if loader == nil then
		loader = main_loader(modname)
		loaders[modname] = loader or false
	end
	return loader or nil
end

local function get_data(modname, loader)
	if loaded_data == nil then
		local data = load_data(modname)
		loaded_data = setmetatable({[loader] = data}, mt or get_mt())
		return data
	end
	local data = loaded_data[loader]
	if data == nil then
		data = load_data(modname)
		loaded_data[loader] = data
	end
	return data
end

--[==[
Like `require`, but returns `nil` if a module does not exist, instead of throwing an error.

Outputs are cached, which is faster for all modules, but much faster for nonexistent modules, since `require` will attempt to use the full loader each time because loading failures don't get cached.]==]
function export.safe_require(modname)
	local mod = loaded[modname]
	if mod == nil and get_loader(modname) then
		-- Call with require instead of the loader directly, as it protects against infinite loading loops.
		return require(modname)
	end
	return mod
end

--[==[
Like `mw.loadData`, but it does not generate a new table each time the same module is loaded via this function. Instead, it caches the returned data so that the same table can be returned each time, which is more efficient.]==]
function export.load_data(modname)
	return get_data(modname, get_loader(modname))
end

--[==[
Like `export.load_data` (itself a variant of `mw.loadData`), but returns `nil` if a module does not exist, instead of throwing an error.

Outputs are cached, which is faster for all modules, but much faster for nonexistent modules, since `mw.loadData` will attempt to use the full loader each time because loading failures don't get cached.]==]
function export.safe_load_data(modname)
	local loader = get_loader(modname)
	if loader == nil then
		return nil
	end
	return get_data(modname, loader)
end

return export