---@class jjannotate.Cache ---@field wins table win id -> Window ---@field win_by_revision table>> root -> path -> change id -> Window local Cache = {} Cache.__index = Cache ---@return jjannotate.Cache function Cache.new() ---@type jjannotate.Cache local c = { wins = {}, win_by_revision = {}, } return setmetatable(c, Cache) end ---@param revision jjannotate.RevisionRef ---@return jjannotate.Window? function Cache:get_by_revision(revision) return self.win_by_revision[revision.repo] and self.win_by_revision[revision.repo][revision.path] and self.win_by_revision[revision.repo][revision.path][revision.change_id] end ---@param win_id integer ---@return jjannotate.Window? function Cache:get_by_id(win_id) return self.wins[win_id] end ---@param revision jjannotate.RevisionRef ---@param w jjannotate.Window function Cache:set_win(revision, w) self.wins[w.id] = w if not self.win_by_revision[revision.repo] then self.win_by_revision[revision.repo] = {} end if not self.win_by_revision[revision.repo][revision.path] then self.win_by_revision[revision.repo][revision.path] = {} end self.win_by_revision[revision.repo][revision.path][revision.change_id] = w end ---@param win_id integer ---@return boolean function Cache:del_win(win_id) local w = self:get_by_id(win_id) if not w then return false end self.wins[win_id] = nil local repo, path, change_id = w.revision.repo, w.revision.path, w.revision.change_id local by_path = self.win_by_revision[repo][path] by_path[change_id] = nil if vim.tbl_isempty(by_path) then self.win_by_revision[repo][path] = nil if vim.tbl_isempty(self.win_by_revision[repo]) then self.win_by_revision[repo] = nil end end return true end return Cache.new()