perf: cache repo root and harden async paths
Problem: get_repo_root() shells out on every call, causing 4-6 redundant subprocesses per gdiff_file() invocation. highlight_vim_syntax() leaks a scratch buffer if nvim_buf_call errors. lib.ensure() silently drops callbacks during download, permanently missing intra-line highlights. The debounce timer callback can operate on an invalid buffer. Solution: Cache get_repo_root() results by parent directory. Wrap nvim_buf_call and nvim_buf_delete in pcall so the scratch buffer is always cleaned up. Queue pending callbacks in lib.ensure() so all callers receive the handle once the download completes. Guard the debounce timer callback with nvim_buf_is_valid.
This commit is contained in:
parent
a2053a132b
commit
3c3b27a2cb
4 changed files with 30 additions and 14 deletions
|
|
@ -8,6 +8,9 @@ local cached_handle = nil
|
|||
---@type boolean
|
||||
local download_in_progress = false
|
||||
|
||||
---@type fun(handle: table?)[]
|
||||
local pending_callbacks = {}
|
||||
|
||||
---@return string
|
||||
local function get_os()
|
||||
local os_name = jit.os:lower()
|
||||
|
|
@ -164,9 +167,10 @@ function M.ensure(callback)
|
|||
return
|
||||
end
|
||||
|
||||
table.insert(pending_callbacks, callback)
|
||||
|
||||
if download_in_progress then
|
||||
dbg('download already in progress')
|
||||
callback(nil)
|
||||
dbg('download already in progress, queued callback')
|
||||
return
|
||||
end
|
||||
|
||||
|
|
@ -192,21 +196,25 @@ function M.ensure(callback)
|
|||
vim.system(cmd, {}, function(result)
|
||||
download_in_progress = false
|
||||
vim.schedule(function()
|
||||
local handle = nil
|
||||
if result.code ~= 0 then
|
||||
vim.notify('[diffs] failed to download libvscode_diff', vim.log.levels.WARN)
|
||||
dbg('curl failed: %s', result.stderr or '')
|
||||
callback(nil)
|
||||
return
|
||||
else
|
||||
local f = io.open(version_path(), 'w')
|
||||
if f then
|
||||
f:write(EXPECTED_VERSION)
|
||||
f:close()
|
||||
end
|
||||
vim.notify('[diffs] libvscode_diff downloaded', vim.log.levels.INFO)
|
||||
handle = M.load()
|
||||
end
|
||||
|
||||
local f = io.open(version_path(), 'w')
|
||||
if f then
|
||||
f:write(EXPECTED_VERSION)
|
||||
f:close()
|
||||
local cbs = pending_callbacks
|
||||
pending_callbacks = {}
|
||||
for _, cb in ipairs(cbs) do
|
||||
cb(handle)
|
||||
end
|
||||
|
||||
vim.notify('[diffs] libvscode_diff downloaded', vim.log.levels.INFO)
|
||||
callback(M.load())
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue