Changeset

1180:8c5d945c1f35

util.events: Much more efficient index building
author Waqas Hussain <waqas20@gmail.com>
date Sun, 17 May 2009 02:06:35 +0500
parents 1179:503d68e7e18a
children 1181:dffbb7d1da4b
files util/events.lua
diffstat 1 files changed, 11 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/util/events.lua	Sat May 16 20:48:07 2009 +0100
+++ b/util/events.lua	Sun May 17 02:06:35 2009 +0500
@@ -11,17 +11,16 @@
 	local dispatchers = {};
 	local handlers = {};
 	local event_map = {};
-	local function _rebuild_index() -- TODO optimize index rebuilding
-		for event, _handlers in pairs(event_map) do
-			local index = handlers[event];
-			if index then
-				for i=#index,1,-1 do index[i] = nil; end
-			else index = {}; handlers[event] = index; end
-			for handler in pairs(_handlers) do
-				t_insert(index, handler);
-			end
-			t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end);
+	local function _rebuild_index(event) -- TODO optimize index rebuilding
+		local _handlers = event_map[event];
+		local index = handlers[event];
+		if index then
+			for i=#index,1,-1 do index[i] = nil; end
+		else index = {}; handlers[event] = index; end
+		for handler in pairs(_handlers) do
+			t_insert(index, handler);
 		end
+		t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end);
 	end;
 	local function add_handler(event, handler, priority)
 		local map = event_map[event];
@@ -31,13 +30,13 @@
 			map = {[handler] = priority or 0};
 			event_map[event] = map;
 		end
-		_rebuild_index();
+		_rebuild_index(event);
 	end;
 	local function remove_handler(event, handler)
 		local map = event_map[event];
 		if map then
 			map[handler] = nil;
-			_rebuild_index();
+			_rebuild_index(event);
 		end
 	end;
 	local function add_plugin(plugin)