diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/datamanager.lua | 7 | ||||
-rw-r--r-- | util/events.lua | 96 |
2 files changed, 103 insertions, 0 deletions
diff --git a/util/datamanager.lua b/util/datamanager.lua index f67c8864..614f0c80 100644 --- a/util/datamanager.lua +++ b/util/datamanager.lua @@ -50,6 +50,7 @@ local function mkdir(path) end local data_path = "data"; +local callback; ------- API ------------- @@ -57,6 +58,9 @@ function set_data_path(path) log("info", "Setting data path to: %s", path); data_path = path; end +function set_callback(func) + callback = func; +end function getpath(username, host, datastore, ext, create) ext = ext or "dat"; @@ -93,6 +97,7 @@ function store(username, host, datastore, data) if not data then data = {}; end + if callback and callback(username, host, datastore) then return true; end -- save the datastore local f, msg = io_open(getpath(username, host, datastore, nil, true), "w+"); if not f then @@ -113,6 +118,7 @@ end function list_append(username, host, datastore, data) if not data then return; end + if callback and callback(username, host, datastore) then return true; end -- save the datastore local f, msg = io_open(getpath(username, host, datastore, "list", true), "a+"); if not f then @@ -130,6 +136,7 @@ function list_store(username, host, datastore, data) if not data then data = {}; end + if callback and callback(username, host, datastore) then return true; end -- save the datastore local f, msg = io_open(getpath(username, host, datastore, "list", true), "w+"); if not f then diff --git a/util/events.lua b/util/events.lua new file mode 100644 index 00000000..b1f3811c --- /dev/null +++ b/util/events.lua @@ -0,0 +1,96 @@ +
+local ipairs = ipairs;
+local pairs = pairs;
+local t_insert = table.insert;
+local select = select;
+
+module "events"
+
+function new()
+ local dispatchers = {};
+ local handlers = {};
+ local event_map = {};
+ local function _rebuild_index() -- TODO optimize index rebuilding
+ for event, _handlers in pairs(event_map) do
+ local index = handlers[event];
+ if index then
+ for i=#index,1,-1 do index[i] = nil; end
+ else index = {}; handlers[event] = index; end
+ for handler in pairs(_handlers) do
+ t_insert(index, handler);
+ end
+ end
+ end;
+ local function add_handler(event, handler)
+ local map = event_map[event];
+ if map then
+ map[handler] = true;
+ else
+ map = {[handler] = true};
+ event_map[event] = map;
+ end
+ _rebuild_index();
+ end;
+ local function remove_handler(event, handler)
+ local map = event_map[event];
+ if map then
+ map[handler] = nil;
+ _rebuild_index();
+ end
+ end;
+ local function add_plugin(plugin)
+ for event, handler in pairs(plugin) do
+ add_handler(event, handler);
+ end
+ end;
+ local function remove_plugin(plugin)
+ for event, handler in pairs(plugin) do
+ remove_handler(event, handler);
+ end
+ end;
+ local function _create_dispatcher(event) -- FIXME duplicate code in fire_event
+ local h = handlers[event];
+ if not h then h = {}; handlers[event] = h; end
+ local dispatcher = function(data)
+ for _, handler in ipairs(h) do
+ handler(data);
+ end
+ end;
+ dispatchers[event] = dispatcher;
+ return dispatcher;
+ end;
+ local function get_dispatcher(event)
+ return dispatchers[event] or _create_dispatcher(event);
+ end;
+ local function fire_event(event, data) -- FIXME duplicates dispatcher code
+ local h = handlers[event];
+ if h then
+ for _, handler in ipairs(h) do
+ handler(data);
+ end
+ end
+ end;
+ local function get_named_arg_dispatcher(event, ...)
+ local dispatcher = get_dispatcher(event);
+ local keys = {...};
+ local data = {};
+ return function(...)
+ for i, key in ipairs(keys) do data[key] = select(i, ...); end
+ dispatcher(data);
+ end;
+ end;
+ return {
+ add_handler = add_handler;
+ remove_handler = remove_handler;
+ add_plugin = add_plugin;
+ remove_plugin = remove_plugin;
+ get_dispatcher = get_dispatcher;
+ fire_event = fire_event;
+ get_named_arg_dispatcher = get_named_arg_dispatcher;
+ _dispatchers = dispatchers;
+ _handlers = handlers;
+ _event_map = event_map;
+ };
+end
+
+return _M;
|