Skip to content

Commit 0bae1a3

Browse files
8veStorysindrets
andauthored
fix: cancel update if view becomes out of focus (#472)
Co-authored-by: Sindre T. Strøm <sindrets@gmail.com>
1 parent 84164f7 commit 0bae1a3

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lua/diffview/scene/views/diff/diff_view.lua

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ DiffView.update_files = debounce.debounce_trailing(
327327
async.wrap(function(self, callback)
328328
await(async.scheduler())
329329

330+
-- Never update unless the view is in focus
331+
if self.tabpage ~= api.nvim_get_current_tabpage() then
332+
callback({ "The update was cancelled." })
333+
return
334+
end
335+
330336
---@type PerfTimer
331337
local perf = PerfTimer("[DiffView] Status Update")
332338
self:ensure_layout()
@@ -344,19 +350,24 @@ DiffView.update_files = debounce.debounce_trailing(
344350
end
345351

346352
local index_stat = pl:stat(pl:join(self.adapter.ctx.dir, "index"))
347-
local last_winid = api.nvim_get_current_win()
348353

349354
---@type string[]?, FileDict
350355
local err, new_files = await(self:get_updated_files())
356+
await(async.scheduler())
351357

352358
if err then
353359
utils.err("Failed to update files in a diff view!", true)
354360
logger:error("[DiffView] Failed to update files!")
355-
await(async.scheduler())
356361
callback(err)
357362
return
358363
end
359364

365+
-- Stop the update if the view is no longer in focus.
366+
if self.tabpage ~= api.nvim_get_current_tabpage() then
367+
callback({ "The update was cancelled." })
368+
return
369+
end
370+
360371
perf:lap("received new file list")
361372

362373
local files = {
@@ -365,8 +376,6 @@ DiffView.update_files = debounce.debounce_trailing(
365376
{ cur_files = self.files.staged, new_files = new_files.staged },
366377
}
367378

368-
await(async.scheduler())
369-
370379
for _, v in ipairs(files) do
371380
-- We diff the old file list against the new file list in order to find
372381
-- the most efficient way to morph the current list into the new. This
@@ -473,10 +482,6 @@ DiffView.update_files = debounce.debounce_trailing(
473482
end
474483
self:set_file(self.panel.cur_file or self.panel:next_file(), false, not self.initialized)
475484

476-
if api.nvim_win_is_valid(last_winid) then
477-
api.nvim_set_current_win(last_winid)
478-
end
479-
480485
self.update_needed = false
481486
perf:time()
482487
logger:lvl(5):debug(perf)

0 commit comments

Comments
 (0)