mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-16 22:46:03 +01:00
fix(waitFor): allow timeout to be disabled
This commit is contained in:
@@ -14,3 +14,4 @@ max_line_length = 120
|
|||||||
|
|
||||||
[*.lua]
|
[*.lua]
|
||||||
indent_size = 4
|
indent_size = 4
|
||||||
|
max_line_length = 160
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
---Yields the current thread until a non-nil value is returned by the function.
|
---Yields the current thread until a non-nil value is returned by the function.
|
||||||
---Errors after timeout (default 1000) iterations.
|
|
||||||
---@generic T
|
---@generic T
|
||||||
---@param cb fun(): T?
|
---@param cb fun(): T?
|
||||||
---@param errMessage string?
|
---@param errMessage string?
|
||||||
---@param timeout number?
|
---@param timeout number? Error out after `~x` ms. Defaults to 1000, unless set to `false`.
|
||||||
---@return T?
|
---@return T?
|
||||||
---@async
|
---@async
|
||||||
function lib.waitFor(cb, errMessage, timeout)
|
function lib.waitFor(cb, errMessage, timeout)
|
||||||
@@ -11,14 +10,18 @@ function lib.waitFor(cb, errMessage, timeout)
|
|||||||
|
|
||||||
if value ~= nil then return value end
|
if value ~= nil then return value end
|
||||||
|
|
||||||
timeout = tonumber(timeout) or 1000
|
|
||||||
|
|
||||||
if IsDuplicityVersion() then
|
if timeout or timeout == nil then
|
||||||
timeout /= 50;
|
if type(timeout) ~= 'number' then timeout = 1000 end
|
||||||
else
|
|
||||||
timeout -= GetFrameTime() * 1000;
|
if IsDuplicityVersion() then
|
||||||
|
timeout /= 50;
|
||||||
|
else
|
||||||
|
timeout -= GetFrameTime() * 1000;
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local start = GetGameTimer()
|
local start = GetGameTimer()
|
||||||
local i = 0
|
local i = 0
|
||||||
|
|
||||||
@@ -26,7 +29,9 @@ function lib.waitFor(cb, errMessage, timeout)
|
|||||||
if timeout then
|
if timeout then
|
||||||
i += 1
|
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
|
end
|
||||||
|
|
||||||
Wait(0)
|
Wait(0)
|
||||||
|
|||||||
@@ -11,13 +11,21 @@ export function sleep(ms: number) {
|
|||||||
return new Promise((resolve) => setTimeout(resolve, ms, null));
|
return new Promise((resolve) => setTimeout(resolve, ms, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function waitFor<T>(cb: () => T, errMessage: string | void, timeout = 1000): Promise<T> {
|
/**
|
||||||
|
* Creates a promise that will be resolved once any value is returned by the function (including null).
|
||||||
|
* @param {number?} timeout Error out after `~x` ms. Defaults to 1000, unless set to `false`.
|
||||||
|
*/
|
||||||
|
export async function waitFor<T>(cb: () => T, errMessage?: string, timeout?: number): Promise<T> {
|
||||||
let value = await cb();
|
let value = await cb();
|
||||||
|
|
||||||
if (value !== undefined) return value;
|
if (value !== undefined) return value;
|
||||||
|
|
||||||
if (IsDuplicityVersion()) timeout /= 50;
|
if (timeout || timeout == null) {
|
||||||
else timeout -= GetFrameTime() * 1000;
|
if (typeof timeout !== 'number') timeout = 1000;
|
||||||
|
|
||||||
|
if (IsDuplicityVersion()) timeout /= 50;
|
||||||
|
else timeout -= GetFrameTime() * 1000;
|
||||||
|
}
|
||||||
|
|
||||||
const start = GetGameTimer();
|
const start = GetGameTimer();
|
||||||
let id: number;
|
let id: number;
|
||||||
@@ -25,10 +33,12 @@ export async function waitFor<T>(cb: () => T, errMessage: string | void, timeout
|
|||||||
|
|
||||||
const p = new Promise<T>((resolve, reject) => {
|
const p = new Promise<T>((resolve, reject) => {
|
||||||
id = setTick(async () => {
|
id = setTick(async () => {
|
||||||
i++;
|
if (timeout) {
|
||||||
|
i++;
|
||||||
|
|
||||||
if (i > timeout)
|
if (i > timeout)
|
||||||
return reject(`${errMessage || 'failed to resolve callback'} (waited ${(GetGameTimer() - start) / 1000}ms)`);
|
return reject(`${errMessage || 'failed to resolve callback'} (waited ${(GetGameTimer() - start) / 1000}ms)`);
|
||||||
|
}
|
||||||
|
|
||||||
value = await cb();
|
value = await cb();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user