Software /
code /
prosody
Comparison
util/events.lua @ 3500:a49ed9166820
util.events: Create new index on handler change instead of modifying existing one (this makes util.events fully reentrant).
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 17 Sep 2010 04:09:59 +0500 |
parent | 3499:91c55ae31ef3 |
child | 3501:90c18e0355af |
comparison
equal
deleted
inserted
replaced
3499:91c55ae31ef3 | 3500:a49ed9166820 |
---|---|
16 function new() | 16 function new() |
17 local handlers = {}; | 17 local handlers = {}; |
18 local event_map = {}; | 18 local event_map = {}; |
19 local function _rebuild_index(event) -- TODO optimize index rebuilding | 19 local function _rebuild_index(event) -- TODO optimize index rebuilding |
20 local _handlers = event_map[event]; | 20 local _handlers = event_map[event]; |
21 local index = handlers[event]; | 21 local index = {}; |
22 if index then | |
23 for i=#index,1,-1 do index[i] = nil; end | |
24 else index = {}; handlers[event] = index; end | |
25 for handler in pairs(_handlers) do | 22 for handler in pairs(_handlers) do |
26 t_insert(index, handler); | 23 t_insert(index, handler); |
27 end | 24 end |
28 t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end); | 25 t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end); |
26 handlers[event] = index; | |
29 end; | 27 end; |
30 local function add_handler(event, handler, priority) | 28 local function add_handler(event, handler, priority) |
31 local map = event_map[event]; | 29 local map = event_map[event]; |
32 if map then | 30 if map then |
33 map[handler] = priority or 0; | 31 map[handler] = priority or 0; |