feat(imports): lib.waitFor

This commit is contained in:
Linden
2023-07-31 16:06:44 +10:00
parent c6742eeae9
commit f11668b95c
8 changed files with 63 additions and 100 deletions

View File

@@ -15,21 +15,11 @@ function lib.requestAnimDict(animDict, timeout)
RequestAnimDict(animDict)
if coroutine.isyieldable() then
timeout = tonumber(timeout) or 500
if not coroutine.isyieldable() then return animDict end
for _ = 1, timeout do
if HasAnimDictLoaded(animDict) then
return animDict
end
Wait(0)
end
print(("failed to load animDict '%s' after %s ticks"):format(animDict, timeout))
end
return animDict
return lib.waitFor(function()
if HasAnimDictLoaded(animDict) then return animDict end
end, ("failed to load animDict '%s'"):format(animDict), timeout or 500)
end
return lib.requestAnimDict

View File

@@ -11,21 +11,11 @@ function lib.requestAnimSet(animSet, timeout)
RequestAnimSet(animSet)
if coroutine.isyieldable() then
timeout = tonumber(timeout) or 500
if not coroutine.isyieldable() then return animSet end
for _ = 1, timeout do
if HasAnimSetLoaded(animSet) then
return animSet
end
Wait(0)
end
print(("failed to load animSet '%s' after %s ticks"):format(animSet, timeout))
end
return animSet
return lib.waitFor(function()
if HasAnimSetLoaded(animSet) then return animSet end
end, ("failed to load animSet '%s'"):format(animSet), timeout or 500)
end
return lib.requestAnimSet

View File

@@ -13,21 +13,11 @@ function lib.requestModel(model, timeout)
RequestModel(model)
if coroutine.isyieldable() then
timeout = tonumber(timeout) or 500
if not coroutine.isyieldable() then return model end
for _ = 1, timeout do
if HasModelLoaded(model) then
return model
end
Wait(0)
end
print(("failed to load model '%s' after %s ticks"):format(model, timeout))
end
return model
return lib.waitFor(function()
if HasModelLoaded(model) then return model end
end, ("failed to load model '%s'"):format(model), timeout or 500)
end
return lib.requestModel

View File

@@ -11,21 +11,11 @@ function lib.requestNamedPtfxAsset(ptFxName, timeout)
RequestNamedPtfxAsset(ptFxName)
if coroutine.isyieldable() then
timeout = tonumber(timeout) or 500
if not coroutine.isyieldable() then return ptFxName end
for _ = 1, timeout do
if HasNamedPtfxAssetLoaded(ptFxName) then
return ptFxName
end
Wait(0)
end
print(("failed to load ptFxName '%s' after %s ticks"):format(ptFxName, timeout))
end
return ptFxName
return lib.waitFor(function()
if HasNamedPtfxAssetLoaded(ptFxName) then return ptFxName end
end, ("failed to load ptFxName '%s'"):format(ptFxName), timeout or 500)
end
return lib.requestNamedPtfxAsset

View File

@@ -9,23 +9,11 @@ function lib.requestScaleformMovie(scaleformName, timeout)
local scaleform = RequestScaleformMovie(scaleformName)
if not coroutine.isyieldable() then return scaleform end
return lib.waitFor(function()
if HasScaleformMovieLoaded(scaleform) then return scaleform end
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, ("failed to load scaleform '%s'"):format(scaleform), timeout or 500)
end
return lib.requestScaleformMovie

View File

@@ -11,21 +11,11 @@ function lib.requestStreamedTextureDict(textureDict, timeout)
RequestStreamedTextureDict(textureDict, false)
if coroutine.isyieldable() then
timeout = tonumber(timeout) or 500
if not coroutine.isyieldable() then return textureDict end
for _ = 1, timeout do
if HasStreamedTextureDictLoaded(textureDict) then
return textureDict
end
Wait(0)
end
print(("failed to load textureDict '%s' after %s ticks"):format(textureDict, timeout))
end
return textureDict
return lib.waitFor(function()
if HasStreamedTextureDictLoaded(textureDict) then return textureDict end
end, ("failed to load textureDict '%s'"):format(textureDict), timeout or 500)
end
return lib.requestStreamedTextureDict

View File

@@ -40,21 +40,11 @@ function lib.requestWeaponAsset(weaponType, timeout, weaponResourceFlags, extraW
RequestWeaponAsset(weaponType, weaponResourceFlags or 31, extraWeaponComponentFlags or 0)
if coroutine.isyieldable() then
timeout = tonumber(timeout) or 500
if not coroutine.isyieldable() then return weaponType end
for _ = 1, timeout do
if HasWeaponAssetLoaded(weaponType) then
return weaponType
end
Wait(0)
end
print(("failed to load weaponType '%s' after %s ticks"):format(weaponType, timeout))
end
return weaponType
return lib.waitFor(function()
if HasWeaponAssetLoaded(weaponType) then return weaponType end
end, ("failed to load weaponType '%s'"):format(weaponType), timeout or 500)
end
return lib.requestWeaponAsset

View 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