Comparison

core/modulemanager.lua @ 39:89877d61ac51

Add support for arbitrary events and event hooks
author Matthew Wild <mwild1@gmail.com>
date Fri, 03 Oct 2008 22:17:20 +0100
parent 38:3fdfd6e0cb4e
child 42:2e3715e30912
comparison
equal deleted inserted replaced
38:3fdfd6e0cb4e 39:89877d61ac51
2 local log = require "util.logger".init("modulemanager") 2 local log = require "util.logger".init("modulemanager")
3 3
4 local loadfile, pcall = loadfile, pcall; 4 local loadfile, pcall = loadfile, pcall;
5 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv; 5 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
6 local pairs, ipairs = pairs, ipairs; 6 local pairs, ipairs = pairs, ipairs;
7 local t_insert = table.insert;
7 local type = type; 8 local type = type;
8 9
9 local tostring, print = tostring, print; 10 local tostring, print = tostring, print;
10 11
11 local _G = _G; 12 local _G = _G;
88 end 89 end
89 end 90 end
90 log("debug", "Stanza unhandled by any modules"); 91 log("debug", "Stanza unhandled by any modules");
91 return false; -- we didn't handle it 92 return false; -- we didn't handle it
92 end 93 end
94
95 do
96 local event_handlers = {};
97
98 function modulehelpers.add_event_hook(name, handler)
99 if not event_handlers[name] then
100 event_handlers[name] = {};
101 end
102 t_insert(event_handlers[name] , handler);
103 end
104
105 function fire_event(name, ...)
106 local event_handlers = event_handlers[name];
107 if event_handlers then
108 for name, handler in ipairs(event_handlers) do
109 handler(...);
110 end
111 end
112 end
113 end
114
115 return _M;