Annotate

util/events.lua @ 1545:e6bd29342f65

Merge with 0.5
author Matthew Wild <mwild1@gmail.com>
date Sun, 12 Jul 2009 20:33:09 +0100
parent 1522:569d58d21612
child 2406:2c416334579c
child 2923:b7049746bd29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1522
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
1 -- Prosody IM
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
4 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
6 -- COPYING file in the source package for more information.
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
7 --
569d58d21612 Add copyright header to those files missing one
Matthew Wild <mwild1@gmail.com>
parents: 1507
diff changeset
8
1507
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
9
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
10 local ipairs = ipairs;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
11 local pairs = pairs;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
12 local t_insert = table.insert;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
13 local t_sort = table.sort;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
14 local select = select;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
15
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
16 module "events"
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
17
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
18 function new()
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
19 local dispatchers = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
20 local handlers = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
21 local event_map = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
22 local function _rebuild_index(event) -- TODO optimize index rebuilding
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
23 local _handlers = event_map[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
24 local index = handlers[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
25 if index then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
26 for i=#index,1,-1 do index[i] = nil; end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
27 else index = {}; handlers[event] = index; end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
28 for handler in pairs(_handlers) do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
29 t_insert(index, handler);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
30 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
31 t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
32 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
33 local function add_handler(event, handler, priority)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
34 local map = event_map[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
35 if map then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
36 map[handler] = priority or 0;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
37 else
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
38 map = {[handler] = priority or 0};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
39 event_map[event] = map;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
40 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
41 _rebuild_index(event);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
42 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
43 local function remove_handler(event, handler)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
44 local map = event_map[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
45 if map then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
46 map[handler] = nil;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
47 _rebuild_index(event);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
48 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
49 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
50 local function add_plugin(plugin)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
51 for event, handler in pairs(plugin) do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
52 add_handler(event, handler);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
53 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
54 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
55 local function remove_plugin(plugin)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
56 for event, handler in pairs(plugin) do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
57 remove_handler(event, handler);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
58 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
59 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
60 local function _create_dispatcher(event) -- FIXME duplicate code in fire_event
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
61 local h = handlers[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
62 if not h then h = {}; handlers[event] = h; end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
63 local dispatcher = function(...)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
64 for i=1,#h do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
65 local ret = h[i](...);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
66 if ret ~= nil then return ret; end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
67 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
68 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
69 dispatchers[event] = dispatcher;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
70 return dispatcher;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
71 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
72 local function get_dispatcher(event)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
73 return dispatchers[event] or _create_dispatcher(event);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
74 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
75 local function fire_event(event, ...) -- FIXME duplicates dispatcher code
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
76 local h = handlers[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
77 if h then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
78 for i=1,#h do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
79 local ret = h[i](...);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
80 if ret ~= nil then return ret; end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
81 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
82 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
83 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
84 local function get_named_arg_dispatcher(event, ...)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
85 local dispatcher = get_dispatcher(event);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
86 local keys = {...};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
87 local data = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
88 return function(...)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
89 for i, key in ipairs(keys) do data[key] = select(i, ...); end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
90 dispatcher(data);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
91 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
92 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
93 return {
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
94 add_handler = add_handler;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
95 remove_handler = remove_handler;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
96 add_plugin = add_plugin;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
97 remove_plugin = remove_plugin;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
98 get_dispatcher = get_dispatcher;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
99 fire_event = fire_event;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
100 get_named_arg_dispatcher = get_named_arg_dispatcher;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
101 _dispatchers = dispatchers;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
102 _handlers = handlers;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
103 _event_map = event_map;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
104 };
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
105 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
106
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
107 return _M;