util/events.lua

Mon, 04 Jan 2016 17:03:19 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 04 Jan 2016 17:03:19 +0000
changeset 18
8050134f35b3
parent 0
d363a6692a10
permissions
-rw-r--r--

clients: Include request in the new-client event

0
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 -- Prosody IM
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local pairs = pairs;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local t_insert = table.insert;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local t_sort = table.sort;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 local setmetatable = setmetatable;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 local next = next;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 module "events"
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 function new()
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 local handlers = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local event_map = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local function _rebuild_index(handlers, event)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local _handlers = event_map[event];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if not _handlers or next(_handlers) == nil then return; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 local index = {};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 for handler in pairs(_handlers) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 t_insert(index, handler);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 t_sort(index, function(a, b) return _handlers[a] > _handlers[b]; end);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 handlers[event] = index;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 return index;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 setmetatable(handlers, { __index = _rebuild_index });
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local function add_handler(event, handler, priority)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 local map = event_map[event];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 if map then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 map[handler] = priority or 0;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 else
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 map = {[handler] = priority or 0};
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 event_map[event] = map;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 handlers[event] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 local function remove_handler(event, handler)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 local map = event_map[event];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 if map then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 map[handler] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 handlers[event] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 if next(map) == nil then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 event_map[event] = nil;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 local function add_handlers(handlers)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 for event, handler in pairs(handlers) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 add_handler(event, handler);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 local function remove_handlers(handlers)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 for event, handler in pairs(handlers) do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 remove_handler(event, handler);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 local function fire_event(event_name, event_data)
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 local h = handlers[event_name];
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 if h then
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 for i=1,#h do
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 local ret = h[i](event_data);
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 if ret ~= nil then return ret; end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71 end;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 return {
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 add_handler = add_handler;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74 remove_handler = remove_handler;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 add_handlers = add_handlers;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 remove_handlers = remove_handlers;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 fire_event = fire_event;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 _handlers = handlers;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 _event_map = event_map;
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 };
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 end
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
d363a6692a10 Initial commit. Tortoises are fun.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 return _M;

mercurial