diff options
author | Matthew Wild <mwild1@gmail.com> | 2012-01-22 19:35:50 +0000 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2012-01-22 19:35:50 +0000 |
commit | 94327cb7b23a75cbec35f8fc9a18dca3b044dd6e (patch) | |
tree | 7d20032604d0af2922b53b19bab47ca473c23049 | |
parent | 7780d6690a22585ae15d11691d15712269d37f57 (diff) | |
download | prosody-94327cb7b23a75cbec35f8fc9a18dca3b044dd6e.tar.gz prosody-94327cb7b23a75cbec35f8fc9a18dca3b044dd6e.zip |
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
-rw-r--r-- | core/moduleapi.lua | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/core/moduleapi.lua b/core/moduleapi.lua index 3a28ec62..44ae9a07 100644 --- a/core/moduleapi.lua +++ b/core/moduleapi.lua @@ -100,6 +100,35 @@ function api:require(lib) return f(); end +function api:depends(name) + if not self.dependencies then + self.dependencies = {}; + self:hook("module-reloaded", function (event) + if self.dependencies[event.module] then + self:log("info", "Auto-reloading due to reload of %s:%s", event.host, event.module); + modulemanager.reload(self.host, self.name); + return; + end + end); + self:hook("module-unloaded", function (event) + if self.dependencies[event.module] then + self:log("info", "Auto-unloading due to unload of %s:%s", event.host, event.module); + modulemanager.unload(self.host, self.name); + end + end); + end + local mod = modulemanager.get_module(self.host, name) or modulemanager.get_module("*", name); + if not mod then + local err; + mod, err = modulemanager.load(self.host, name); + if not mod then + return error(("Unable to load required module, mod_%s: %s"):format(name, ((err or "unknown error"):gsub("%-", " ")) )); + end + end + self.dependencies[name] = true; + return mod; +end + function api:get_option(name, default_value) local value = config.get(self.host, self.name, name); if value == nil then |