fix(waitFor): allow timeout to be disabled

This commit is contained in:
Linden
2024-01-20 17:29:36 +11:00
parent 24400f877d
commit 0e816ecfc0
3 changed files with 32 additions and 16 deletions

View File

@@ -1,9 +1,8 @@
---Yields the current thread until a non-nil value is returned by the function.
---Errors after timeout (default 1000) iterations.
---Yields the current thread until a non-nil value is returned by the function.
---@generic T
---@param cb fun(): T?
---@param errMessage string?
---@param timeout number?
---@param timeout number? Error out after `~x` ms. Defaults to 1000, unless set to `false`.
---@return T?
---@async
function lib.waitFor(cb, errMessage, timeout)
@@ -11,14 +10,18 @@ function lib.waitFor(cb, errMessage, timeout)
if value ~= nil then return value end
timeout = tonumber(timeout) or 1000
if IsDuplicityVersion() then
timeout /= 50;
else
timeout -= GetFrameTime() * 1000;
if timeout or timeout == nil then
if type(timeout) ~= 'number' then timeout = 1000 end
if IsDuplicityVersion() then
timeout /= 50;
else
timeout -= GetFrameTime() * 1000;
end
end
local start = GetGameTimer()
local i = 0
@@ -26,7 +29,9 @@ function lib.waitFor(cb, errMessage, timeout)
if timeout then
i += 1
if i > timeout then return error(('%s (waited %.1fms)'):format(errMessage or 'failed to resolve callback', (GetGameTimer() - start) / 1000), 2) end
if i > timeout then
return error(('%s (waited %.1fms)'):format(errMessage or 'failed to resolve callback', (GetGameTimer() - start) / 1000), 2)
end
end
Wait(0)