diff --git a/lua/octo/reviews/file-entry.lua b/lua/octo/reviews/file-entry.lua index 4acc6485..a04957ac 100644 --- a/lua/octo/reviews/file-entry.lua +++ b/lua/octo/reviews/file-entry.lua @@ -42,6 +42,8 @@ M._null_buffer = {} ---@field right_winid? integer ---@field left_comment_ranges? [integer, integer][] ---@field right_comment_ranges? [integer, integer][] +---@field left_fetching boolean +---@field right_fetching boolean ---@field associated_bufs integer[] ---@field diffhunks? string[] ---@field viewed_state ViewedState @@ -103,6 +105,8 @@ function FileEntry:new(opt) right_lines = {}, left_fetched = false, right_fetched = false, + left_fetching = false, + right_fetching = false, diffhunks = diffhunks, associated_bufs = {}, viewed_state = pr.files[opt.path], @@ -209,18 +213,29 @@ function FileEntry:get_buf(split) end ---Fetch file content locally or from GitHub. -function FileEntry:fetch() +---@param sync boolean +function FileEntry:fetch(sync) local right_path = self.path local left_path = self.path local current_review = require("octo.reviews").get_current_review() if not current_review then return end + local conf = config.values + if self.left_fetching or self.right_fetching then + if sync then + vim.wait(conf.timeout, function() + return self:is_ready_to_render() + end) + end + return + end + self.left_fetching = true + self.right_fetching = true local right_sha = current_review.layout.right.commit local left_sha = current_review.layout.left.commit local right_abbrev = current_review.layout.right:abbrev() local left_abbrev = current_review.layout.left:abbrev() - local conf = config.values -- handle renamed files if self.status == "R" and self.previous_path then @@ -232,11 +247,13 @@ function FileEntry:fetch() utils.get_file_at_commit(right_path, right_sha, function(lines) self.right_lines = lines self.right_fetched = true + self.right_fetching = false end) else utils.get_file_contents(self.pull_request.repo, right_abbrev, right_path, function(lines) self.right_lines = lines self.right_fetched = true + self.right_fetching = false end) end @@ -245,18 +262,22 @@ function FileEntry:fetch() utils.get_file_at_commit(left_path, left_sha, function(lines) self.left_lines = lines self.left_fetched = true + self.left_fetching = false end) else utils.get_file_contents(self.pull_request.repo, left_abbrev, left_path, function(lines) self.left_lines = lines self.left_fetched = true + self.left_fetching = false end) end -- wait until we have both versions - return vim.wait(conf.timeout, function() - return self:is_ready_to_render() - end) + if sync then + vim.wait(conf.timeout, function() + return self:is_ready_to_render() + end) + end end ---Determines whether the file content has been loaded and the file is ready to render diff --git a/lua/octo/reviews/init.lua b/lua/octo/reviews/init.lua index ce4b777e..80b7c4c4 100644 --- a/lua/octo/reviews/init.lua +++ b/lua/octo/reviews/init.lua @@ -175,9 +175,12 @@ function Review:set_files_and_select_first(files) self.layout.files = files if selected_file_idx then - files[selected_file_idx]:fetch() + files[selected_file_idx]:fetch(true) self.layout.selected_file_idx = selected_file_idx end + for _, file in ipairs(files) do + file:fetch(false) + end self.layout:update_files() end diff --git a/lua/octo/reviews/layout.lua b/lua/octo/reviews/layout.lua index 7b72616a..14d9a2be 100644 --- a/lua/octo/reviews/layout.lua +++ b/lua/octo/reviews/layout.lua @@ -121,7 +121,7 @@ function Layout:set_current_file(file, focus) end if found then if not file:is_ready_to_render() then - local result = file:fetch() + local result = file:fetch(true) if not result then utils.print_err("Timeout fetching " .. file.path) return