diff --git a/.editorconfig b/.editorconfig index 7cc3d6a..b5a926c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -13,4 +13,5 @@ indent_style = space max_line_length = 120 [*.lua] -indent_size = 4 \ No newline at end of file +indent_size = 4 +max_line_length = 160 diff --git a/imports/waitFor/shared.lua b/imports/waitFor/shared.lua index 2bfa107..460697a 100644 --- a/imports/waitFor/shared.lua +++ b/imports/waitFor/shared.lua @@ -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) diff --git a/package/shared/index.ts b/package/shared/index.ts index 499f3b0..2235456 100644 --- a/package/shared/index.ts +++ b/package/shared/index.ts @@ -11,13 +11,21 @@ export function sleep(ms: number) { return new Promise((resolve) => setTimeout(resolve, ms, null)); } -export async function waitFor(cb: () => T, errMessage: string | void, timeout = 1000): Promise { +/** + * 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(cb: () => T, errMessage?: string, timeout?: number): Promise { 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; @@ -25,10 +33,12 @@ export async function waitFor(cb: () => T, errMessage: string | void, timeout const p = new Promise((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();