mirror of
https://github.com/CommunityOx/ox_lib.git
synced 2026-08-17 06:56:03 +01:00
feat(imports): lib.waitFor
This commit is contained in:
@@ -15,21 +15,11 @@ function lib.requestAnimDict(animDict, timeout)
|
|||||||
|
|
||||||
RequestAnimDict(animDict)
|
RequestAnimDict(animDict)
|
||||||
|
|
||||||
if coroutine.isyieldable() then
|
if not coroutine.isyieldable() then return animDict end
|
||||||
timeout = tonumber(timeout) or 500
|
|
||||||
|
|
||||||
for _ = 1, timeout do
|
return lib.waitFor(function()
|
||||||
if HasAnimDictLoaded(animDict) then
|
if HasAnimDictLoaded(animDict) then return animDict end
|
||||||
return animDict
|
end, ("failed to load animDict '%s'"):format(animDict), timeout or 500)
|
||||||
end
|
|
||||||
|
|
||||||
Wait(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
print(("failed to load animDict '%s' after %s ticks"):format(animDict, timeout))
|
|
||||||
end
|
|
||||||
|
|
||||||
return animDict
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.requestAnimDict
|
return lib.requestAnimDict
|
||||||
|
|||||||
@@ -11,21 +11,11 @@ function lib.requestAnimSet(animSet, timeout)
|
|||||||
|
|
||||||
RequestAnimSet(animSet)
|
RequestAnimSet(animSet)
|
||||||
|
|
||||||
if coroutine.isyieldable() then
|
if not coroutine.isyieldable() then return animSet end
|
||||||
timeout = tonumber(timeout) or 500
|
|
||||||
|
|
||||||
for _ = 1, timeout do
|
return lib.waitFor(function()
|
||||||
if HasAnimSetLoaded(animSet) then
|
if HasAnimSetLoaded(animSet) then return animSet end
|
||||||
return animSet
|
end, ("failed to load animSet '%s'"):format(animSet), timeout or 500)
|
||||||
end
|
|
||||||
|
|
||||||
Wait(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
print(("failed to load animSet '%s' after %s ticks"):format(animSet, timeout))
|
|
||||||
end
|
|
||||||
|
|
||||||
return animSet
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.requestAnimSet
|
return lib.requestAnimSet
|
||||||
|
|||||||
@@ -13,21 +13,11 @@ function lib.requestModel(model, timeout)
|
|||||||
|
|
||||||
RequestModel(model)
|
RequestModel(model)
|
||||||
|
|
||||||
if coroutine.isyieldable() then
|
if not coroutine.isyieldable() then return model end
|
||||||
timeout = tonumber(timeout) or 500
|
|
||||||
|
|
||||||
for _ = 1, timeout do
|
return lib.waitFor(function()
|
||||||
if HasModelLoaded(model) then
|
if HasModelLoaded(model) then return model end
|
||||||
return model
|
end, ("failed to load model '%s'"):format(model), timeout or 500)
|
||||||
end
|
|
||||||
|
|
||||||
Wait(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
print(("failed to load model '%s' after %s ticks"):format(model, timeout))
|
|
||||||
end
|
|
||||||
|
|
||||||
return model
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.requestModel
|
return lib.requestModel
|
||||||
|
|||||||
@@ -11,21 +11,11 @@ function lib.requestNamedPtfxAsset(ptFxName, timeout)
|
|||||||
|
|
||||||
RequestNamedPtfxAsset(ptFxName)
|
RequestNamedPtfxAsset(ptFxName)
|
||||||
|
|
||||||
if coroutine.isyieldable() then
|
if not coroutine.isyieldable() then return ptFxName end
|
||||||
timeout = tonumber(timeout) or 500
|
|
||||||
|
|
||||||
for _ = 1, timeout do
|
return lib.waitFor(function()
|
||||||
if HasNamedPtfxAssetLoaded(ptFxName) then
|
if HasNamedPtfxAssetLoaded(ptFxName) then return ptFxName end
|
||||||
return ptFxName
|
end, ("failed to load ptFxName '%s'"):format(ptFxName), timeout or 500)
|
||||||
end
|
|
||||||
|
|
||||||
Wait(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
print(("failed to load ptFxName '%s' after %s ticks"):format(ptFxName, timeout))
|
|
||||||
end
|
|
||||||
|
|
||||||
return ptFxName
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.requestNamedPtfxAsset
|
return lib.requestNamedPtfxAsset
|
||||||
|
|||||||
@@ -9,23 +9,11 @@ function lib.requestScaleformMovie(scaleformName, timeout)
|
|||||||
|
|
||||||
local scaleform = RequestScaleformMovie(scaleformName)
|
local scaleform = RequestScaleformMovie(scaleformName)
|
||||||
|
|
||||||
|
if not coroutine.isyieldable() then return scaleform end
|
||||||
|
|
||||||
|
return lib.waitFor(function()
|
||||||
if HasScaleformMovieLoaded(scaleform) then return scaleform end
|
if HasScaleformMovieLoaded(scaleform) then return scaleform end
|
||||||
|
end, ("failed to load scaleform '%s'"):format(scaleform), timeout or 500)
|
||||||
if coroutine.isyieldable() then
|
|
||||||
timeout = tonumber(timeout) or 500
|
|
||||||
|
|
||||||
for _ = 1, timeout do
|
|
||||||
if HasScaleformMovieLoaded(scaleform) then
|
|
||||||
return scaleform
|
|
||||||
end
|
|
||||||
|
|
||||||
Wait(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
print(("failed to load scaleformName '%s' after %s ticks"):format(scaleformName, timeout))
|
|
||||||
end
|
|
||||||
|
|
||||||
return scaleform
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.requestScaleformMovie
|
return lib.requestScaleformMovie
|
||||||
|
|||||||
@@ -11,21 +11,11 @@ function lib.requestStreamedTextureDict(textureDict, timeout)
|
|||||||
|
|
||||||
RequestStreamedTextureDict(textureDict, false)
|
RequestStreamedTextureDict(textureDict, false)
|
||||||
|
|
||||||
if coroutine.isyieldable() then
|
if not coroutine.isyieldable() then return textureDict end
|
||||||
timeout = tonumber(timeout) or 500
|
|
||||||
|
|
||||||
for _ = 1, timeout do
|
return lib.waitFor(function()
|
||||||
if HasStreamedTextureDictLoaded(textureDict) then
|
if HasStreamedTextureDictLoaded(textureDict) then return textureDict end
|
||||||
return textureDict
|
end, ("failed to load textureDict '%s'"):format(textureDict), timeout or 500)
|
||||||
end
|
|
||||||
|
|
||||||
Wait(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
print(("failed to load textureDict '%s' after %s ticks"):format(textureDict, timeout))
|
|
||||||
end
|
|
||||||
|
|
||||||
return textureDict
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.requestStreamedTextureDict
|
return lib.requestStreamedTextureDict
|
||||||
|
|||||||
@@ -40,21 +40,11 @@ function lib.requestWeaponAsset(weaponType, timeout, weaponResourceFlags, extraW
|
|||||||
|
|
||||||
RequestWeaponAsset(weaponType, weaponResourceFlags or 31, extraWeaponComponentFlags or 0)
|
RequestWeaponAsset(weaponType, weaponResourceFlags or 31, extraWeaponComponentFlags or 0)
|
||||||
|
|
||||||
if coroutine.isyieldable() then
|
if not coroutine.isyieldable() then return weaponType end
|
||||||
timeout = tonumber(timeout) or 500
|
|
||||||
|
|
||||||
for _ = 1, timeout do
|
return lib.waitFor(function()
|
||||||
if HasWeaponAssetLoaded(weaponType) then
|
if HasWeaponAssetLoaded(weaponType) then return weaponType end
|
||||||
return weaponType
|
end, ("failed to load weaponType '%s'"):format(weaponType), timeout or 500)
|
||||||
end
|
|
||||||
|
|
||||||
Wait(0)
|
|
||||||
end
|
|
||||||
|
|
||||||
print(("failed to load weaponType '%s' after %s ticks"):format(weaponType, timeout))
|
|
||||||
end
|
|
||||||
|
|
||||||
return weaponType
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return lib.requestWeaponAsset
|
return lib.requestWeaponAsset
|
||||||
|
|||||||
35
imports/waitFor/shared.lua
Normal file
35
imports/waitFor/shared.lua
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
---Yields the current thread until a non-nil value is returned by the function.
|
||||||
|
---Errors after timeout (default 1000) iterations.
|
||||||
|
---@generic T
|
||||||
|
---@param cb fun(): T?
|
||||||
|
---@param errMessage string?
|
||||||
|
---@param timeout number?
|
||||||
|
---@return T?
|
||||||
|
---@async
|
||||||
|
function lib.waitFor(cb, errMessage, timeout)
|
||||||
|
local value = cb()
|
||||||
|
|
||||||
|
if value ~= nil then return value end
|
||||||
|
|
||||||
|
if timeout or timeout == nil then
|
||||||
|
timeout = tonumber(timeout) or 1000
|
||||||
|
end
|
||||||
|
|
||||||
|
local start = GetGameTimer()
|
||||||
|
local i = 0
|
||||||
|
|
||||||
|
while value == nil do
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
Wait(0)
|
||||||
|
value = cb()
|
||||||
|
end
|
||||||
|
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
|
||||||
|
return lib.waitFor
|
||||||
Reference in New Issue
Block a user