Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Question: how should I use timer:close? #2

Open
fvictorio opened this issue May 16, 2023 · 2 comments
Open

Question: how should I use timer:close? #2

fvictorio opened this issue May 16, 2023 · 2 comments

Comments

@fvictorio
Copy link

Hi, thanks a lot for writing and sharing this.

I'm trying to show LSP diagnostics on cursor hold, but I also want to (trail) throttle it. This is what I'm doing:

function ThrottledOpenDiagnostic()
  local fn, timer = require("throttle-debounce").throttle_trailing(OpenDiagnostic, 1000)
  fn()
end

-- Show diagnostics under the cursor when holding position
vim.api.nvim_create_augroup("lsp_diagnostics_hold", { clear = true })
vim.api.nvim_create_autocmd({ "CursorHold" }, {
  pattern = "*",
  command = "lua ThrottledOpenDiagnostic()",
  group = "lsp_diagnostics_hold",
})

This works, but I'm not calling timer:close anywhere. If I call it immediately after fn(), this will obviously won't work. Could you help me understand what's the right way to use it? Thanks!

@runiq
Copy link
Owner

runiq commented Jun 6, 2023

Honestly, I'd do it the same way, i.e. just call it right after the call to fn(). Your ticket made me think, however, that maybe I ought to close the timer right there in the throttle_trailing() function instead of exposing it to the user. Unfortunately it's been a while since I touched this code, so I won't be able to fix this right away I'm afraid.

@fvictorio
Copy link
Author

Haha, understandable, thanks for responding!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants