From 6e65b2a020925f868d1dcde9b94df1cc39e5c95c Mon Sep 17 00:00:00 2001 From: Dusk-92 Date: Sat, 5 Sep 2026 16:20:16 +0200 Subject: [PATCH] fix: invalidate inventory index on database mutations --- Core/InventoryIndexSafety.lua | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 Core/InventoryIndexSafety.lua diff --git a/Core/InventoryIndexSafety.lua b/Core/InventoryIndexSafety.lua new file mode 100644 index 0000000..e8efe2d --- /dev/null +++ b/Core/InventoryIndexSafety.lua @@ -0,0 +1,86 @@ +-- Guda ClassicAPI InventoryIndex mutation hooks +-- Ensures the O(1) tooltip index never stays stale after SavedVariables or +-- cross-character data changes that do not necessarily emit a fresh bag event. +-- Lua 5.0 compatible. + +local addon = Guda +local Index = addon.Modules.InventoryIndex +local DB = addon.Modules.DB +if not Index or not DB then return end + +local oldSaveBags = DB.SaveBags +local oldSaveBank = DB.SaveBank +local oldSaveEquipment = DB.SaveEquipment +local oldSaveMailbox = DB.SaveMailbox +local oldAddMailToCharacter = DB.AddMailToCharacter +local oldToggleGoldBlacklist = DB.ToggleGoldBlacklist +local oldRemoveCharacter = DB.RemoveCharacter +local oldCleanupOldCharacters = DB.CleanupOldCharacters + +if oldSaveBags then + function DB:SaveBags(bagData) + oldSaveBags(self, bagData) + Index:InvalidateCurrent() + end +end + +if oldSaveBank then + function DB:SaveBank(bankData) + oldSaveBank(self, bankData) + Index:InvalidateCurrent() + end +end + +if oldSaveEquipment then + function DB:SaveEquipment(equipmentData) + oldSaveEquipment(self, equipmentData) + Index:InvalidateCurrent() + end +end + +if oldSaveMailbox then + function DB:SaveMailbox(mailboxData) + oldSaveMailbox(self, mailboxData) + -- MAIL_INBOX_UPDATE can fire before MailboxScanner's debounced save. + -- Re-invalidating here guarantees the next tooltip sees the persisted + -- post-update mailbox snapshot rather than the pre-save one. + Index:InvalidateCurrent() + end +end + +if oldAddMailToCharacter then + function DB:AddMailToCharacter(name, realm, mailRow) + local added = oldAddMailToCharacter(self, name, realm, mailRow) + if added then + -- This can mutate a different local character's mailbox. + Index:InvalidateAll() + end + return added + end +end + +if oldToggleGoldBlacklist then + function DB:ToggleGoldBlacklist(fullName) + oldToggleGoldBlacklist(self, fullName) + Index:InvalidateAll() + end +end + +if oldRemoveCharacter then + function DB:RemoveCharacter(fullName) + local removed = oldRemoveCharacter(self, fullName) + if removed then + Index:InvalidateAll() + end + return removed + end +end + +if oldCleanupOldCharacters then + function DB:CleanupOldCharacters() + oldCleanupOldCharacters(self) + Index:InvalidateAll() + end +end + +addon:Debug("ClassicAPI inventory index mutation safety enabled")