Comparison

util/filters.lua @ 3142:3cf02e5e632b

util.filters: Ensure initialize() never initializes an object twice - and return the filter function to the caller
author Matthew Wild <mwild1@gmail.com>
date Wed, 02 Jun 2010 17:23:57 +0100
parent 3140:8c6ed6fd2d35
child 3331:57a9cf5f4259
comparison
equal deleted inserted replaced
3141:50318ac90394 3142:3cf02e5e632b
9 local t_insert, t_remove = table.insert, table.remove; 9 local t_insert, t_remove = table.insert, table.remove;
10 10
11 module "filters" 11 module "filters"
12 12
13 function initialize(session) 13 function initialize(session)
14 local filters = {}; 14 if not session.filters then
15 session.filters = filters; 15 local filters = {};
16 16 session.filters = filters;
17 function session.filter(type, data) 17
18 local filter_list = filters[type]; 18 function session.filter(type, data)
19 if filter_list then 19 local filter_list = filters[type];
20 for i = 1, #filter_list do 20 if filter_list then
21 data = filter_list[i](data); 21 for i = 1, #filter_list do
22 if data == nil then break; end 22 data = filter_list[i](data);
23 if data == nil then break; end
24 end
23 end 25 end
26 return data;
24 end 27 end
25 return data;
26 end 28 end
29 return session.filter;
27 end 30 end
28 31
29 function add_filter(session, type, callback, priority) 32 function add_filter(session, type, callback, priority)
30 if not session.filters then 33 if not session.filters then
31 initialize(session); 34 initialize(session);