Comparison

util/async.lua @ 8651:1b7c5933b215

util.async: Add sleep() method
author Matthew Wild <mwild1@gmail.com>
date Thu, 22 Mar 2018 13:02:00 +0000
parent 8649:9246f64d6f1e
child 8655:ba6a6a04b46c
comparison
equal deleted inserted replaced
8650:2f133b6e8740 8651:1b7c5933b215
1 local log = require "util.logger".init("util.async"); 1 local log = require "util.logger".init("util.async");
2 local new_id = require "util.id".short; 2 local new_id = require "util.id".short;
3 local timer = require "util.timer";
3 4
4 local function checkthread() 5 local function checkthread()
5 local thread, main = coroutine.running(); 6 local thread, main = coroutine.running();
6 if not thread or main then 7 if not thread or main then
7 error("Not running in an async context, see https://prosody.im/doc/developers/util/async"); 8 error("Not running in an async context, see https://prosody.im/doc/developers/util/async");
231 local r = runner(func, once_watchers); 232 local r = runner(func, once_watchers);
232 return r:run(func); 233 return r:run(func);
233 end 234 end
234 end 235 end
235 236
237 local function sleep(s)
238 local wait, done = waiter();
239 timer.add_task(s, done);
240 wait();
241 end
242
236 return { 243 return {
237 once = once; 244 once = once;
238 ready = ready; 245 ready = ready;
239 waiter = waiter; 246 waiter = waiter;
240 guarder = guarder; 247 guarder = guarder;
241 runner = runner 248 runner = runner;
249 sleep = sleep;
242 }; 250 };