Mercurial > prosody-hg
comparison util/events.lua @ 1175:edef0c10e076
util.events: handler priorities
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Sat, 16 May 2009 15:56:45 +0500 |
| parents | 542d49518d3a |
| children | 8c5d945c1f35 |
comparison
equal
deleted
inserted
replaced
| 1174:f7b6d5839092 | 1175:edef0c10e076 |
|---|---|
| 1 | 1 |
| 2 local ipairs = ipairs; | 2 local ipairs = ipairs; |
| 3 local pairs = pairs; | 3 local pairs = pairs; |
| 4 local t_insert = table.insert; | 4 local t_insert = table.insert; |
| 5 local t_sort = table.sort; | |
| 5 local select = select; | 6 local select = select; |
| 6 | 7 |
| 7 module "events" | 8 module "events" |
| 8 | 9 |
| 9 function new() | 10 function new() |
| 17 for i=#index,1,-1 do index[i] = nil; end | 18 for i=#index,1,-1 do index[i] = nil; end |
| 18 else index = {}; handlers[event] = index; end | 19 else index = {}; handlers[event] = index; end |
| 19 for handler in pairs(_handlers) do | 20 for handler in pairs(_handlers) do |
| 20 t_insert(index, handler); | 21 t_insert(index, handler); |
| 21 end | 22 end |
| 23 t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end); | |
| 22 end | 24 end |
| 23 end; | 25 end; |
| 24 local function add_handler(event, handler) | 26 local function add_handler(event, handler, priority) |
| 25 local map = event_map[event]; | 27 local map = event_map[event]; |
| 26 if map then | 28 if map then |
| 27 map[handler] = true; | 29 map[handler] = priority or 0; |
| 28 else | 30 else |
| 29 map = {[handler] = true}; | 31 map = {[handler] = priority or 0}; |
| 30 event_map[event] = map; | 32 event_map[event] = map; |
| 31 end | 33 end |
| 32 _rebuild_index(); | 34 _rebuild_index(); |
| 33 end; | 35 end; |
| 34 local function remove_handler(event, handler) | 36 local function remove_handler(event, handler) |
