From a9fea11133e7bf00575e540e97a776b8677cb76c Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Sun, 9 Mar 2025 01:34:59 +0300 Subject: [PATCH] rework scheduler to use vim.uv; fall back to vim.loop --- lua/syntax-gaslighting.lua | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lua/syntax-gaslighting.lua b/lua/syntax-gaslighting.lua index 0b431a6..833bbc8 100644 --- a/lua/syntax-gaslighting.lua +++ b/lua/syntax-gaslighting.lua @@ -191,12 +191,22 @@ end --- Debounced update function function M.schedule_update() + local uv = vim.uv or vim.loop + + -- Properly clean up the previous timer before creating a new one. + -- Do NOT move this block below `uv.new_timer()`, or the new timer + -- will be stopped and closed immediately after being created. if timer then - timer:stop() - timer:close() + if timer.stop then timer:stop() end + if timer.close then timer:close() end + end + + timer = uv.new_timer() + + -- Ensure the new timer is valid before starting it. + if timer then + timer:start(config.debounce_delay, 0, vim.schedule_wrap(M.update_decorations)) end - timer = vim.loop.new_timer() - timer:start(config.debounce_delay, 0, vim.schedule_wrap(M.update_decorations)) end return M