Skip to content

Commit

Permalink
Addressed PR feedback from Kenshiin13
Browse files Browse the repository at this point in the history
  • Loading branch information
Zykem committed Dec 15, 2024
1 parent 276fc69 commit c32c41d
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions [core]/es_extended/shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,31 @@ 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())

while GetGameTimer() - startTime < timeout do
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
Expand All @@ -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
Expand Down

0 comments on commit c32c41d

Please sign in to comment.