Annotate

util/events.lua @ 1507:92357dffe743

util.events: Convert from Windows line endings
author Matthew Wild <mwild1@gmail.com>
date Thu, 09 Jul 2009 22:57:32 +0100
parent 1417:b02b22f77326
child 1522:569d58d21612
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1507
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
1
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
2 local ipairs = ipairs;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
3 local pairs = pairs;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
4 local t_insert = table.insert;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
5 local t_sort = table.sort;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
6 local select = select;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
7
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
8 module "events"
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 function new()
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
11 local dispatchers = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
12 local handlers = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
13 local event_map = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
14 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
15 local _handlers = event_map[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
16 local index = handlers[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
17 if index then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
18 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
19 else index = {}; handlers[event] = index; end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
20 for handler in pairs(_handlers) do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
21 t_insert(index, handler);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
22 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
23 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
24 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
25 local function add_handler(event, handler, priority)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
26 local map = event_map[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
27 if map then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
28 map[handler] = priority or 0;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
29 else
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
30 map = {[handler] = priority or 0};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
31 event_map[event] = map;
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 _rebuild_index(event);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
34 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
35 local function remove_handler(event, handler)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
36 local map = event_map[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
37 if map then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
38 map[handler] = nil;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
39 _rebuild_index(event);
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 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
42 local function add_plugin(plugin)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
43 for event, handler in pairs(plugin) do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
44 add_handler(event, handler);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
45 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
46 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
47 local function remove_plugin(plugin)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
48 for event, handler in pairs(plugin) do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
49 remove_handler(event, handler);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
50 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
51 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
52 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
53 local h = handlers[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
54 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
55 local dispatcher = function(...)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
56 for i=1,#h do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
57 local ret = h[i](...);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
58 if ret ~= nil then return ret; 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 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
61 dispatchers[event] = dispatcher;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
62 return dispatcher;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
63 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
64 local function get_dispatcher(event)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
65 return dispatchers[event] or _create_dispatcher(event);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
66 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
67 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
68 local h = handlers[event];
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
69 if h then
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
70 for i=1,#h do
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
71 local ret = h[i](...);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
72 if ret ~= nil then return ret; end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
73 end
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 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
76 local function get_named_arg_dispatcher(event, ...)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
77 local dispatcher = get_dispatcher(event);
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
78 local keys = {...};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
79 local data = {};
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
80 return function(...)
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
81 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
82 dispatcher(data);
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 end;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
85 return {
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
86 add_handler = add_handler;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
87 remove_handler = remove_handler;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
88 add_plugin = add_plugin;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
89 remove_plugin = remove_plugin;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
90 get_dispatcher = get_dispatcher;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
91 fire_event = fire_event;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
92 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
93 _dispatchers = dispatchers;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
94 _handlers = handlers;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
95 _event_map = event_map;
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
96 };
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
97 end
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
98
92357dffe743 util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents: 1417
diff changeset
99 return _M;