diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-08-08 08:19:01 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-08-08 08:19:01 +0100 |
commit | 10fe4432f4557fe4a12db3d11cf7e292fd74f34d (patch) | |
tree | 9f904c273603084d922634a4dc5ebb6c2b73ed18 /core | |
parent | a60bc20468b9c78cbf5e72ce090a755f23a4d61e (diff) | |
download | prosody-10fe4432f4557fe4a12db3d11cf7e292fd74f34d.tar.gz prosody-10fe4432f4557fe4a12db3d11cf7e292fd74f34d.zip |
moduleapi: Remove multiple-parameters feature from module:shared()
Multiple paths are rarely used, and leads to less clear code than just
calling module:shared() once per shared table. It also prevents us from
extending the API with new parameters in the future.
Diffstat (limited to 'core')
-rw-r--r-- | core/moduleapi.lua | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 764df2a2..76d9d958 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -164,33 +164,32 @@ function api:depends(name) return mod; end --- Returns one or more shared tables at the specified virtual paths --- Intentionally does not allow the table at a path to be _set_, it --- is auto-created if it does not exist. -function api:shared(...) - if not self.shared_data then self.shared_data = {}; end - local paths = { n = select("#", ...), ... }; - local data_array = {}; - local default_path_components = { self.host, self.name }; - for i = 1, paths.n do - local path = paths[i]; - if path:sub(1,1) ~= "/" then -- Prepend default components - local n_components = select(2, path:gsub("/", "%1")); - path = (n_components<#default_path_components and "/" or "") - ..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path; - end - local shared = shared_data[path]; - if not shared then - shared = {}; - if path:match("%-cache$") then - setmetatable(shared, { __mode = "kv" }); - end - shared_data[path] = shared; +local function get_shared_table_from_path(module, tables, path) + if path:sub(1,1) ~= "/" then -- Prepend default components + local default_path_components = { module.host, module.name }; + local n_components = select(2, path:gsub("/", "%1")); + path = (n_components<#default_path_components and "/" or "") + ..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path; + end + local shared = tables[path]; + if not shared then + shared = {}; + if path:match("%-cache$") then + setmetatable(shared, { __mode = "kv" }); end - t_insert(data_array, shared); - self.shared_data[path] = shared; + tables[path] = shared; end - return unpack(data_array); + return shared; +end + +-- Returns a shared table at the specified virtual path +-- Intentionally does not allow the table to be _set_, it +-- is auto-created if it does not exist. +function api:shared(path) + if not self.shared_data then self.shared_data = {}; end + local shared = get_shared_table_from_path(self, shared_data, path); + self.shared_data[path] = shared; + return shared; end function api:get_option(name, default_value) |