diff options
author | Kim Alvefur <zash@zash.se> | 2015-05-03 18:39:27 +0200 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2015-05-03 18:39:27 +0200 |
commit | 009cde88a5518e95481126c547bf0b79f95ac797 (patch) | |
tree | 1db58cd52f622c3560457cf589cdc8e6326d28e6 /util | |
parent | 86867275492107937cea9b5887de077679bc751d (diff) | |
parent | a7606f77d99c798faa7067331e5b9ac0272c5ad1 (diff) | |
download | prosody-009cde88a5518e95481126c547bf0b79f95ac797.tar.gz prosody-009cde88a5518e95481126c547bf0b79f95ac797.zip |
Merge 0.10->trunk
Diffstat (limited to 'util')
-rw-r--r-- | util/datamanager.lua | 2 | ||||
-rw-r--r-- | util/events.lua | 68 |
2 files changed, 68 insertions, 2 deletions
diff --git a/util/datamanager.lua b/util/datamanager.lua index b82349f1..b4138638 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -293,7 +293,7 @@ function users(host, store, typ) local mode, err = lfs.attributes(store_dir, "mode"); if not mode then - return function() log("debug", err or (store_dir .. " does not exist")) end + return function() log("debug", "%s", err or (store_dir .. " does not exist")) end end local next, state = lfs.dir(store_dir); return function(state) diff --git a/util/events.lua b/util/events.lua index 40ca3913..79de1151 100644 --- a/util/events.lua +++ b/util/events.lua @@ -17,6 +17,8 @@ module "events" function new() local handlers = {}; + local global_wrappers; + local wrappers = {}; local event_map = {}; local function _rebuild_index(handlers, event) local _handlers = event_map[event]; @@ -60,7 +62,7 @@ function new() remove_handler(event, handler); end end; - local function fire_event(event_name, event_data) + local function _fire_event(event_name, event_data) local h = handlers[event_name]; if h then for i=1,#h do @@ -69,11 +71,75 @@ function new() end end end; + local function fire_event(event_name, event_data) + local w = wrappers[event_name] or global_wrappers; + if w then + local curr_wrapper = #w; + local function c(event_name, event_data) + curr_wrapper = curr_wrapper - 1; + if curr_wrapper == 0 then + if global_wrappers == nil or w == global_wrappers then + return _fire_event(event_name, event_data); + end + w, curr_wrapper = global_wrappers, #global_wrappers; + return w[curr_wrapper](c, event_name, event_data); + else + return w[curr_wrapper](c, event_name, event_data); + end + end + return w[curr_wrapper](c, event_name, event_data); + end + return _fire_event(event_name, event_data); + end + local function add_wrapper(event_name, wrapper) + local w; + if event_name == false then + w = global_wrappers; + if not w then + w = {}; + global_wrappers = w; + end + else + w = wrappers[event_name]; + if not w then + w = {}; + wrappers[event_name] = w; + end + end + w[#w+1] = wrapper; + end + local function remove_wrapper(event_name, wrapper) + local w; + if event_name == false then + w = global_wrappers; + else + w = wrappers[event_name]; + end + if not w then return; end + for i = #w, 1 do + if w[i] == wrapper then + table.remove(w, i); + end + end + if #w == 0 then + if event_name == nil then + global_wrappers = nil; + else + wrappers[event_name] = nil; + end + end + end return { add_handler = add_handler; remove_handler = remove_handler; add_handlers = add_handlers; remove_handlers = remove_handlers; + wrappers = { + add_handler = add_wrapper; + remove_handler = remove_wrapper; + }; + add_wrapper = add_wrapper; + remove_wrapper = remove_wrapper; fire_event = fire_event; _handlers = handlers; _event_map = event_map; |