mirror of https://github.com/folke/lazy.nvim.git
refactor: cleanup
This commit is contained in:
parent
37c7163f8d
commit
461552474c
|
@ -9,7 +9,6 @@ local M = {}
|
||||||
M._queue = {}
|
M._queue = {}
|
||||||
M._executor = assert(vim.loop.new_check())
|
M._executor = assert(vim.loop.new_check())
|
||||||
M._running = false
|
M._running = false
|
||||||
M.SLEEP = "sleep"
|
|
||||||
---@type Async
|
---@type Async
|
||||||
M.current = nil
|
M.current = nil
|
||||||
|
|
||||||
|
@ -62,9 +61,7 @@ function Async:step()
|
||||||
self.opts.on_error(tostring(res))
|
self.opts.on_error(tostring(res))
|
||||||
end
|
end
|
||||||
elseif res then
|
elseif res then
|
||||||
if res == M.SLEEP then
|
if self.opts.on_yield then
|
||||||
self.sleeping = true
|
|
||||||
elseif self.opts.on_yield then
|
|
||||||
self.opts.on_yield(res)
|
self.opts.on_yield(res)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -130,4 +127,11 @@ function M.wrap(fn, opts)
|
||||||
end
|
end
|
||||||
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
|
return M
|
||||||
|
|
|
@ -75,16 +75,8 @@ function Task:status()
|
||||||
return msg ~= "" and msg or nil
|
return msg ~= "" and msg or nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function Task:has_started()
|
|
||||||
return self._started ~= nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function Task:has_ended()
|
|
||||||
return self._ended ~= nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function Task:is_running()
|
function Task:is_running()
|
||||||
return not self:has_ended()
|
return self._ended == nil
|
||||||
end
|
end
|
||||||
|
|
||||||
function Task:has_errors()
|
function Task:has_errors()
|
||||||
|
@ -103,8 +95,8 @@ end
|
||||||
---@private
|
---@private
|
||||||
---@param task LazyTaskFn
|
---@param task LazyTaskFn
|
||||||
function Task:_start(task)
|
function Task:_start(task)
|
||||||
assert(not self:has_started(), "task already started")
|
assert(not self._started, "task already started")
|
||||||
assert(not self:has_ended(), "task already done")
|
assert(not self._ended, "task already done")
|
||||||
|
|
||||||
if Config.headless() and Config.options.headless.task then
|
if Config.headless() and Config.options.headless.task then
|
||||||
self:log("Running task " .. self.name, vim.log.levels.INFO)
|
self:log("Running task " .. self.name, vim.log.levels.INFO)
|
||||||
|
@ -171,8 +163,8 @@ end
|
||||||
|
|
||||||
---@private
|
---@private
|
||||||
function Task:_done()
|
function Task:_done()
|
||||||
assert(self:has_started(), "task not started")
|
assert(self._started, "task not started")
|
||||||
assert(not self:has_ended(), "task already done")
|
assert(not self._ended, "task already done")
|
||||||
|
|
||||||
if self._running and self._running:running() then
|
if self._running and self._running:running() then
|
||||||
return
|
return
|
||||||
|
@ -194,10 +186,10 @@ function Task:_done()
|
||||||
end
|
end
|
||||||
|
|
||||||
function Task:time()
|
function Task:time()
|
||||||
if not self:has_started() then
|
if not self._started then
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
if not self:has_ended() then
|
if not self._ended then
|
||||||
return (vim.uv.hrtime() - self._started) / 1e6
|
return (vim.uv.hrtime() - self._started) / 1e6
|
||||||
end
|
end
|
||||||
return (self._ended - self._started) / 1e6
|
return (self._ended - self._started) / 1e6
|
||||||
|
|
|
@ -229,14 +229,6 @@ function M.markdown(msg, opts)
|
||||||
)
|
)
|
||||||
end
|
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)
|
function M._dump(value, result)
|
||||||
local t = type(value)
|
local t = type(value)
|
||||||
if t == "number" or t == "boolean" then
|
if t == "number" or t == "boolean" then
|
||||||
|
|
|
@ -21,7 +21,6 @@ describe("task", function()
|
||||||
|
|
||||||
it("simple function", function()
|
it("simple function", function()
|
||||||
local task = Task.new(plugin, "test", function() end, opts)
|
local task = Task.new(plugin, "test", function() end, opts)
|
||||||
assert(task:has_started())
|
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
task:wait()
|
task:wait()
|
||||||
assert(not task:is_running())
|
assert(not task:is_running())
|
||||||
|
@ -32,7 +31,6 @@ describe("task", function()
|
||||||
local task = Task.new(plugin, "test", function()
|
local task = Task.new(plugin, "test", function()
|
||||||
error("test")
|
error("test")
|
||||||
end, opts)
|
end, opts)
|
||||||
assert(task:has_started())
|
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
task:wait()
|
task:wait()
|
||||||
assert(not task:is_running())
|
assert(not task:is_running())
|
||||||
|
@ -48,7 +46,6 @@ describe("task", function()
|
||||||
coroutine.yield()
|
coroutine.yield()
|
||||||
running = false
|
running = false
|
||||||
end, opts)
|
end, opts)
|
||||||
assert(task:has_started())
|
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
assert(running)
|
assert(running)
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
|
@ -63,7 +60,6 @@ describe("task", function()
|
||||||
local task = Task.new(plugin, "spawn_errors", function(task)
|
local task = Task.new(plugin, "spawn_errors", function(task)
|
||||||
task:spawn("foobar")
|
task:spawn("foobar")
|
||||||
end, opts)
|
end, opts)
|
||||||
assert(task:has_started())
|
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
task:wait()
|
task:wait()
|
||||||
assert(not task:is_running())
|
assert(not task:is_running())
|
||||||
|
@ -75,9 +71,7 @@ describe("task", function()
|
||||||
local task = Task.new(plugin, "test", function(task)
|
local task = Task.new(plugin, "test", function(task)
|
||||||
task:spawn("echo", { args = { "foo" } })
|
task:spawn("echo", { args = { "foo" } })
|
||||||
end, opts)
|
end, opts)
|
||||||
assert(task:has_started())
|
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
assert(task:has_started())
|
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
task:wait()
|
task:wait()
|
||||||
assert.same(task:output(), "foo")
|
assert.same(task:output(), "foo")
|
||||||
|
@ -90,7 +84,6 @@ describe("task", function()
|
||||||
task:spawn("echo", { args = { "foo" } })
|
task:spawn("echo", { args = { "foo" } })
|
||||||
task:spawn("echo", { args = { "bar" } })
|
task:spawn("echo", { args = { "bar" } })
|
||||||
end, opts)
|
end, opts)
|
||||||
assert(task:has_started())
|
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
assert(task:is_running())
|
assert(task:is_running())
|
||||||
task:wait()
|
task:wait()
|
||||||
|
|
Loading…
Reference in New Issue