mirror of
https://github.com/bjc/prosody.git
synced 2025-04-03 21:27:38 +03:00
Add support for arbitrary events and event hooks
This commit is contained in:
parent
6db3d039b3
commit
4ac420e3b0
1 changed files with 23 additions and 0 deletions
|
@ -4,6 +4,7 @@ local log = require "util.logger".init("modulemanager")
|
|||
local loadfile, pcall = loadfile, pcall;
|
||||
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
|
||||
local pairs, ipairs = pairs, ipairs;
|
||||
local t_insert = table.insert;
|
||||
local type = type;
|
||||
|
||||
local tostring, print = tostring, print;
|
||||
|
@ -90,3 +91,25 @@ function handle_stanza(origin, stanza)
|
|||
log("debug", "Stanza unhandled by any modules");
|
||||
return false; -- we didn't handle it
|
||||
end
|
||||
|
||||
do
|
||||
local event_handlers = {};
|
||||
|
||||
function modulehelpers.add_event_hook(name, handler)
|
||||
if not event_handlers[name] then
|
||||
event_handlers[name] = {};
|
||||
end
|
||||
t_insert(event_handlers[name] , handler);
|
||||
end
|
||||
|
||||
function fire_event(name, ...)
|
||||
local event_handlers = event_handlers[name];
|
||||
if event_handlers then
|
||||
for name, handler in ipairs(event_handlers) do
|
||||
handler(...);
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return _M;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue