aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2012-01-22 19:35:50 +0000
committerMatthew Wild <mwild1@gmail.com>2012-01-22 19:35:50 +0000
commit2f397255c26a57c36102ece3883c3765870c0bf9 (patch)
tree7d20032604d0af2922b53b19bab47ca473c23049 /core
parent11bf5edc1d5395822ab2dfe1edc84af85bc6dc57 (diff)
downloadprosody-2f397255c26a57c36102ece3883c3765870c0bf9.tar.gz
prosody-2f397255c26a57c36102ece3883c3765870c0bf9.zip
moduleapi: Add module:depends(), a way to safely depend upon another module at runtime
Diffstat (limited to 'core')
-rw-r--r--core/moduleapi.lua29
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