Mercurial > prosody-hg
comparison util/async.lua @ 8685:2548111e71d6
util.timer: Move sleep() here from util.async
This is to solve a indirect dependency issue where net.server was initialized before the config was read
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Fri, 23 Mar 2018 21:18:15 +0100 |
| parents | 801f253ef52d |
| children | 01264bc60c2b |
comparison
equal
deleted
inserted
replaced
| 8684:801f253ef52d | 8685:2548111e71d6 |
|---|---|
| 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"; | |
| 4 | 3 |
| 5 local function checkthread() | 4 local function checkthread() |
| 6 local thread, main = coroutine.running(); | 5 local thread, main = coroutine.running(); |
| 7 if not thread or main then | 6 if not thread or main then |
| 8 error("Not running in an async context, see https://prosody.im/doc/developers/util/async"); | 7 error("Not running in an async context, see https://prosody.im/doc/developers/util/async"); |
| 224 | 223 |
| 225 local function ready() | 224 local function ready() |
| 226 return pcall(checkthread); | 225 return pcall(checkthread); |
| 227 end | 226 end |
| 228 | 227 |
| 229 local function sleep(s) | |
| 230 local wait, done = waiter(); | |
| 231 timer.add_task(s, done); | |
| 232 wait(); | |
| 233 end | |
| 234 | |
| 235 return { | 228 return { |
| 236 ready = ready; | 229 ready = ready; |
| 237 waiter = waiter; | 230 waiter = waiter; |
| 238 guarder = guarder; | 231 guarder = guarder; |
| 239 runner = runner; | 232 runner = runner; |
| 240 sleep = sleep; | |
| 241 }; | 233 }; |
