Skip to content

Commit d197fe3

Browse files
committed
Initial commit to asynchronously sort the gem list
1 parent 4738c5d commit d197fe3

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

‎src/Classes/GemSelectControl.lua‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,24 @@ function GemSelectClass:BuildList(buf)
245245
end
246246

247247
function GemSelectClass:UpdateSortCache()
248-
--local start = GetTime()
248+
if self.sortCacheBuilding then
249+
if coroutine.status(self.sortCacheBuilding) == "dead" then
250+
self.sortCacheBuilding = nil
251+
return
252+
end
253+
local res, errMsg = coroutine.resume(self.sortCacheBuilding, self)
254+
if launch.devMode and not res then
255+
error(errMsg)
256+
end
257+
else
258+
self.sortCacheBuilding = coroutine.create(self.UpdateSortCacheCoroutine)
259+
coroutine.resume(self.sortCacheBuilding, self)
260+
end
261+
end
262+
263+
function GemSelectClass:UpdateSortCacheCoroutine()
264+
local start = GetTime()
265+
local totalTimeStart = GetTime()
249266
local sortCache = self.sortCache
250267
local sameSortBy = self.sortGemsBy == self.lastSortGemsBy
251268
-- Don't update the cache if no settings have changed that would impact the ordering
@@ -333,6 +350,9 @@ function GemSelectClass:UpdateSortCache()
333350
-- Check for nil because some fields may not be populated, default to 0
334351
local baseDPS = (dpsField == "FullDPS" and calcBase[dpsField] ~= nil and calcBase[dpsField]) or (calcBase.Minion and calcBase.Minion.CombinedDPS) or (calcBase[dpsField] ~= nil and calcBase[dpsField]) or 0
335352

353+
if coroutine.running() then
354+
coroutine.yield()
355+
end
336356
for gemId, gemData in pairs(self.gems) do
337357
sortCache.dps[gemId] = baseDPS
338358
-- Ignore gems that don't support the active skill
@@ -349,6 +369,10 @@ function GemSelectClass:UpdateSortCache()
349369
else
350370
sortCache.dpsColor[gemId] = "^xFFFF66"
351371
end
372+
if coroutine.running() and GetTime() - start > 100 then
373+
coroutine.yield()
374+
start = GetTime()
375+
end
352376
end
353377

354378
--ConPrintf("Gem Selector time: %d ms", GetTime() - start)
@@ -359,7 +383,13 @@ function GemSelectClass:SortGemList(gemList)
359383
t_sort(gemList, function(a, b)
360384
if sortCache.canSupport[a] == sortCache.canSupport[b] then
361385
if self.skillsTab.sortGemsByDPS and sortCache.dps[a] ~= sortCache.dps[b] then
362-
return sortCache.dps[a] > sortCache.dps[b]
386+
if sortCache.dps[a] and not sortCache.dps[b] then
387+
return true
388+
elseif sortCache.dps[b] and not sortCache.dps[a] then
389+
return false
390+
else
391+
return sortCache.dps[a] > sortCache.dps[b]
392+
end
363393
else
364394
return a < b
365395
end
@@ -441,6 +471,8 @@ function GemSelectClass:Draw(viewPort, noTooltip)
441471
SetViewport(x + 2, y + height + 2, width - 4, dropHeight)
442472
local minIndex = m_floor(scrollBar.offset / 16 + 1)
443473
local maxIndex = m_min(m_floor((scrollBar.offset + dropHeight) / 16 + 1), #self.list)
474+
self:UpdateSortCache()
475+
self:SortGemList(self.list)
444476
for index = minIndex, maxIndex do
445477
local y = (index - 1) * (height - 4) - scrollBar.offset
446478
if index == self.hoverSel or index == self.selIndex or (index == 1 and self.selIndex == 0) then
@@ -464,7 +496,7 @@ function GemSelectClass:Draw(viewPort, noTooltip)
464496
gemText = altQualMap[self:GetQualityType(gemId)] .. gemText
465497
end
466498
DrawString(0, y, "LEFT", height - 4, "VAR", gemText)
467-
if gemData then
499+
if gemData and self.sortCache.dpsColor[gemId] then
468500
if gemData.grantedEffect.support and self.sortCache.canSupport[gemId] then
469501
SetDrawColor(self.sortCache.dpsColor[gemId])
470502
main:DrawCheckMark(width - 4 - height / 2 - (scrollBar.enabled and 18 or 0), y + (height - 4) / 2, (height - 4) * 0.8)

0 commit comments

Comments
 (0)