Diff

util/events.lua @ 1175:edef0c10e076

util.events: handler priorities
author Waqas Hussain <waqas20@gmail.com>
date Sat, 16 May 2009 15:56:45 +0500
parent 1146:542d49518d3a
child 1180:8c5d945c1f35
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();