Changeset

4534:7a0a31c4f6c5

modulemanager, moduleapi: Replace hooks multitable with an event_handlers map stored in individual modules. Also adds module:hook_object_event() to hook events on any util.events compatible object.
author Matthew Wild <mwild1@gmail.com>
date Sun, 22 Jan 2012 18:49:11 +0000
parents 4533:c6480d17be1e
children 4535:d46e9ad4fe8a
files core/moduleapi.lua core/modulemanager.lua
diffstat 2 files changed, 12 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/core/moduleapi.lua	Sun Jan 22 18:47:33 2012 +0000
+++ b/core/moduleapi.lua	Sun Jan 22 18:49:11 2012 +0000
@@ -66,14 +66,17 @@
 	return (hosts[self.host] or prosody).events.fire_event(...);
 end
 
+function api:hook_object_event(object, event, handler, priority)
+	self.event_handlers[handler] = { name = event, priority = priority, object = object };
+	return object.add_handler(event, handler, priority);
+end
+
 function api:hook(event, handler, priority)
-	hooks:set(self.host, self.name, event, handler, true);
-	(hosts[self.host] or prosody).events.add_handler(event, handler, priority);
+	return self:hook_object_event((hosts[self.host] or prosody).events, event, handler, priority);
 end
 
 function api:hook_global(event, handler, priority)
-	hooks:set("*", self.name, event, handler, true);
-	prosody.events.add_handler(event, handler, priority);
+	return self:hook_object_event(prosody.events, event, handler, priority);
 end
 
 function api:hook_stanza(xmlns, name, handler, priority)
--- a/core/modulemanager.lua	Sun Jan 22 18:47:33 2012 +0000
+++ b/core/modulemanager.lua	Sun Jan 22 18:49:11 2012 +0000
@@ -48,10 +48,6 @@
 
 local modulemap = { ["*"] = {} };
 
-local modulehelpers = setmetatable({}, { __index = _G });
-
-local hooks = multitable_new();
-
 local NULL = {};
 
 -- Load modules when a host is activated
@@ -100,19 +96,11 @@
 			log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err);
 		end
 	end
-	-- unhook event handlers hooked by module:hook
-	for event, handlers in pairs(hooks:get(host, name) or NULL) do
-		for handler in pairs(handlers or NULL) do
-			(hosts[host] or prosody).events.remove_handler(event, handler);
-		end
+	
+	for handler, event in pairs(mod.module.event_handlers) do
+		event.object.remove_handler(event.name, handler);
 	end
-	-- unhook event handlers hooked by module:hook_global
-	for event, handlers in pairs(hooks:get("*", name) or NULL) do
-		for handler in pairs(handlers or NULL) do
-			prosody.events.remove_handler(event, handler);
-		end
-	end
-	hooks:remove(host, name);
+	
 	if mod.module.items then -- remove items
 		for key,t in pairs(mod.module.items) do
 			for i = #t,1,-1 do
@@ -153,7 +141,7 @@
 
 	local _log = logger.init(host..":"..module_name);
 	local api_instance = setmetatable({ name = module_name, host = host, path = err,
-		_log = _log, log = function (self, ...) return _log(...); end }
+		_log = _log, log = function (self, ...) return _log(...); end, event_handlers = {} }
 		, { __index = api });
 
 	local pluginenv = setmetatable({ module = api_instance }, { __index = _G });