Comparison

util/events.lua @ 1181:dffbb7d1da4b

util.events: Dispatch code now accepts a variable number of arguments
author Waqas Hussain <waqas20@gmail.com>
date Sun, 17 May 2009 04:50:32 +0500
parent 1180:8c5d945c1f35
child 1417:b02b22f77326
comparison
equal deleted inserted replaced
1180:8c5d945c1f35 1181:dffbb7d1da4b
50 end 50 end
51 end; 51 end;
52 local function _create_dispatcher(event) -- FIXME duplicate code in fire_event 52 local function _create_dispatcher(event) -- FIXME duplicate code in fire_event
53 local h = handlers[event]; 53 local h = handlers[event];
54 if not h then h = {}; handlers[event] = h; end 54 if not h then h = {}; handlers[event] = h; end
55 local dispatcher = function(data) 55 local dispatcher = function(...)
56 for _, handler in ipairs(h) do 56 for _, handler in ipairs(h) do
57 local ret = handler(data); 57 local ret = handler(...);
58 if ret ~= nil then return ret; end 58 if ret ~= nil then return ret; end
59 end 59 end
60 end; 60 end;
61 dispatchers[event] = dispatcher; 61 dispatchers[event] = dispatcher;
62 return dispatcher; 62 return dispatcher;
63 end; 63 end;
64 local function get_dispatcher(event) 64 local function get_dispatcher(event)
65 return dispatchers[event] or _create_dispatcher(event); 65 return dispatchers[event] or _create_dispatcher(event);
66 end; 66 end;
67 local function fire_event(event, data) -- FIXME duplicates dispatcher code 67 local function fire_event(event, ...) -- FIXME duplicates dispatcher code
68 local h = handlers[event]; 68 local h = handlers[event];
69 if h then 69 if h then
70 for _, handler in ipairs(h) do 70 for _, handler in ipairs(h) do
71 local ret = handler(data); 71 local ret = handler(...);
72 if ret ~= nil then return ret; end 72 if ret ~= nil then return ret; end
73 end 73 end
74 end 74 end
75 end; 75 end;
76 local function get_named_arg_dispatcher(event, ...) 76 local function get_named_arg_dispatcher(event, ...)