3
0
mirror of https://github.com/britzl/monarch.git synced 2025-06-27 02:17:53 +02:00
Monarch-Extension/test/cowait.lua
Björn Ritzl 20cf731fdb
Wait with screen unload until next screen is loaded and ready to be shown (#74)
* Removed screen flicker when transitioning between screens

* Moved advanced example to subfolder. Added basic example.

* Remove flicker on back navigation too

* Fix issue with no_stack screens added at any time
2021-06-24 07:45:09 +02:00

41 lines
942 B
Lua

local M = {}
function M.seconds(amount)
local co = coroutine.running()
assert(co, "You must run this from within a coroutine")
timer.delay(amount, false, function()
local ok, err = coroutine.resume(co)
if not ok then
print(err)
end
end)
coroutine.yield()
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.02, true, function(self, handle, time_elapsed)
if fn() or (timeout and socket.gettime() > (start + timeout)) then
timer.cancel(handle)
local ok, err = coroutine.resume(co)
if not ok then
print(err)
end
end
end)
coroutine.yield()
end
return setmetatable(M, {
__call = function(self, arg1, ...)
if type(arg1) == "number" then
return M.seconds(arg1, ...)
elseif type(arg1) == "function" then
return M.eval(arg1, ...)
else
error("Unknown argument type")
end
end
})