lazy.nvim/lua/lazy/async.lua

174 lines
3.3 KiB
Lua
Raw Normal View History

2024-06-26 14:11:31 +01:00
local M = {}
---@type Async[]
M._queue = {}
M._executor = assert(vim.loop.new_timer())
M.TIMER = 10
M.BUDGET = 100
2024-06-28 15:08:26 +01:00
---@type table<thread, Async>
M._threads = setmetatable({}, { __mode = "k" })
---@alias AsyncEvent "done" | "error" | "yield" | "ok"
2024-06-26 14:11:31 +01:00
---@class Async
2024-06-28 15:08:26 +01:00
---@field _co thread
---@field _fn fun()
---@field _suspended? boolean
---@field _on table<AsyncEvent, fun(res:any, async:Async)[]>
2024-06-26 14:11:31 +01:00
local Async = {}
---@param fn async fun()
---@return Async
2024-06-28 15:08:26 +01:00
function Async.new(fn)
2024-06-26 14:11:31 +01:00
local self = setmetatable({}, { __index = Async })
2024-06-28 15:08:26 +01:00
return self:init(fn)
end
---@param fn async fun()
---@return Async
function Async:init(fn)
self._fn = fn
self._on = {}
self._co = coroutine.create(function()
local ok, err = pcall(self._fn)
if not ok then
self:_emit("error", err)
end
self:_emit("done")
end)
M._threads[self._co] = self
return M.add(self)
end
function Async:restart()
assert(not self:running(), "Cannot restart a running async")
self:init(self._fn)
end
---@param event AsyncEvent
---@param cb async fun(res:any, async:Async)
function Async:on(event, cb)
self._on[event] = self._on[event] or {}
table.insert(self._on[event], cb)
2024-06-26 14:11:31 +01:00
return self
end
2024-06-28 15:08:26 +01:00
---@private
---@param event AsyncEvent
---@param res any
function Async:_emit(event, res)
for _, cb in ipairs(self._on[event] or {}) do
cb(res, self)
end
end
2024-06-26 14:11:31 +01:00
function Async:running()
2024-06-28 15:08:26 +01:00
return coroutine.status(self._co) ~= "dead"
2024-06-26 14:11:31 +01:00
end
2024-06-28 15:08:26 +01:00
---@async
function Async:sleep(ms)
vim.defer_fn(function()
self:resume()
end, ms)
self:suspend()
end
2024-06-28 15:08:26 +01:00
---@async
function Async:suspend()
2024-06-28 15:08:26 +01:00
self._suspended = true
if coroutine.running() == self._co then
coroutine.yield()
end
end
function Async:resume()
2024-06-28 15:08:26 +01:00
self._suspended = false
end
function Async:wait()
local async = M.running()
if coroutine.running() == self._co then
error("Cannot wait on self")
end
while self:running() do
if async then
coroutine.yield()
else
vim.wait(10)
end
end
return self
end
2024-06-26 14:11:31 +01:00
function Async:step()
2024-06-28 15:08:26 +01:00
if self._suspended then
return true
end
2024-06-28 15:08:26 +01:00
local status = coroutine.status(self._co)
2024-06-26 14:11:31 +01:00
if status == "suspended" then
2024-06-28 15:08:26 +01:00
local ok, res = coroutine.resume(self._co)
2024-06-26 14:11:31 +01:00
if not ok then
2024-06-28 15:08:26 +01:00
error(res)
2024-06-26 14:11:31 +01:00
elseif res then
2024-06-28 15:08:26 +01:00
self:_emit("yield", res)
2024-06-26 14:11:31 +01:00
end
end
2024-06-28 15:08:26 +01:00
return self:running()
2024-06-26 14:11:31 +01:00
end
function M.step()
local budget = M.BUDGET * 1e6
local start = vim.uv.hrtime()
2024-06-26 14:11:31 +01:00
local count = #M._queue
local i = 0
while #M._queue > 0 and vim.uv.hrtime() - start < budget do
2024-06-26 14:11:31 +01:00
---@type Async
local state = table.remove(M._queue, 1)
if state:step() then
table.insert(M._queue, state)
end
i = i + 1
if i >= count then
break
end
end
if #M._queue == 0 then
return M._executor:stop()
end
end
---@param async Async
function M.add(async)
table.insert(M._queue, async)
if not M._executor:is_active() then
M._executor:start(1, M.TIMER, vim.schedule_wrap(M.step))
2024-06-26 14:11:31 +01:00
end
return async
end
2024-06-28 15:08:26 +01:00
function M.running()
local co = coroutine.running()
if co then
local async = M._threads[co]
assert(async, "In coroutine without async context")
return async
2024-06-26 14:11:31 +01:00
end
end
2024-06-27 13:43:35 +01:00
---@async
---@param ms number
function M.sleep(ms)
2024-06-28 15:08:26 +01:00
local async = M.running()
assert(async, "Not in an async context")
async:sleep(ms)
2024-06-27 13:43:35 +01:00
end
2024-06-28 15:08:26 +01:00
M.Async = Async
M.new = Async.new
2024-06-26 14:11:31 +01:00
return M