Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion lua/vgit/core/loop.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ function loop.free_textlock(times)
return loop
end

-- Registry to track debounced handlers and their cleanup functions
-- Uses weak keys so handlers can be garbage collected when no longer referenced
local debounced_registry = setmetatable({}, { __mode = 'k' })

function loop.debounce(fn, ms)
local timer = vim.loop.new_timer()
local closed = false

return function(...)
local debounced = function(...)
if closed then return end
local argv = { ... }
local argc = select('#', ...)

Expand All @@ -28,6 +34,37 @@ function loop.debounce(fn, ms)
fn(unpack(argv, 1, argc))
end)
end

-- Store cleanup function in registry keyed by the debounced function
debounced_registry[debounced] = function()
if not closed and timer and not timer:is_closing() then
timer:stop()
timer:close()
closed = true
timer = nil
end
end

return debounced
end

-- Close a debounced function's timer handle
function loop.close_debounced(debounced_fn)
local close_fn = debounced_registry[debounced_fn]
if close_fn then
close_fn()
debounced_registry[debounced_fn] = nil
end
end

-- Close all debounced handlers in a table
function loop.close_debounced_handlers(handlers)
if not handlers then return end
for _, handler in pairs(handlers) do
if type(handler) == 'function' then
loop.close_debounced(handler)
end
end
end

function loop.debounce_coroutine(fn, ms)
Expand Down
64 changes: 41 additions & 23 deletions lua/vgit/features/screens/DiffScreen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function DiffScreen:constructor(opts)
model = model,
diff_view = DiffScreen:create_diff_view(scene, model),
app_bar_view = DiffScreen:create_app_bar_view(scene, model),
keymaps = {}, -- Store debounced keymap handlers for cleanup
}
end

Expand Down Expand Up @@ -271,60 +272,73 @@ end
function DiffScreen:setup_keymaps(buffer)
local keymaps = diff_preview_setting:get('keymaps')

-- Create debounced handlers and store them for cleanup
local handlers = {
reset = loop.debounce_coroutine(function()
self:reset(buffer)
end, 100),
stage = loop.debounce_coroutine(function()
self:stage(buffer)
self:toggle_view(buffer)
end, 100),
unstage = loop.debounce_coroutine(function()
self:unstage(buffer)
self:toggle_view(buffer)
end, 100),
hunk_stage = loop.debounce_coroutine(function()
self:stage_hunk(buffer)
end, 100),
hunk_unstage = loop.debounce_coroutine(function()
self:unstage_hunk(buffer)
end, 100),
enter = loop.debounce_coroutine(function()
self:enter_view()
end, 100),
toggle = loop.debounce_coroutine(function()
self:toggle_view(buffer)
end, 100),
}

self.keymaps = handlers

self.diff_view:set_keymap({
{
mode = 'n',
mapping = keymaps.reset,
handler = loop.debounce_coroutine(function()
self:reset(buffer)
end, 100),
handler = handlers.reset,
},
{
mode = 'n',
mapping = keymaps.buffer_stage,
handler = loop.debounce_coroutine(function()
self:stage(buffer)
self:toggle_view(buffer)
end, 100),
handler = handlers.stage,
},
{
mode = 'n',
mapping = keymaps.buffer_unstage,
handler = loop.debounce_coroutine(function()
self:unstage(buffer)
self:toggle_view(buffer)
end, 100),
handler = handlers.unstage,
},
{
mode = 'n',
mapping = keymaps.buffer_hunk_stage,
handler = loop.debounce_coroutine(function()
self:stage_hunk(buffer)
end, 100),
handler = handlers.hunk_stage,
},
{
mode = 'n',
mapping = keymaps.buffer_hunk_unstage,
handler = loop.debounce_coroutine(function()
self:unstage_hunk(buffer)
end, 100),
handler = handlers.hunk_unstage,
},
{
mode = 'n',
mapping = {
key = '<enter>',
desc = 'Open buffer',
},
handler = loop.debounce_coroutine(function()
self:enter_view()
end, 100),
handler = handlers.enter,
},
{
mode = 'n',
mapping = keymaps.toggle_view,
handler = loop.debounce_coroutine(function()
self:toggle_view(buffer)
end, 100),
handler = handlers.toggle,
},
})
end
Expand Down Expand Up @@ -365,6 +379,10 @@ function DiffScreen:create(opts)
end

function DiffScreen:destroy()
-- Clean up timer handles from debounced keymap handlers
loop.close_debounced_handlers(self.keymaps)
self.keymaps = {}

self.scene:destroy()
end

Expand Down
81 changes: 51 additions & 30 deletions lua/vgit/features/screens/ProjectDiffScreen/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function ProjectDiffScreen:constructor(opts)
name = 'Project Diff Screen',
scene = scene,
model = model,
diff_keymaps = {}, -- Store debounced diff keymap handlers for cleanup
app_bar_view = KeyHelpBarView(scene, {
keymaps = function()
local keymaps = project_diff_preview_setting:get('keymaps')
Expand Down Expand Up @@ -417,79 +418,95 @@ end
function ProjectDiffScreen:setup_diff_keymaps()
local keymaps = project_diff_preview_setting:get('keymaps')

-- Create debounced handlers and store them for cleanup
local handlers = {
hunk_stage = loop.debounce_coroutine(function()
self:stage_hunk()
end, 15),
hunk_unstage = loop.debounce_coroutine(function()
self:unstage_hunk()
end, 15),
reset = loop.debounce_coroutine(function()
self:reset_file()
end, 15),
stage = loop.debounce_coroutine(function()
self:stage_file()
end, 15),
unstage = loop.debounce_coroutine(function()
self:unstage_file()
end, 15),
stage_all = loop.debounce_coroutine(function()
self:stage_all()
end, 15),
unstage_all = loop.debounce_coroutine(function()
self:unstage_all()
end, 15),
reset_all = loop.debounce_coroutine(function()
self:reset_all()
end, 15),
commit = loop.debounce_coroutine(function()
self:commit()
end, 15),
enter = loop.coroutine(function()
self:enter_view()
end),
}

self.diff_keymaps = handlers

self.diff_view:set_keymap({
{
mode = 'n',
mapping = keymaps.buffer_hunk_stage,
handler = loop.debounce_coroutine(function()
self:stage_hunk()
end, 15),
handler = handlers.hunk_stage,
},
{
mode = 'n',
mapping = keymaps.buffer_hunk_unstage,
handler = loop.debounce_coroutine(function()
self:unstage_hunk()
end, 15),
handler = handlers.hunk_unstage,
},
{
mode = 'n',
mapping = keymaps.buffer_reset,
handler = loop.debounce_coroutine(function()
self:reset_file()
end, 15),
handler = handlers.reset,
},
{
mode = 'n',
mapping = keymaps.buffer_stage,
handler = loop.debounce_coroutine(function()
self:stage_file()
end, 15),
handler = handlers.stage,
},
{
mode = 'n',
mapping = keymaps.buffer_unstage,
handler = loop.debounce_coroutine(function()
self:unstage_file()
end, 15),
handler = handlers.unstage,
},
{
mode = 'n',
mapping = keymaps.stage_all,
handler = loop.debounce_coroutine(function()
self:stage_all()
end, 15),
handler = handlers.stage_all,
},
{
mode = 'n',
mapping = keymaps.unstage_all,
handler = loop.debounce_coroutine(function()
self:unstage_all()
end, 15),
handler = handlers.unstage_all,
},
{
mode = 'n',
mapping = keymaps.reset_all,
handler = loop.debounce_coroutine(function()
self:reset_all()
end, 15),
handler = handlers.reset_all,
},
{
mode = 'n',
mapping = keymaps.commit,
handler = loop.debounce_coroutine(function()
self:commit()
end, 15),
handler = handlers.commit,
},
{
mode = 'n',
mapping = {
key = '<enter>',
desc = 'Open buffer'
},
handler = loop.coroutine(function()
self:enter_view()
end),
handler = handlers.enter,
},
})
end
Expand Down Expand Up @@ -547,6 +564,10 @@ function ProjectDiffScreen:create()
end

function ProjectDiffScreen:destroy()
-- Clean up timer handles from debounced keymap handlers
loop.close_debounced_handlers(self.diff_keymaps)
self.diff_keymaps = {}

self.scene:destroy()
end

Expand Down
5 changes: 1 addition & 4 deletions lua/vgit/ui/screen_manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ function screen_manager.destroy_active_screen()
local screen = screen_manager.active_screen
if not screen then return screen_manager end

local scene = screen.scene
if not scene then return screen_manager end

scene:destroy()
screen:destroy()
screen_manager.active_screen = nil

return screen_manager
Expand Down
Loading