Comparison

core/moduleapi.lua @ 6609:d2faaaca695d

Merge 0.10->trunk
author Matthew Wild <mwild1@gmail.com>
date Fri, 27 Mar 2015 22:24:57 +0000
parent 6557:8aa967c81cbc
child 6647:0c363fddcdd9
comparison
equal deleted inserted replaced
6608:b6e558febb7a 6609:d2faaaca695d
16 local resolve_relative_path = require"util.paths".resolve_relative_path; 16 local resolve_relative_path = require"util.paths".resolve_relative_path;
17 local measure = require "core.statsmanager".measure; 17 local measure = require "core.statsmanager".measure;
18 18
19 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat; 19 local t_insert, t_remove, t_concat = table.insert, table.remove, table.concat;
20 local error, setmetatable, type = error, setmetatable, type; 20 local error, setmetatable, type = error, setmetatable, type;
21 local ipairs, pairs, select, unpack = ipairs, pairs, select, unpack; 21 local ipairs, pairs, select = ipairs, pairs, select;
22 local tonumber, tostring = tonumber, tostring; 22 local tonumber, tostring = tonumber, tostring;
23 local require = require; 23 local require = require;
24 local pack = table.pack or function(...) return {n=select("#",...), ...}; end -- table.pack is only in 5.2
25 local unpack = table.unpack or unpack; -- renamed in 5.2
24 26
25 local prosody = prosody; 27 local prosody = prosody;
26 local hosts = prosody.hosts; 28 local hosts = prosody.hosts;
27 29
28 -- FIXME: This assert() is to try and catch an obscure bug (2013-04-05) 30 -- FIXME: This assert() is to try and catch an obscure bug (2013-04-05)
348 350
349 function api:send(stanza) 351 function api:send(stanza)
350 return core_post_stanza(hosts[self.host], stanza); 352 return core_post_stanza(hosts[self.host], stanza);
351 end 353 end
352 354
353 function api:add_timer(delay, callback) 355 local timer_methods = { }
354 return timer.add_task(delay, function (t) 356 local timer_mt = {
355 if self.loaded == false then return; end 357 __index = timer_methods;
356 return callback(t); 358 }
357 end); 359 function timer_methods:stop( )
360 timer.stop(self.id);
361 end
362 timer_methods.disarm = timer_methods.stop
363 function timer_methods:reschedule(delay)
364 timer.reschedule(self.id, delay)
365 end
366
367 local function timer_callback(now, id, t)
368 if t.module_env.loaded == false then return; end
369 return t.callback(now, unpack(t, 1, t.n));
370 end
371
372 function api:add_timer(delay, callback, ...)
373 local t = pack(...)
374 t.module_env = self;
375 t.callback = callback;
376 t.id = timer.add_task(delay, timer_callback, t);
377 return setmetatable(t, timer_mt);
358 end 378 end
359 379
360 local path_sep = package.config:sub(1,1); 380 local path_sep = package.config:sub(1,1);
361 function api:get_directory() 381 function api:get_directory()
362 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; 382 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil;