mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
fix(waitFor): allow timeout to be disabled
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user