From 276fc69449f4d4a12816147ffaf51492e97c073c Mon Sep 17 00:00:00 2001 From: zykem#0643 <86602828+Zykem@users.noreply.github.com> Date: Sun, 15 Dec 2024 10:11:01 +0100 Subject: [PATCH 1/5] Add ESX.Await function --- [core]/es_extended/shared/functions.lua | 39 +++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index 1a5d00900..293d73c91 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -170,4 +170,43 @@ function ESX.IsFunctionReference(val) local typeVal = type(val) return typeVal == "function" or (typeVal == "table" and type(getmetatable(val)?.__call) == "function") +end + +---@param conditionFunc function A function that returns a boolean indicating whether the condition is met. +---@param timeout number The maximum time (in milliseconds) to wait for the condition to be met. +---@param errorMessage? string The error message to print if the condition is not met within the timeout period. +---@return boolean: Returns true if the condition is met within the timeout, otherwise returns `false`. +---@return number: The time (in milliseconds) it took to meet the condition, or the timeout time if not met. +ESX.Await = function(conditionFunc, timeout, errorMessage) + timeout = timeout or 1000 + + if timeout < 0 then + error('Timeout should be a positive number.') + end + + local startTime = GetGameTimer() + local prefix = ('[%s] -> '):format(GetInvokingResource()) + + while GetGameTimer() - startTime < timeout do + local success, result = pcall(conditionFunc) + + if not success then + print(prefix .. 'Error while calling conditionFunc! Result: ' .. result) + local elapsedTime = GetGameTimer() - startTime + return false, elapsedTime + end + + if result then + local elapsedTime = GetGameTimer() - startTime + return true, elapsedTime + end + + Wait(10) + end + + if errorMessage then + print(prefix .. errorMessage) + end + + return false, timeout end \ No newline at end of file From c32c41d718d62836cea6ee29e86e61ee2dfa1cf4 Mon Sep 17 00:00:00 2001 From: zykem#0643 <86602828+Zykem@users.noreply.github.com> Date: Sun, 15 Dec 2024 14:26:57 +0100 Subject: [PATCH 2/5] Addressed PR feedback from Kenshiin13 --- [core]/es_extended/shared/functions.lua | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index 293d73c91..e64112228 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -173,17 +173,22 @@ function ESX.IsFunctionReference(val) end ---@param conditionFunc function A function that returns a boolean indicating whether the condition is met. ----@param timeout number The maximum time (in milliseconds) to wait for the condition to be met. ---@param errorMessage? string The error message to print if the condition is not met within the timeout period. ----@return boolean: Returns true if the condition is met within the timeout, otherwise returns `false`. ----@return number: The time (in milliseconds) it took to meet the condition, or the timeout time if not met. -ESX.Await = function(conditionFunc, timeout, errorMessage) +---@param timeout? number The maximum time (in milliseconds) to wait for the condition to be met. +---@return boolean, number: Returns success status and the time taken +ESX.Await = function(conditionFunc, errorMessage, timeout) timeout = timeout or 1000 if timeout < 0 then error('Timeout should be a positive number.') end + local isFunctionReference = ESX.IsFunctionReference(conditionFunc) + + if not isFunctionReference then + error('Condition Function should be a function reference.') + end + local startTime = GetGameTimer() local prefix = ('[%s] -> '):format(GetInvokingResource()) @@ -191,7 +196,8 @@ ESX.Await = function(conditionFunc, timeout, errorMessage) local success, result = pcall(conditionFunc) if not success then - print(prefix .. 'Error while calling conditionFunc! Result: ' .. result) + local conditionErrorMessage = ('%s Error while calling conditionFunc! Result: %s'):format(prefix, result) + print(conditionErrorMessage) local elapsedTime = GetGameTimer() - startTime return false, elapsedTime end @@ -201,11 +207,14 @@ ESX.Await = function(conditionFunc, timeout, errorMessage) return true, elapsedTime end - Wait(10) + Wait(0) end if errorMessage then - print(prefix .. errorMessage) + ESX.AssertType(errorMessage, 'string', 'errorMessage should be a string.') + + local formattedErrorMessage = ('%s %s'):format(prefix, errorMessage) + print(formattedErrorMessage) end return false, timeout From b6e82bd93da1ceece4c70504215d87995a090283 Mon Sep 17 00:00:00 2001 From: zykem#0643 <86602828+Zykem@users.noreply.github.com> Date: Sun, 15 Dec 2024 15:31:40 +0100 Subject: [PATCH 3/5] Addressing requested changes: pcall replace, errorMessage validation before loop, throwing actual error as formattedErrorMessage --- [core]/es_extended/shared/functions.lua | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index e64112228..4b8b4f2d9 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -189,18 +189,16 @@ ESX.Await = function(conditionFunc, errorMessage, timeout) error('Condition Function should be a function reference.') end + -- since errorMessage is optional, we only validate it if the user provided it. + if errorMessage then + ESX.AssertType(errorMessage, 'string', 'errorMessage should be a string.') + end + local startTime = GetGameTimer() local prefix = ('[%s] -> '):format(GetInvokingResource()) while GetGameTimer() - startTime < timeout do - local success, result = pcall(conditionFunc) - - if not success then - local conditionErrorMessage = ('%s Error while calling conditionFunc! Result: %s'):format(prefix, result) - print(conditionErrorMessage) - local elapsedTime = GetGameTimer() - startTime - return false, elapsedTime - end + local result = conditionFunc() if result then local elapsedTime = GetGameTimer() - startTime @@ -211,10 +209,8 @@ ESX.Await = function(conditionFunc, errorMessage, timeout) end if errorMessage then - ESX.AssertType(errorMessage, 'string', 'errorMessage should be a string.') - local formattedErrorMessage = ('%s %s'):format(prefix, errorMessage) - print(formattedErrorMessage) + error(formattedErrorMessage) end return false, timeout From 9336cfdc69da223d311c4a3bcbe41c25d70d1bb3 Mon Sep 17 00:00:00 2001 From: Kenshin13 <63159154+Kenshiin13@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:51:12 +0100 Subject: [PATCH 4/5] fix(es_extended/shared/functions): return result of conditionFunc & refactored code --- [core]/es_extended/shared/functions.lua | 42 +++++++++++-------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index 4b8b4f2d9..52e3030ae 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -172,46 +172,40 @@ function ESX.IsFunctionReference(val) return typeVal == "function" or (typeVal == "table" and type(getmetatable(val)?.__call) == "function") end ----@param conditionFunc function A function that returns a boolean indicating whether the condition is met. ----@param errorMessage? string The error message to print if the condition is not met within the timeout period. ----@param timeout? number The maximum time (in milliseconds) to wait for the condition to be met. ----@return boolean, number: Returns success status and the time taken -ESX.Await = function(conditionFunc, errorMessage, timeout) - timeout = timeout or 1000 - - if timeout < 0 then - error('Timeout should be a positive number.') +---@param conditionFunc function A function that is repeatedly called until it returns a truthy value or the timeout is exceeded. +---@param errorMessage? string Optional. If set, an error will be thrown with this message if the condition is not met within the timeout. If not set, no error will be thrown. +---@param timeoutMs? number Optional. The maximum time to wait (in milliseconds) for the condition to be met. Defaults to 1000ms. +---@return boolean, any: Returns success status and the returned value of the condition function. +function ESX.Await(conditionFunc, errorMessage, timeoutMs) + timeoutMs = timeoutMs or 1000 + + if timeoutMs < 0 then + error("Timeout should be a positive number.") end - local isFunctionReference = ESX.IsFunctionReference(conditionFunc) - - if not isFunctionReference then - error('Condition Function should be a function reference.') + if not ESX.IsFunctionReference(conditionFunc) then + error("Condition Function should be a function reference.") end -- since errorMessage is optional, we only validate it if the user provided it. if errorMessage then - ESX.AssertType(errorMessage, 'string', 'errorMessage should be a string.') + ESX.AssertType(errorMessage, "string", "errorMessage should be a string.") end - local startTime = GetGameTimer() - local prefix = ('[%s] -> '):format(GetInvokingResource()) - - while GetGameTimer() - startTime < timeout do + local startTimeMs = GetGameTimer() + while GetGameTimer() - startTimeMs < timeoutMs do local result = conditionFunc() if result then - local elapsedTime = GetGameTimer() - startTime - return true, elapsedTime + return true, result end Wait(0) end if errorMessage then - local formattedErrorMessage = ('%s %s'):format(prefix, errorMessage) - error(formattedErrorMessage) + error(("[%s] -> %s"):format(GetInvokingResource(), errorMessage)) end - return false, timeout -end \ No newline at end of file + return false +end From db68fceccc69f69866e4b316837d1b80e24c15b6 Mon Sep 17 00:00:00 2001 From: Kenshin13 <63159154+Kenshiin13@users.noreply.github.com> Date: Sun, 15 Dec 2024 17:56:12 +0100 Subject: [PATCH 5/5] fix(es_extended/shared/functions): invokingResource not being displayed --- [core]/es_extended/shared/functions.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/[core]/es_extended/shared/functions.lua b/[core]/es_extended/shared/functions.lua index 52e3030ae..184def1bd 100644 --- a/[core]/es_extended/shared/functions.lua +++ b/[core]/es_extended/shared/functions.lua @@ -192,6 +192,7 @@ function ESX.Await(conditionFunc, errorMessage, timeoutMs) ESX.AssertType(errorMessage, "string", "errorMessage should be a string.") end + local invokingResource = GetInvokingResource() local startTimeMs = GetGameTimer() while GetGameTimer() - startTimeMs < timeoutMs do local result = conditionFunc() @@ -204,7 +205,7 @@ function ESX.Await(conditionFunc, errorMessage, timeoutMs) end if errorMessage then - error(("[%s] -> %s"):format(GetInvokingResource(), errorMessage)) + error(("[%s] -> %s"):format(invokingResource, errorMessage)) end return false