Software /
code /
prosody
Annotate
util/events.lua @ 1141:9d9570516ce8
mod_muc: commented connection replace detection code because google keeps resendng directed presence
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 08 May 2009 04:41:18 +0500 |
parent | 936:5663db788fdf |
child | 1146:542d49518d3a |
rev | line source |
---|---|
936
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 local ipairs = ipairs; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 local pairs = pairs; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 local t_insert = table.insert; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 local select = select; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 module "events" |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 function new() |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 local dispatchers = {}; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 local handlers = {}; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
12 local event_map = {}; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
13 local function _rebuild_index() -- TODO optimize index rebuilding |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
14 for event, _handlers in pairs(event_map) do |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
15 local index = handlers[event]; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
16 if index then |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
17 for i=#index,1,-1 do index[i] = nil; end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
18 else index = {}; handlers[event] = index; end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
19 for handler in pairs(_handlers) do |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
20 t_insert(index, handler); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local function add_handler(event, handler) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 local map = event_map[event]; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 if map then |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 map[handler] = true; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 else |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 map = {[handler] = true}; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 event_map[event] = map; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 _rebuild_index(); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 local function remove_handler(event, handler) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 local map = event_map[event]; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 if map then |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 map[handler] = nil; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 _rebuild_index(); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 local function add_plugin(plugin) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 for event, handler in pairs(plugin) do |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 add_handler(event, handler); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 local function remove_plugin(plugin) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 for event, handler in pairs(plugin) do |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 remove_handler(event, handler); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 local function _create_dispatcher(event) -- FIXME duplicate code in fire_event |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 local h = handlers[event]; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 if not h then h = {}; handlers[event] = h; end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 local dispatcher = function(data) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 for _, handler in ipairs(h) do |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 handler(data); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 dispatchers[event] = dispatcher; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 return dispatcher; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 local function get_dispatcher(event) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 return dispatchers[event] or _create_dispatcher(event); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 local function fire_event(event, data) -- FIXME duplicates dispatcher code |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 local h = handlers[event]; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 if h then |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 for _, handler in ipairs(h) do |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 handler(data); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 local function get_named_arg_dispatcher(event, ...) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 local dispatcher = get_dispatcher(event); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 local keys = {...}; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 local data = {}; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 return function(...) |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 for i, key in ipairs(keys) do data[key] = select(i, ...); end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 dispatcher(data); |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 end; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 return { |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 add_handler = add_handler; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 remove_handler = remove_handler; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 add_plugin = add_plugin; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 remove_plugin = remove_plugin; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 get_dispatcher = get_dispatcher; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 fire_event = fire_event; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 get_named_arg_dispatcher = get_named_arg_dispatcher; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 _dispatchers = dispatchers; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 _handlers = handlers; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 _event_map = event_map; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 }; |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 end |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 |
5663db788fdf
Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 return _M; |