Comparison

util/events.lua @ 11058:c99afee1c548

util.events: Add set_debug_hook() method Hook signature: ret = debug_hook(handler, event_name, event_data)
author Matthew Wild <mwild1@gmail.com>
date Thu, 03 Sep 2020 12:59:43 +0100
parent 8759:9e839fb4541a
comparison
equal deleted inserted replaced
11057:13eee48071c8 11058:c99afee1c548
24 local global_wrappers; 24 local global_wrappers;
25 -- Per-event wrappers: wrappers[event_name] = wrapper_function 25 -- Per-event wrappers: wrappers[event_name] = wrapper_function
26 local wrappers = {}; 26 local wrappers = {};
27 -- Event map: event_map[handler_function] = priority_number 27 -- Event map: event_map[handler_function] = priority_number
28 local event_map = {}; 28 local event_map = {};
29 -- Debug hook, if any
30 local active_debug_hook = nil;
29 -- Called on-demand to build handlers entries 31 -- Called on-demand to build handlers entries
30 local function _rebuild_index(self, event) 32 local function _rebuild_index(self, event)
31 local _handlers = event_map[event]; 33 local _handlers = event_map[event];
32 if not _handlers or next(_handlers) == nil then return; end 34 if not _handlers or next(_handlers) == nil then return; end
33 local index = {}; 35 local index = {};
72 remove_handler(event, handler); 74 remove_handler(event, handler);
73 end 75 end
74 end; 76 end;
75 local function _fire_event(event_name, event_data) 77 local function _fire_event(event_name, event_data)
76 local h = handlers[event_name]; 78 local h = handlers[event_name];
77 if h then 79 if h and not active_debug_hook then
78 for i=1,#h do 80 for i=1,#h do
79 local ret = h[i](event_data); 81 local ret = h[i](event_data);
82 if ret ~= nil then return ret; end
83 end
84 elseif h and active_debug_hook then
85 for i=1,#h do
86 local ret = active_debug_hook(h[i], event_name, event_data);
80 if ret ~= nil then return ret; end 87 if ret ~= nil then return ret; end
81 end 88 end
82 end 89 end
83 end; 90 end;
84 local function fire_event(event_name, event_data) 91 local function fire_event(event_name, event_data)
138 else 145 else
139 wrappers[event_name] = nil; 146 wrappers[event_name] = nil;
140 end 147 end
141 end 148 end
142 end 149 end
150
151 local function set_debug_hook(new_hook)
152 local old_hook = active_debug_hook;
153 active_debug_hook = new_hook;
154 return old_hook;
155 end
156
143 return { 157 return {
144 add_handler = add_handler; 158 add_handler = add_handler;
145 remove_handler = remove_handler; 159 remove_handler = remove_handler;
146 add_handlers = add_handlers; 160 add_handlers = add_handlers;
147 remove_handlers = remove_handlers; 161 remove_handlers = remove_handlers;
148 get_handlers = get_handlers; 162 get_handlers = get_handlers;
149 wrappers = { 163 wrappers = {
150 add_handler = add_wrapper; 164 add_handler = add_wrapper;
151 remove_handler = remove_wrapper; 165 remove_handler = remove_wrapper;
152 }; 166 };
167
153 add_wrapper = add_wrapper; 168 add_wrapper = add_wrapper;
154 remove_wrapper = remove_wrapper; 169 remove_wrapper = remove_wrapper;
170
171 set_debug_hook = set_debug_hook;
172
155 fire_event = fire_event; 173 fire_event = fire_event;
156 _handlers = handlers; 174 _handlers = handlers;
157 _event_map = event_map; 175 _event_map = event_map;
158 }; 176 };
159 end 177 end