Comparison

util/events.lua @ 1146:542d49518d3a

util.events: event handlers can now return a result, which also interrupts further handling of the event
author Waqas Hussain <waqas20@gmail.com>
date Fri, 15 May 2009 06:23:55 +0500
parent 936:5663db788fdf
child 1175:edef0c10e076
comparison
equal deleted inserted replaced
1145:06051191913d 1146:542d49518d3a
51 local function _create_dispatcher(event) -- FIXME duplicate code in fire_event 51 local function _create_dispatcher(event) -- FIXME duplicate code in fire_event
52 local h = handlers[event]; 52 local h = handlers[event];
53 if not h then h = {}; handlers[event] = h; end 53 if not h then h = {}; handlers[event] = h; end
54 local dispatcher = function(data) 54 local dispatcher = function(data)
55 for _, handler in ipairs(h) do 55 for _, handler in ipairs(h) do
56 handler(data); 56 local ret = handler(data);
57 if ret ~= nil then return ret; end
57 end 58 end
58 end; 59 end;
59 dispatchers[event] = dispatcher; 60 dispatchers[event] = dispatcher;
60 return dispatcher; 61 return dispatcher;
61 end; 62 end;
64 end; 65 end;
65 local function fire_event(event, data) -- FIXME duplicates dispatcher code 66 local function fire_event(event, data) -- FIXME duplicates dispatcher code
66 local h = handlers[event]; 67 local h = handlers[event];
67 if h then 68 if h then
68 for _, handler in ipairs(h) do 69 for _, handler in ipairs(h) do
69 handler(data); 70 local ret = handler(data);
71 if ret ~= nil then return ret; end
70 end 72 end
71 end 73 end
72 end; 74 end;
73 local function get_named_arg_dispatcher(event, ...) 75 local function get_named_arg_dispatcher(event, ...)
74 local dispatcher = get_dispatcher(event); 76 local dispatcher = get_dispatcher(event);