fix(task): run on_exit async. See #1569

This commit is contained in:
Folke Lemaitre 2024-06-28 00:35:38 +02:00
parent 461552474c
commit 60fe75c88d
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
3 changed files with 23 additions and 7 deletions

View File

@ -37,6 +37,7 @@ function Async:sleep(ms)
vim.defer_fn(function() vim.defer_fn(function()
self.sleeping = false self.sleeping = false
end, ms) end, ms)
coroutine.yield()
end end
function Async:suspend() function Async:suspend()

View File

@ -197,7 +197,21 @@ function M.exec(cmd, opts)
lines = _lines lines = _lines
end, end,
}) })
vim.fn.jobwait({ job })
if job <= 0 then
error("Failed to start job: " .. vim.inspect(cmd))
end
local Async = require("lazy.async")
local async = Async.current
if async then
while vim.fn.jobwait({ job }, 0)[1] == -1 do
async:sleep(10)
end
else
vim.fn.jobwait({ job })
end
return lines return lines
end end

View File

@ -217,16 +217,13 @@ function Task:spawn(cmd, opts)
self._running:suspend() self._running:suspend()
local running = true local running = true
local ret = true local ret = { ok = true, output = "" }
---@param output string ---@param output string
function opts.on_exit(ok, output) function opts.on_exit(ok, output)
if not headless then if not headless then
self:log(vim.trim(output), ok and vim.log.levels.DEBUG or vim.log.levels.ERROR) self:log(vim.trim(output), ok and vim.log.levels.DEBUG or vim.log.levels.ERROR)
end end
if on_exit then ret = { ok = ok, output = output }
pcall(on_exit, ok, output)
end
ret = ok
running = false running = false
self._running:resume() self._running:resume()
end end
@ -241,7 +238,11 @@ function Task:spawn(cmd, opts)
Process.spawn(cmd, opts) Process.spawn(cmd, opts)
coroutine.yield() coroutine.yield()
assert(not running, "process still running?") assert(not running, "process still running?")
return ret if on_exit then
pcall(on_exit, ret.ok, ret.output)
end
coroutine.yield()
return ret.ok
end end
function Task:prefix() function Task:prefix()