Changeset

4896:27cda15104f2

modulemanager, moduleapi: Turn module.event_handlers into a multitable and track object->event->handler associations correctly (thanks Zash)
author Matthew Wild <mwild1@gmail.com>
date Sat, 19 May 2012 15:39:16 +0100
parents 4895:36df30395c44
children 4897:1a90e5225b22
files core/moduleapi.lua core/modulemanager.lua
diffstat 2 files changed, 8 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/core/moduleapi.lua	Sat May 19 15:35:49 2012 +0100
+++ b/core/moduleapi.lua	Sat May 19 15:39:16 2012 +0100
@@ -70,12 +70,7 @@
 end
 
 function api:hook_object_event(object, event, handler, priority)
-	local handlers = self.event_handlers[event];
-	if not handlers then
-		handlers = {};
-		self.event_handlers[event] = handlers;
-	end
-	handlers[event] = { handler = handler, priority = priority, object = object };
+	self.event_handlers:set(object, event, handler, true);
 	return object.add_handler(event, handler, priority);
 end
 
--- a/core/modulemanager.lua	Sat May 19 15:35:49 2012 +0100
+++ b/core/modulemanager.lua	Sat May 19 15:39:16 2012 +0100
@@ -10,6 +10,9 @@
 local log = logger.init("modulemanager");
 local config = require "core.configmanager";
 local pluginloader = require "util.pluginloader";
+local set = require "util.set";
+
+local new_multitable = require "util.multitable".new;
 
 local hosts = hosts;
 local prosody = prosody;
@@ -26,8 +29,6 @@
 	return xpcall(function() return f(unpack(params, 1, n)) end, function(e) return tostring(e).."\n"..debug_traceback(); end);
 end
 
-local set = require "util.set";
-
 local autoload_modules = {"presence", "message", "iq", "offline", "c2s", "s2s"};
 local component_inheritable_modules = {"tls", "dialback", "iq", "s2s"};
 
@@ -91,8 +92,8 @@
 		end
 	end
 	
-	for event, data in pairs(mod.module.event_handlers) do
-		data.object.remove_handler(event, data.handler);
+	for object, event, handler in mod.module.event_handlers:iter(nil, nil, nil) do
+		object.remove_handler(event, handler);
 	end
 	
 	if mod.module.items then -- remove items
@@ -132,7 +133,7 @@
 		if module_has_method(mod, "add_host") then
 			local _log = logger.init(host..":"..module_name);
 			local host_module_api = setmetatable({
-				host = host, event_handlers = {}, items = {};
+				host = host, event_handlers = new_multitable(), items = {};
 				_log = _log, log = function (self, ...) return _log(...); end;
 			},{
 				__index = modulemap["*"][module_name].module;
@@ -159,7 +160,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, event_handlers = {} }
+		_log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable() }
 		, { __index = api });
 
 	local pluginenv = setmetatable({ module = api_instance }, { __index = _G });