Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es_extended) Add ESX.Await function #1540

Merged
merged 5 commits into from
Dec 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion [core]/es_extended/shared/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
end

---@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

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.")
end

local invokingResource = GetInvokingResource()
local startTimeMs = GetGameTimer()
while GetGameTimer() - startTimeMs < timeoutMs do
local result = conditionFunc()

if result then
return true, result
end

Wait(0)
end

if errorMessage then
error(("[%s] -> %s"):format(invokingResource, errorMessage))
end

return false
end
Loading