Software /
code /
prosody
Annotate
util/events.lua @ 3641:3603aeb325de
mod_pubsub, util.pubsub: Support for fetching items
author | Florian Zeitz <florob@babelmonkeys.de> |
---|---|
date | Thu, 25 Nov 2010 21:47:12 +0100 |
parent | 3501:90c18e0355af |
child | 3776:bc4f67a0658d |
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; |
3501
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
13 local setmetatable = setmetatable; |
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
14 local next = next; |
1507
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 handlers = {}; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
20 local event_map = {}; |
3501
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
21 local function _rebuild_index(handlers, event) |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
22 local _handlers = event_map[event]; |
3501
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
23 if not _handlers or next(_handlers) == nil then return; end |
3500
a49ed9166820
util.events: Create new index on handler change instead of modifying existing one (this makes util.events fully reentrant).
Waqas Hussain <waqas20@gmail.com>
parents:
3499
diff
changeset
|
24 local index = {}; |
1507
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); |
3500
a49ed9166820
util.events: Create new index on handler change instead of modifying existing one (this makes util.events fully reentrant).
Waqas Hussain <waqas20@gmail.com>
parents:
3499
diff
changeset
|
29 handlers[event] = index; |
3501
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
30 return index; |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
31 end; |
3501
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
32 setmetatable(handlers, { __index = _rebuild_index }); |
1507
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 |
3501
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
41 handlers[event] = nil; |
1507
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; |
3501
90c18e0355af
util.events: Event handler indices are now built lazily (faster server startup for large number of hosts).
Waqas Hussain <waqas20@gmail.com>
parents:
3500
diff
changeset
|
47 handlers[event] = nil; |
1507
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; |
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
|
50 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
|
51 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
|
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; |
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
|
55 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
|
56 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
|
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; |
3498
3f52f3d9db5a
util.events: Removed dispatcher creation functions (these weren't being used).
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
60 local function fire_event(event, ...) |
1507
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 h then |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
63 for i=1,#h do |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
64 local ret = h[i](...); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
65 if ret ~= nil then return ret; end |
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 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 return { |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
70 add_handler = add_handler; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
71 remove_handler = remove_handler; |
3499
91c55ae31ef3
util.events: Fixed the exposed API for adding/removing sets of event handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
3498
diff
changeset
|
72 add_handlers = add_handlers; |
91c55ae31ef3
util.events: Fixed the exposed API for adding/removing sets of event handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
3498
diff
changeset
|
73 remove_handlers = remove_handlers; |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
74 fire_event = fire_event; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
75 _handlers = handlers; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
76 _event_map = event_map; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
77 }; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
78 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
79 |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
80 return _M; |