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 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) function M.seconds(amount)
local time = socket.gettime() + amount local co = coroutine.running()
create(function() assert(co, "You must run this from within a coroutine")
return socket.gettime() >= time timer.delay(amount, false, function()
coroutine.resume(co)
end) end)
coroutine.yield()
end end
function M.eval(fn, timeout)
function M.eval(fn) local co = coroutine.running()
create(fn) assert(co, "You must run this from within a coroutine")
end local start = socket.gettime()
timer.delay(0.01, true, function(self, handle, time_elapsed)
function M.update() if fn() or (timeout and socket.gettime() > (start + timeout)) then
for k,instance in pairs(instances) do timer.cancel(handle)
if instance.fn() then coroutine.resume(co)
instances[k] = nil
coroutine.resume(instance.co)
end
end end
end)
coroutine.yield()
end end
return setmetatable(M, { 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" " w: 1.0\n"
" }\n" " }\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" "embedded_components {\n"
" id: \"screensfactory\"\n" " id: \"screensfactory\"\n"
" type: \"collectionfactory\"\n" " type: \"collectionfactory\"\n"

View File

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