How can I optimize these 2 functions inside my module script?
Currently it takes like 1 minute just for getAllAwaitingResultData function to run. And that is considering there is only 3 keys stored in the datastore, but I plan on having hundreds in the future.
local getAsyncCooldown = 2
local set_remove_AsyncCooldown = 2
local listKeyAsyncCooldown = 10
local advanceToNextPageAsyncCooldown = 10
local lastGetTime = os.time()
local lastSetRemoveTime = os.time()
local lastListTime = os.time()
local lastAdvanceTime = os.time()
function systemManager.getAllAwaitingResultData()
repeat
task.wait(0.1)
until os.time()-lastListTime > listKeyAsyncCooldown
lastListTime = os.time()
local dataDictionary = {}
local listSuccess, pages = pcall(function()
return awaitingResult:ListKeysAsync()
end)
if listSuccess then
while true do
local items = pages:GetCurrentPage()
coroutine.wrap(function()
for _, v in ipairs(items) do
repeat
task.wait(0.1)
until os.time()-lastGetTime > getAsyncCooldown
lastGetTime = os.time()
local value = awaitingResult:GetAsync(v.KeyName)
dataDictionary[v.KeyName] = value
end
end)()
if pages.IsFinished then
break
end
repeat
task.wait(0.1)
until os.time()-lastAdvanceTime > advanceToNextPageAsyncCooldown
lastAdvanceTime = os.time()
pages:AdvanceToNextPageAsync()
end
end
return dataDictionary
end
function systemManager.getAllAppResultData()
repeat
task.wait(0.1)
until os.time()-lastListTime > listKeyAsyncCooldown
lastListTime = os.time()
local dataDictionary = {}
local listSuccess, pages = pcall(function()
return appResult:ListKeysAsync()
end)
if listSuccess then
while true do
local items = pages:GetCurrentPage()
coroutine.wrap(function()
for _, v in ipairs(items) do
-- Take current key + its associated value and write it into the dictionary.
repeat
task.wait(0.1)
until os.time()-lastGetTime > getAsyncCooldown
lastGetTime = os.time()
local value = appResult:GetAsync(v.KeyName)
dataDictionary[v.KeyName] = value
end
end)()
if pages.IsFinished then
break
end
repeat
task.wait(0.1)
until os.time()-lastAdvanceTime > advanceToNextPageAsyncCooldown
lastAdvanceTime = os.time()
pages:AdvanceToNextPageAsync()
end
end
return dataDictionary
end