Diff

util/datamanager.lua @ 1381:46a58df8557d

util.datamanager: Allow multiple data storage callbacks, and allow them to modify parameters
author Matthew Wild <mwild1@gmail.com>
date Mon, 22 Jun 2009 14:15:33 +0100
parent 1097:c5b33640a5f0
child 1462:44780b856ce7
line wrap: on
line diff
--- a/util/datamanager.lua	Mon Jun 22 14:14:28 2009 +0100
+++ b/util/datamanager.lua	Mon Jun 22 14:15:33 2009 +0100
@@ -50,7 +50,7 @@
 end
 
 local data_path = "data";
-local callback;
+local callbacks = {};
 
 ------- API -------------
 
@@ -58,8 +58,32 @@
 	log("debug", "Setting data path to: %s", path);
 	data_path = path;
 end
-function set_callback(func)
-	callback = func;
+
+local function callback(username, host, datastore, data)
+	for _, f in ipairs(callbacks) do
+		username, host, datastore, data = f(username, host, datastore, data);
+		if not username then break; end
+	end
+	
+	return username, host, datastore, data;
+end
+function add_callback(func)
+	if not callbacks[func] then -- Would you really want to set the same callback more than once?
+		callbacks[func] = true;
+		callbacks[#callbacks+1] = func;
+		return true;
+	end
+end
+function remove_callback(func)
+	if callbacks[func] then
+		for i, f in ipairs(callbacks) do
+			if f == func then
+				callbacks[i] = nil;
+				callbacks[f] = nil;
+				return true;
+			end
+		end
+	end
 end
 
 function getpath(username, host, datastore, ext, create)
@@ -97,7 +121,12 @@
 	if not data then
 		data = {};
 	end
-	if callback and callback(username, host, datastore) then return true; end
+
+	username, host, datastore, data = callback(username, host, datastore, data);
+	if not username then
+		return true; -- Don't save this data at all
+	end
+
 	-- save the datastore
 	local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+");
 	if not f then