3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-27 10:27:49 +02:00

Improved test wait functionality

This commit is contained in:
Björn Ritzl 2019-08-01 07:13:25 +02:00
parent ebc1d09c21
commit 63c965572d
4 changed files with 19 additions and 58 deletions

View File

@ -1,38 +1,25 @@
local M = {}
local instances = {}
local function create(fn)
local instance = {
co = coroutine.running(),
fn = fn,
}
table.insert(instances, instance)
coroutine.yield(instance.co)
end
function M.seconds(amount)
local time = socket.gettime() + amount
create(function()
return socket.gettime() >= time
local co = coroutine.running()
assert(co, "You must run this from within a coroutine")
timer.delay(amount, false, function()
coroutine.resume(co)
end)
coroutine.yield()
end
function M.eval(fn)
create(fn)
end
function M.update()
for k,instance in pairs(instances) do
if instance.fn() then
instances[k] = nil
coroutine.resume(instance.co)
end
function M.eval(fn, timeout)
local co = coroutine.running()
assert(co, "You must run this from within a coroutine")
local start = socket.gettime()
timer.delay(0.01, true, function(self, handle, time_elapsed)
if fn() or (timeout and socket.gettime() > (start + timeout)) then
timer.cancel(handle)
coroutine.resume(co)
end
end)
coroutine.yield()
end
return setmetatable(M, {

View File

@ -1,5 +0,0 @@
local cowait = require "test.cowait"
function update(self, dt)
cowait.update()
end

View File

@ -17,21 +17,6 @@ embedded_instances {
" w: 1.0\n"
" }\n"
"}\n"
"components {\n"
" id: \"cowait\"\n"
" component: \"/test/cowait.script\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"screensfactory\"\n"
" type: \"collectionfactory\"\n"

View File

@ -28,20 +28,14 @@ return function()
local function wait_timeout(fn, ...)
local args = { ... }
local time = socket.gettime()
cowait(function()
return fn(unpack(args)) or socket.gettime() > time + 5
end)
cowait(function() return fn(unpack(args)) end, 5)
return fn(...)
end
local function wait_until_done(fn)
local is_done = false
local function done()
is_done = true
end
fn(done)
wait_timeout(function() return is_done end)
local done = false
fn(function() done = true end)
wait_timeout(function() return done end)
end
local function wait_until_visible(screen_id)
return wait_timeout(is_visible, screen_id)