Software /
code /
prosody
Comparison
core/moduleapi.lua @ 5899:26f54b462601
core/moduleapi: Return timer object from module:add_timer
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Wed, 06 Nov 2013 12:56:35 -0500 |
parent | 5825:ac5e05ffc921 |
child | 5900:cb1103423aa7 |
comparison
equal
deleted
inserted
replaced
5898:bf9aba718c01 | 5899:26f54b462601 |
---|---|
345 | 345 |
346 function api:send(stanza) | 346 function api:send(stanza) |
347 return core_post_stanza(hosts[self.host], stanza); | 347 return core_post_stanza(hosts[self.host], stanza); |
348 end | 348 end |
349 | 349 |
350 function api:add_timer(delay, callback) | 350 local timer_methods = { } |
351 return timer.add_task(delay, function (t) | 351 local timer_mt = { |
352 if self.loaded == false then return; end | 352 __index = timer_methods; |
353 return callback(t); | 353 } |
354 end); | 354 function timer_methods:stop( ) |
355 timer.stop(self.id); | |
356 end | |
357 timer_methods.disarm = timer_methods.stop | |
358 function timer_methods:reschedule(delay) | |
359 timer.reschedule(self.id, delay) | |
360 end | |
361 | |
362 local function timer_callback(now, id, t) | |
363 if t.module_env.loaded == false then return; end | |
364 return t.callback(now, unpack(t, 1, t.n)); | |
365 end | |
366 | |
367 local pack = table.pack or function(...) return {n=select("#",...), ...}; end | |
368 function api:add_timer(delay, callback, ...) | |
369 local t = pack(...) | |
370 t.module_env = self; | |
371 t.callback = callback; | |
372 t.id = timer.add_task(delay, timer_callback, t); | |
373 return setmetatable(t, timer_mt); | |
355 end | 374 end |
356 | 375 |
357 local path_sep = package.config:sub(1,1); | 376 local path_sep = package.config:sub(1,1); |
358 function api:get_directory() | 377 function api:get_directory() |
359 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; | 378 return self.path and (self.path:gsub("%"..path_sep.."[^"..path_sep.."]*$", "")) or nil; |