Software /
code /
prosody
Comparison
util/events.lua @ 3498:3f52f3d9db5a
util.events: Removed dispatcher creation functions (these weren't being used).
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 17 Sep 2010 04:09:58 +0500 |
parent | 2925:692b3c6c5bd2 |
child | 3499:91c55ae31ef3 |
comparison
equal
deleted
inserted
replaced
3497:e9159b325e33 | 3498:3f52f3d9db5a |
---|---|
5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
7 -- | 7 -- |
8 | 8 |
9 | 9 |
10 local ipairs = ipairs; | |
11 local pairs = pairs; | 10 local pairs = pairs; |
12 local t_insert = table.insert; | 11 local t_insert = table.insert; |
13 local t_sort = table.sort; | 12 local t_sort = table.sort; |
14 local select = select; | |
15 | 13 |
16 module "events" | 14 module "events" |
17 | 15 |
18 function new() | 16 function new() |
19 local dispatchers = {}; | |
20 local handlers = {}; | 17 local handlers = {}; |
21 local event_map = {}; | 18 local event_map = {}; |
22 local function _rebuild_index(event) -- TODO optimize index rebuilding | 19 local function _rebuild_index(event) -- TODO optimize index rebuilding |
23 local _handlers = event_map[event]; | 20 local _handlers = event_map[event]; |
24 local index = handlers[event]; | 21 local index = handlers[event]; |
55 local function remove_handlers(handlers) | 52 local function remove_handlers(handlers) |
56 for event, handler in pairs(handlers) do | 53 for event, handler in pairs(handlers) do |
57 remove_handler(event, handler); | 54 remove_handler(event, handler); |
58 end | 55 end |
59 end; | 56 end; |
60 local function _create_dispatcher(event) -- FIXME duplicate code in fire_event | 57 local function fire_event(event, ...) |
61 local h = handlers[event]; | |
62 if not h then h = {}; handlers[event] = h; end | |
63 local dispatcher = function(...) | |
64 for i=1,#h do | |
65 local ret = h[i](...); | |
66 if ret ~= nil then return ret; end | |
67 end | |
68 end; | |
69 dispatchers[event] = dispatcher; | |
70 return dispatcher; | |
71 end; | |
72 local function get_dispatcher(event) | |
73 return dispatchers[event] or _create_dispatcher(event); | |
74 end; | |
75 local function fire_event(event, ...) -- FIXME duplicates dispatcher code | |
76 local h = handlers[event]; | 58 local h = handlers[event]; |
77 if h then | 59 if h then |
78 for i=1,#h do | 60 for i=1,#h do |
79 local ret = h[i](...); | 61 local ret = h[i](...); |
80 if ret ~= nil then return ret; end | 62 if ret ~= nil then return ret; end |
81 end | 63 end |
82 end | 64 end |
83 end; | 65 end; |
84 local function get_named_arg_dispatcher(event, ...) | |
85 local dispatcher = get_dispatcher(event); | |
86 local keys = {...}; | |
87 local data = {}; | |
88 return function(...) | |
89 for i, key in ipairs(keys) do data[key] = select(i, ...); end | |
90 dispatcher(data); | |
91 end; | |
92 end; | |
93 return { | 66 return { |
94 add_handler = add_handler; | 67 add_handler = add_handler; |
95 remove_handler = remove_handler; | 68 remove_handler = remove_handler; |
96 add_plugin = add_plugin; | 69 add_plugin = add_plugin; |
97 remove_plugin = remove_plugin; | 70 remove_plugin = remove_plugin; |
98 get_dispatcher = get_dispatcher; | |
99 fire_event = fire_event; | 71 fire_event = fire_event; |
100 get_named_arg_dispatcher = get_named_arg_dispatcher; | |
101 _dispatchers = dispatchers; | |
102 _handlers = handlers; | 72 _handlers = handlers; |
103 _event_map = event_map; | 73 _event_map = event_map; |
104 }; | 74 }; |
105 end | 75 end |
106 | 76 |