Software /
code /
prosody
Annotate
util/events.lua @ 3498:3f52f3d9db5a
util.events: Removed dispatcher creation functions (these weren't being used).
author | Waqas Hussain <waqas20@gmail.com> |
---|---|
date | Fri, 17 Sep 2010 04:09:58 +0500 |
parent | 2925:692b3c6c5bd2 |
child | 3499:91c55ae31ef3 |
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 |
2923
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
2 -- Copyright (C) 2008-2010 Matthew Wild |
b7049746bd29
Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
3 -- Copyright (C) 2008-2010 Waqas Hussain |
1522
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 pairs = pairs; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
11 local t_insert = table.insert; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
12 local t_sort = table.sort; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
13 |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
14 module "events" |
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 function new() |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
17 local handlers = {}; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
18 local event_map = {}; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
19 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
|
20 local _handlers = event_map[event]; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
21 local index = handlers[event]; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
22 if index then |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
23 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
|
24 else index = {}; handlers[event] = index; end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
25 for handler in pairs(_handlers) do |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
26 t_insert(index, handler); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
27 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
28 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
|
29 end; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
30 local function add_handler(event, handler, priority) |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
31 local map = event_map[event]; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
32 if map then |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
33 map[handler] = priority or 0; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
34 else |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
35 map = {[handler] = priority or 0}; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
36 event_map[event] = map; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
37 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
38 _rebuild_index(event); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
39 end; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
40 local function remove_handler(event, handler) |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
41 local map = event_map[event]; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
42 if map then |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
43 map[handler] = nil; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
44 _rebuild_index(event); |
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; |
2406
2c416334579c
util.events: Rename add_plugin() and remove_plugin() to add_handlers() and remove_handlers() respectively
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
47 local function add_handlers(handlers) |
2c416334579c
util.events: Rename add_plugin() and remove_plugin() to add_handlers() and remove_handlers() respectively
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
48 for event, handler in pairs(handlers) do |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
49 add_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; |
2406
2c416334579c
util.events: Rename add_plugin() and remove_plugin() to add_handlers() and remove_handlers() respectively
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
52 local function remove_handlers(handlers) |
2c416334579c
util.events: Rename add_plugin() and remove_plugin() to add_handlers() and remove_handlers() respectively
Matthew Wild <mwild1@gmail.com>
parents:
1522
diff
changeset
|
53 for event, handler in pairs(handlers) do |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
54 remove_handler(event, handler); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
55 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
56 end; |
3498
3f52f3d9db5a
util.events: Removed dispatcher creation functions (these weren't being used).
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
57 local function fire_event(event, ...) |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
58 local h = handlers[event]; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
59 if h then |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
60 for i=1,#h do |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
61 local ret = h[i](...); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
62 if ret ~= nil then return ret; end |
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 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
65 end; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
66 return { |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
67 add_handler = add_handler; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
68 remove_handler = remove_handler; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
69 add_plugin = add_plugin; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
70 remove_plugin = remove_plugin; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
71 fire_event = fire_event; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
72 _handlers = handlers; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
73 _event_map = event_map; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
74 }; |
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 |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
77 return _M; |