Software /
code /
prosody
Annotate
util/events.lua @ 7100:301d58705667
mod_c2s, mod_s2s: Lower priority of session shutdown to negative, so that plugins hooking at the default priority run first (fixes #601)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 26 Jan 2016 00:28:07 +0100 |
parent | 3776:bc4f67a0658d |
child | 5749:60b3b6d27364 |
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; |
3776
bc4f67a0658d
util.events: Remove an event's table when it has no more handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
3501
diff
changeset
|
48 if next(map) == nil then |
bc4f67a0658d
util.events: Remove an event's table when it has no more handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
3501
diff
changeset
|
49 event_map[event] = nil; |
bc4f67a0658d
util.events: Remove an event's table when it has no more handlers.
Waqas Hussain <waqas20@gmail.com>
parents:
3501
diff
changeset
|
50 end |
1507
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 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
|
53 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
|
54 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
|
55 add_handler(event, handler); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
56 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
57 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
|
58 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
|
59 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
|
60 remove_handler(event, handler); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
61 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
62 end; |
3498
3f52f3d9db5a
util.events: Removed dispatcher creation functions (these weren't being used).
Waqas Hussain <waqas20@gmail.com>
parents:
2925
diff
changeset
|
63 local function fire_event(event, ...) |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
64 local h = handlers[event]; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
65 if h then |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
66 for i=1,#h do |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
67 local ret = h[i](...); |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
68 if ret ~= nil then return ret; end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
69 end |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
70 end |
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 return { |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
73 add_handler = add_handler; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
74 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
|
75 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
|
76 remove_handlers = remove_handlers; |
1507
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
77 fire_event = fire_event; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
78 _handlers = handlers; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
79 _event_map = event_map; |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
80 }; |
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 |
92357dffe743
util.events: Convert from Windows line endings
Matthew Wild <mwild1@gmail.com>
parents:
1417
diff
changeset
|
83 return _M; |