mirror of
https://github.com/esx-framework/esx_core.git
synced 2026-08-28 17:01:14 +00:00
Moved Timeouts and Await to esx_lib
This commit is contained in:
@@ -6,3 +6,6 @@ ESX.Streaming.RequestNamedPtfxAsset = xLib.streaming.requestNamedPtfxAsset
|
||||
ESX.Streaming.RequestAnimSet = xLib.streaming.requestAnimSet
|
||||
ESX.Streaming.RequestAnimDict = xLib.streaming.requestAnimDict
|
||||
ESX.Streaming.RequestWeaponAsset = xLib.streaming.requestWeaponAsset
|
||||
ESX.SetTimeout = xLib.timeout.setTimeout
|
||||
ESX.ClearTimeout = xLib.timeout.clearTimeout
|
||||
ESX.Await = xLib.waitFor
|
||||
@@ -1,2 +1,6 @@
|
||||
--[[ All server-side functions outsourced from the Core to the lib will be stored here for compatability, e.g:
|
||||
--]]
|
||||
|
||||
ESX.SetTimeout = xLib.timeout.setTimeout
|
||||
ESX.ClearTimeout = xLib.timeout.clearTimeout
|
||||
ESX.Await = xLib.waitFor
|
||||
@@ -178,45 +178,6 @@ function ESX.IsFunctionReference(val)
|
||||
return typeVal == "function" or (typeVal == "table" and type(getmetatable(val)?.__call) == "function")
|
||||
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
|
||||
|
||||
---@param str string
|
||||
---@param allowDigits boolean? Allow numbers if necessary
|
||||
---@return boolean
|
||||
|
||||
+6
-3
@@ -1,10 +1,12 @@
|
||||
xLib.timeout = {}
|
||||
|
||||
local TimeoutCount = 0
|
||||
local CancelledTimeouts = {}
|
||||
|
||||
---@param msec number
|
||||
---@param cb function
|
||||
---@return number
|
||||
ESX.SetTimeout = function(msec, cb)
|
||||
xLib.timeout.setTimeout = function(msec, cb)
|
||||
local id <const> = TimeoutCount + 1
|
||||
|
||||
SetTimeout(msec, function()
|
||||
@@ -12,7 +14,6 @@ ESX.SetTimeout = function(msec, cb)
|
||||
CancelledTimeouts[id] = nil
|
||||
return
|
||||
end
|
||||
|
||||
cb()
|
||||
end)
|
||||
|
||||
@@ -23,6 +24,8 @@ end
|
||||
|
||||
---@param id number
|
||||
---@return nil
|
||||
ESX.ClearTimeout = function(id)
|
||||
xLib.timeout.clearTimeout = function(id)
|
||||
CancelledTimeouts[id] = true
|
||||
end
|
||||
|
||||
return xLib.timeout
|
||||
@@ -0,0 +1,41 @@
|
||||
|
||||
---@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.
|
||||
xLib.waitFor = function(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
|
||||
|
||||
return xLib.waitFor
|
||||
Reference in New Issue
Block a user