Add support for arbitrary events and event hooks

Fri, 03 Oct 2008 22:17:20 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 03 Oct 2008 22:17:20 +0100
changeset 39
89877d61ac51
parent 38
3fdfd6e0cb4e
child 40
2c0147bbd81a

Add support for arbitrary events and event hooks

core/modulemanager.lua file | annotate | diff | comparison | revisions
--- a/core/modulemanager.lua	Thu Oct 02 01:08:58 2008 +0100
+++ b/core/modulemanager.lua	Fri Oct 03 22:17:20 2008 +0100
@@ -4,6 +4,7 @@
 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 @@
 	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;

mercurial