Skip to content

Commit

Permalink
fix(waitFor): allow timeout to be disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
thelindat committed Jan 20, 2024
1 parent 24400f8 commit 0e816ec
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ indent_style = space
max_line_length = 120

[*.lua]
indent_size = 4
indent_size = 4
max_line_length = 160
23 changes: 14 additions & 9 deletions imports/waitFor/shared.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
---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)
local value = cb()

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

while value == nil do
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)
Expand Down
22 changes: 16 additions & 6 deletions package/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,34 @@ export function sleep(ms: number) {
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();

if (value !== undefined) return value;

if (IsDuplicityVersion()) timeout /= 50;
else timeout -= GetFrameTime() * 1000;
if (timeout || timeout == null) {
if (typeof timeout !== 'number') timeout = 1000;

if (IsDuplicityVersion()) timeout /= 50;
else timeout -= GetFrameTime() * 1000;
}

const start = GetGameTimer();
let id: number;
let i = 0;

const p = new Promise<T>((resolve, reject) => {
id = setTick(async () => {
i++;
if (timeout) {
i++;

if (i > timeout)
return reject(`${errMessage || 'failed to resolve callback'} (waited ${(GetGameTimer() - start) / 1000}ms)`);
if (i > timeout)
return reject(`${errMessage || 'failed to resolve callback'} (waited ${(GetGameTimer() - start) / 1000}ms)`);
}

value = await cb();

Expand Down

0 comments on commit 0e816ec

Please sign in to comment.