diff options
author | Waqas Hussain <waqas20@gmail.com> | 2010-09-19 17:51:00 +0500 |
---|---|---|
committer | Waqas Hussain <waqas20@gmail.com> | 2010-09-19 17:51:00 +0500 |
commit | 152638d87b618276a83516271aac3f3471fb897a (patch) | |
tree | e75d6577547c07d7fed7e38c5b42b4af2ef9e7b2 /util/events.lua | |
parent | 191c3849e27a658df2d21efe581d22a9db6623f6 (diff) | |
download | prosody-152638d87b618276a83516271aac3f3471fb897a.tar.gz prosody-152638d87b618276a83516271aac3f3471fb897a.zip |
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Diffstat (limited to 'util/events.lua')
-rw-r--r-- | util/events.lua | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/util/events.lua b/util/events.lua index 81bea639..8e05072d 100644 --- a/util/events.lua +++ b/util/events.lua @@ -10,21 +10,26 @@ local pairs = pairs; local t_insert = table.insert; local t_sort = table.sort; +local setmetatable = setmetatable; +local next = next; module "events" function new() local handlers = {}; local event_map = {}; - local function _rebuild_index(event) -- TODO optimize index rebuilding + local function _rebuild_index(handlers, event) local _handlers = event_map[event]; + if not _handlers or next(_handlers) == nil then return; end local index = {}; for handler in pairs(_handlers) do t_insert(index, handler); end t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end); handlers[event] = index; + return index; end; + setmetatable(handlers, { __index = _rebuild_index }); local function add_handler(event, handler, priority) local map = event_map[event]; if map then @@ -33,13 +38,13 @@ function new() map = {[handler] = priority or 0}; event_map[event] = map; end - _rebuild_index(event); + handlers[event] = nil; end; local function remove_handler(event, handler) local map = event_map[event]; if map then map[handler] = nil; - _rebuild_index(event); + handlers[event] = nil; end end; local function add_handlers(handlers) |