Changeset

1175:edef0c10e076

util.events: handler priorities
author Waqas Hussain <waqas20@gmail.com>
date Sat, 16 May 2009 15:56:45 +0500
parents 1174:f7b6d5839092
children 1176:2be14b7021b2
files util/events.lua
diffstat 1 files changed, 5 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/util/events.lua	Sat May 16 05:28:04 2009 +0500
+++ b/util/events.lua	Sat May 16 15:56:45 2009 +0500
@@ -2,6 +2,7 @@
 local ipairs = ipairs;
 local pairs = pairs;
 local t_insert = table.insert;
+local t_sort = table.sort;
 local select = select;
 
 module "events"
@@ -19,14 +20,15 @@
 			for handler in pairs(_handlers) do
 				t_insert(index, handler);
 			end
+			t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end);
 		end
 	end;
-	local function add_handler(event, handler)
+	local function add_handler(event, handler, priority)
 		local map = event_map[event];
 		if map then
-			map[handler] = true;
+			map[handler] = priority or 0;
 		else
-			map = {[handler] = true};
+			map = {[handler] = priority or 0};
 			event_map[event] = map;
 		end
 		_rebuild_index();