Annotate

util/events.lua @ 1174:f7b6d5839092

modulemanager: require util.stanza
author Waqas Hussain <waqas20@gmail.com>
date Sat, 16 May 2009 05:28:04 +0500
parent 1146:542d49518d3a
child 1175:edef0c10e076
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
1146
542d49518d3a util.events: event handlers can now return a result, which also interrupts further handling of the event
Waqas Hussain <waqas20@gmail.com>
parents: 936
diff changeset
56 local ret = handler(data);
542d49518d3a util.events: event handlers can now return a result, which also interrupts further handling of the event
Waqas Hussain <waqas20@gmail.com>
parents: 936
diff changeset
57 if ret ~= nil then return ret; end
936
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 end;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 dispatchers[event] = dispatcher;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 return dispatcher;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62 end;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 local function get_dispatcher(event)
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 return dispatchers[event] or _create_dispatcher(event);
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 end;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66 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
67 local h = handlers[event];
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 if h then
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 for _, handler in ipairs(h) do
1146
542d49518d3a util.events: event handlers can now return a result, which also interrupts further handling of the event
Waqas Hussain <waqas20@gmail.com>
parents: 936
diff changeset
70 local ret = handler(data);
542d49518d3a util.events: event handlers can now return a result, which also interrupts further handling of the event
Waqas Hussain <waqas20@gmail.com>
parents: 936
diff changeset
71 if ret ~= nil then return ret; end
936
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 end
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 end;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 local function get_named_arg_dispatcher(event, ...)
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
76 local dispatcher = get_dispatcher(event);
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77 local keys = {...};
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 local data = {};
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 return function(...)
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 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
81 dispatcher(data);
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 end;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 end;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 return {
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85 add_handler = add_handler;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
86 remove_handler = remove_handler;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
87 add_plugin = add_plugin;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
88 remove_plugin = remove_plugin;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 get_dispatcher = get_dispatcher;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 fire_event = fire_event;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
91 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
92 _dispatchers = dispatchers;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
93 _handlers = handlers;
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
94 _event_map = event_map;
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 end
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
97
5663db788fdf Added: util/events.lua: An event handling library
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
98 return _M;