Comparison

core/moduleapi.lua @ 9149:d03f21729b2c

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.
author Matthew Wild <mwild1@gmail.com>
date Wed, 08 Aug 2018 08:19:01 +0100
parent 9042:734ba7080b35
child 9509:b57353f76c83
comparison
equal deleted inserted replaced
9148:a474c94d0b0a 9149:d03f21729b2c
162 end 162 end
163 self.dependencies[name] = true; 163 self.dependencies[name] = true;
164 return mod; 164 return mod;
165 end 165 end
166 166
167 -- Returns one or more shared tables at the specified virtual paths 167 local function get_shared_table_from_path(module, tables, path)
168 -- Intentionally does not allow the table at a path to be _set_, it 168 if path:sub(1,1) ~= "/" then -- Prepend default components
169 local default_path_components = { module.host, module.name };
170 local n_components = select(2, path:gsub("/", "%1"));
171 path = (n_components<#default_path_components and "/" or "")
172 ..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
173 end
174 local shared = tables[path];
175 if not shared then
176 shared = {};
177 if path:match("%-cache$") then
178 setmetatable(shared, { __mode = "kv" });
179 end
180 tables[path] = shared;
181 end
182 return shared;
183 end
184
185 -- Returns a shared table at the specified virtual path
186 -- Intentionally does not allow the table to be _set_, it
169 -- is auto-created if it does not exist. 187 -- is auto-created if it does not exist.
170 function api:shared(...) 188 function api:shared(path)
171 if not self.shared_data then self.shared_data = {}; end 189 if not self.shared_data then self.shared_data = {}; end
172 local paths = { n = select("#", ...), ... }; 190 local shared = get_shared_table_from_path(self, shared_data, path);
173 local data_array = {}; 191 self.shared_data[path] = shared;
174 local default_path_components = { self.host, self.name }; 192 return shared;
175 for i = 1, paths.n do
176 local path = paths[i];
177 if path:sub(1,1) ~= "/" then -- Prepend default components
178 local n_components = select(2, path:gsub("/", "%1"));
179 path = (n_components<#default_path_components and "/" or "")
180 ..t_concat(default_path_components, "/", 1, #default_path_components-n_components).."/"..path;
181 end
182 local shared = shared_data[path];
183 if not shared then
184 shared = {};
185 if path:match("%-cache$") then
186 setmetatable(shared, { __mode = "kv" });
187 end
188 shared_data[path] = shared;
189 end
190 t_insert(data_array, shared);
191 self.shared_data[path] = shared;
192 end
193 return unpack(data_array);
194 end 193 end
195 194
196 function api:get_option(name, default_value) 195 function api:get_option(name, default_value)
197 local config = require "core.configmanager"; 196 local config = require "core.configmanager";
198 local value = config.get(self.host, name); 197 local value = config.get(self.host, name);