From cedb807e89dde4424c3a54a9635e5c38f38c1a5a Mon Sep 17 00:00:00 2001 From: Anavrins Date: Sat, 18 Apr 2020 19:06:50 -0400 Subject: [PATCH] Add throttling to defragger --- milo/apis/storage.lua | 27 +++++++++++++++------------ milo/core/listing.lua | 11 ++++++++++- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/milo/apis/storage.lua b/milo/apis/storage.lua index d6c8df1..c41ca8b 100644 --- a/milo/apis/storage.lua +++ b/milo/apis/storage.lua @@ -293,30 +293,33 @@ end -- provide a raw list of all the items in all storage chests -- it might be beneficial to move this to the adapter class at some point and cache the raw item list in this class at some point -function Storage:listItemsRaw() +function Storage:listItemsRaw(throttle) local res = {} - for _, v in pairs(self.nodes) do - if v.category == "storage" then - local chest = device[v.name] + throttle = throttle or Util.throttle() + + for _, v in pairs(self.nodes) do + if v.category == "storage" then + local chest = device[v.name] local items = chest.list() for slot, item in pairs(items) do items[slot] = itemDB:get(item, function() return chest.getItemMeta(slot) end) end - res[v.name] = items - end - end + res[v.name] = items + throttle() + end + end - return res + return res end -- provide a list of items and which chests provide them -function Storage:listProviders() +function Storage:listProviders(throttle) local res = {} - local rawItems = self:listItemsRaw() + local rawItems = self:listItemsRaw(throttle) for chest, items in pairs(rawItems) do for slot, item in pairs(items) do @@ -331,8 +334,8 @@ function Storage:listProviders() end -- defrags the storage system -function Storage:defrag() - local items = self:listProviders() +function Storage:defrag(throttle) + local items = self:listProviders(throttle) for _, providers in pairs(items) do table.sort(providers, function(a, b) diff --git a/milo/core/listing.lua b/milo/core/listing.lua index 26933e4..afd359c 100644 --- a/milo/core/listing.lua +++ b/milo/core/listing.lua @@ -256,7 +256,8 @@ function page:eventHandler(event) self:setFocus(self.statusBar.filter) elseif event.type == 'defrag' then - context.storage:defrag() + self:defrag() + self:refresh(true) elseif event.type == 'toggle_display' then displayMode = (displayMode + 1) % 2 @@ -365,6 +366,14 @@ function page:refresh(force) self.throttle:disable() end +function page:defrag() + local throttle = function() self.throttle:update() end + + self.throttle:enable() + context.storage:defrag(throttle) + self.throttle:disable() +end + function page:applyFilter() local function filterItems(t, filter) self.grid.sortColumn = Milo:getState('sortColumn') or 'count'