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