refactor: cleanup

This commit is contained in:
Folke Lemaitre 2024-06-27 14:43:35 +02:00
parent 37c7163f8d
commit 461552474c
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
4 changed files with 15 additions and 34 deletions

View File

@ -9,7 +9,6 @@ local M = {}
M._queue = {}
M._executor = assert(vim.loop.new_check())
M._running = false
M.SLEEP = "sleep"
---@type Async
M.current = nil
@ -62,9 +61,7 @@ function Async:step()
self.opts.on_error(tostring(res))
end
elseif res then
if res == M.SLEEP then
self.sleeping = true
elseif self.opts.on_yield then
if self.opts.on_yield then
self.opts.on_yield(res)
end
end
@ -130,4 +127,11 @@ function M.wrap(fn, opts)
end
end
---@async
---@param ms number
function M.sleep(ms)
assert(M.current, "Not in an async context")
M.current:sleep(ms)
end
return M

View File

@ -75,16 +75,8 @@ function Task:status()
return msg ~= "" and msg or nil
end
function Task:has_started()
return self._started ~= nil
end
function Task:has_ended()
return self._ended ~= nil
end
function Task:is_running()
return not self:has_ended()
return self._ended == nil
end
function Task:has_errors()
@ -103,8 +95,8 @@ end
---@private
---@param task LazyTaskFn
function Task:_start(task)
assert(not self:has_started(), "task already started")
assert(not self:has_ended(), "task already done")
assert(not self._started, "task already started")
assert(not self._ended, "task already done")
if Config.headless() and Config.options.headless.task then
self:log("Running task " .. self.name, vim.log.levels.INFO)
@ -171,8 +163,8 @@ end
---@private
function Task:_done()
assert(self:has_started(), "task not started")
assert(not self:has_ended(), "task already done")
assert(self._started, "task not started")
assert(not self._ended, "task already done")
if self._running and self._running:running() then
return
@ -194,10 +186,10 @@ function Task:_done()
end
function Task:time()
if not self:has_started() then
if not self._started then
return 0
end
if not self:has_ended() then
if not self._ended then
return (vim.uv.hrtime() - self._started) / 1e6
end
return (self._ended - self._started) / 1e6

View File

@ -229,14 +229,6 @@ function M.markdown(msg, opts)
)
end
---@async
---@param ms number
function M.sleep(ms)
local async = require("lazy.async").current
assert(async, "Not in an async context")
async:sleep(ms)
end
function M._dump(value, result)
local t = type(value)
if t == "number" or t == "boolean" then

View File

@ -21,7 +21,6 @@ describe("task", function()
it("simple function", function()
local task = Task.new(plugin, "test", function() end, opts)
assert(task:has_started())
assert(task:is_running())
task:wait()
assert(not task:is_running())
@ -32,7 +31,6 @@ describe("task", function()
local task = Task.new(plugin, "test", function()
error("test")
end, opts)
assert(task:has_started())
assert(task:is_running())
task:wait()
assert(not task:is_running())
@ -48,7 +46,6 @@ describe("task", function()
coroutine.yield()
running = false
end, opts)
assert(task:has_started())
assert(task:is_running())
assert(running)
assert(task:is_running())
@ -63,7 +60,6 @@ describe("task", function()
local task = Task.new(plugin, "spawn_errors", function(task)
task:spawn("foobar")
end, opts)
assert(task:has_started())
assert(task:is_running())
task:wait()
assert(not task:is_running())
@ -75,9 +71,7 @@ describe("task", function()
local task = Task.new(plugin, "test", function(task)
task:spawn("echo", { args = { "foo" } })
end, opts)
assert(task:has_started())
assert(task:is_running())
assert(task:has_started())
assert(task:is_running())
task:wait()
assert.same(task:output(), "foo")
@ -90,7 +84,6 @@ describe("task", function()
task:spawn("echo", { args = { "foo" } })
task:spawn("echo", { args = { "bar" } })
end, opts)
assert(task:has_started())
assert(task:is_running())
assert(task:is_running())
task:wait()