refacto: make git module interface to wait for job to finish
allows simplify the explore/reload/find/initialization by making the whole code synchronous. No more callback needed.
This commit is contained in:
@@ -7,32 +7,26 @@ local M = {
|
||||
cwd_to_project_root = {}
|
||||
}
|
||||
|
||||
function M.reload(callback)
|
||||
local num_projects = vim.tbl_count(M.projects)
|
||||
if not M.config.enable or num_projects == 0 then
|
||||
return callback({})
|
||||
function M.reload()
|
||||
if not M.config.enable then
|
||||
return {}
|
||||
end
|
||||
|
||||
local done = 0
|
||||
for project_root in pairs(M.projects) do
|
||||
M.projects[project_root] = {}
|
||||
Runner.run {
|
||||
local git_status = Runner.run {
|
||||
project_root = project_root,
|
||||
list_untracked = git_utils.should_show_untracked(project_root),
|
||||
list_ignored = true,
|
||||
timeout = M.config.timeout,
|
||||
on_end = function(git_status)
|
||||
M.projects[project_root] = {
|
||||
files = git_status,
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
|
||||
}
|
||||
done = done + 1
|
||||
if done == num_projects then
|
||||
callback(M.projects)
|
||||
end
|
||||
end
|
||||
}
|
||||
M.projects[project_root] = {
|
||||
files = git_status,
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
|
||||
}
|
||||
end
|
||||
|
||||
return M.projects
|
||||
end
|
||||
|
||||
function M.get_project_root(cwd)
|
||||
@@ -48,35 +42,33 @@ function M.get_project_root(cwd)
|
||||
return project_root
|
||||
end
|
||||
|
||||
function M.load_project_status(cwd, callback)
|
||||
function M.load_project_status(cwd)
|
||||
if not M.config.enable then
|
||||
return callback({})
|
||||
return {}
|
||||
end
|
||||
|
||||
local project_root = M.get_project_root(cwd)
|
||||
if not project_root then
|
||||
M.cwd_to_project_root[cwd] = false
|
||||
return callback({})
|
||||
return {}
|
||||
end
|
||||
|
||||
local status = M.projects[project_root]
|
||||
if status then
|
||||
return callback(status)
|
||||
return status
|
||||
end
|
||||
|
||||
Runner.run {
|
||||
local git_status = Runner.run {
|
||||
project_root = project_root,
|
||||
list_untracked = git_utils.should_show_untracked(project_root),
|
||||
list_ignored = true,
|
||||
timeout = M.config.timeout,
|
||||
on_end = function(git_status)
|
||||
M.projects[project_root] = {
|
||||
files = git_status,
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
|
||||
}
|
||||
callback(M.projects[project_root])
|
||||
end
|
||||
timeout = M.config.timeout
|
||||
}
|
||||
M.projects[project_root] = {
|
||||
files = git_status,
|
||||
dirs = git_utils.file_status_to_dir_status(git_status, project_root)
|
||||
}
|
||||
return M.projects[project_root]
|
||||
end
|
||||
|
||||
function M.setup(opts)
|
||||
|
||||
@@ -55,7 +55,8 @@ function Runner:_run_git_job()
|
||||
local stdout = uv.new_pipe(false)
|
||||
local timer = uv.new_timer()
|
||||
|
||||
local function on_finish(output)
|
||||
local function on_finish()
|
||||
self._done = true
|
||||
if timer:is_closing() or stdout:is_closing() or (handle and handle:is_closing()) then
|
||||
return
|
||||
end
|
||||
@@ -68,8 +69,6 @@ function Runner:_run_git_job()
|
||||
end
|
||||
|
||||
pcall(uv.kill, pid)
|
||||
|
||||
self.on_end(output or self.output)
|
||||
end
|
||||
|
||||
handle, pid = uv.spawn(
|
||||
@@ -78,7 +77,7 @@ function Runner:_run_git_job()
|
||||
vim.schedule_wrap(function() on_finish() end)
|
||||
)
|
||||
|
||||
timer:start(self.timeout, 0, vim.schedule_wrap(function() on_finish({}) end))
|
||||
timer:start(self.timeout, 0, vim.schedule_wrap(function() on_finish() end))
|
||||
|
||||
local output_leftover = ''
|
||||
local function manage_output(err, data)
|
||||
@@ -89,6 +88,10 @@ function Runner:_run_git_job()
|
||||
uv.read_start(stdout, vim.schedule_wrap(manage_output))
|
||||
end
|
||||
|
||||
function Runner:_wait()
|
||||
while not vim.wait(30, function() return self._done end, 30) do end
|
||||
end
|
||||
|
||||
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
|
||||
function Runner.run(opts)
|
||||
local self = setmetatable({
|
||||
@@ -97,10 +100,12 @@ function Runner.run(opts)
|
||||
list_ignored = opts.list_ignored,
|
||||
timeout = opts.timeout or 400,
|
||||
output = {},
|
||||
on_end = opts.on_end,
|
||||
_done = false
|
||||
}, Runner)
|
||||
|
||||
self:_run_git_job()
|
||||
self:_wait()
|
||||
return self.output
|
||||
end
|
||||
|
||||
return Runner
|
||||
|
||||
Reference in New Issue
Block a user