Skip to content

Commit

Permalink
refactor(client/functions): ESX.SetTimeout and ESX.ClearTimeout
Browse files Browse the repository at this point in the history
timeout functions moved to shared file and removed thread from client side
  • Loading branch information
CsokiHUN committed Jan 22, 2023
1 parent ecb975f commit 5450414
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 58 deletions.
33 changes: 1 addition & 32 deletions [core]/es_extended/client/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ ESX.PlayerData = {}
ESX.PlayerLoaded = false
Core.CurrentRequestId = 0
Core.ServerCallbacks = {}
Core.TimeoutCallbacks = {}
Core.Input = {}
ESX.UI = {}
ESX.UI.HUD = {}
Expand All @@ -21,18 +20,6 @@ ESX.Scaleform.Utils = {}

ESX.Streaming = {}

function ESX.SetTimeout(msec, cb)
table.insert(Core.TimeoutCallbacks, {
time = GetGameTimer() + msec,
cb = cb
})
return #Core.TimeoutCallbacks
end

function ESX.ClearTimeout(i)
Core.TimeoutCallbacks[i] = nil
end

function ESX.IsPlayerLoaded()
return ESX.PlayerLoaded
end
Expand Down Expand Up @@ -1392,22 +1379,4 @@ AddEventHandler('esx:showAdvancedNotification',
RegisterNetEvent('esx:showHelpNotification')
AddEventHandler('esx:showHelpNotification', function(msg, thisFrame, beep, duration)
ESX.ShowHelpNotification(msg, thisFrame, beep, duration)
end)

-- SetTimeout
CreateThread(function()
while true do
local sleep = 100
if #Core.TimeoutCallbacks > 0 then
local currTime = GetGameTimer()
sleep = 0
for i = 1, #Core.TimeoutCallbacks, 1 do
if currTime >= Core.TimeoutCallbacks[i].time then
Core.TimeoutCallbacks[i].cb()
Core.TimeoutCallbacks[i] = nil
end
end
end
Wait(sleep)
end
end)
end)
23 changes: 23 additions & 0 deletions [core]/es_extended/common/modules/timeout.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local TimeoutCount = 0
local CancelledTimeouts = {}

ESX.SetTimeout = function(msec, cb)
local id <const> = TimeoutCount + 1

SetTimeout(msec, function()
if CancelledTimeouts[id] then
CancelledTimeouts[id] = nil
return
end

cb()
end)

TimeoutCount = id

return id
end

ESX.ClearTimeout = function(id)
CancelledTimeouts[id] = true
end
6 changes: 2 additions & 4 deletions [core]/es_extended/fxmanifest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ server_scripts {
'server/main.lua',
'server/commands.lua',

'common/modules/math.lua',
'common/modules/table.lua',
'common/modules/*.lua',
'common/functions.lua'
}

Expand All @@ -42,8 +41,7 @@ client_scripts {
'client/modules/scaleform.lua',
'client/modules/streaming.lua',

'common/modules/math.lua',
'common/modules/table.lua',
'common/modules/*.lua',
'common/functions.lua'
}

Expand Down
2 changes: 0 additions & 2 deletions [core]/es_extended/server/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Core.UsableItemsCallbacks = {}
Core.ServerCallbacks = {}
Core.ClientCallbacks = {}
Core.CurrentRequestId = 0
Core.TimeoutCount = -1
Core.CancelledTimeouts = {}
Core.RegisteredCommands = {}
Core.Pickups = {}
Core.PickupId = 0
Expand Down
20 changes: 0 additions & 20 deletions [core]/es_extended/server/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ function ESX.Trace(msg)
end
end

function ESX.SetTimeout(msec, cb)
local id = Core.TimeoutCount + 1

SetTimeout(msec, function()
if Core.CancelledTimeouts[id] then
Core.CancelledTimeouts[id] = nil
else
cb()
end
end)

Core.TimeoutCount = id

return id
end

function ESX.RegisterCommand(name, group, cb, allowConsole, suggestion)
if type(name) == 'table' then
for k, v in ipairs(name) do
Expand Down Expand Up @@ -159,10 +143,6 @@ function ESX.RegisterCommand(name, group, cb, allowConsole, suggestion)
end
end

function ESX.ClearTimeout(id)
Core.CancelledTimeouts[id] = true
end

function ESX.RegisterServerCallback(name, cb)
Core.ServerCallbacks[name] = cb
end
Expand Down

0 comments on commit 5450414

Please sign in to comment.