Some checks failed
Detach Plugins / check (FlyGrep.vim) (push) Has been cancelled
Detach Plugins / check (GitHub.vim) (push) Has been cancelled
Detach Plugins / check (JavaUnit.vim) (push) Has been cancelled
Detach Plugins / check (SourceCounter.vim) (push) Has been cancelled
Detach Plugins / check (cpicker.nvim) (push) Has been cancelled
Detach Plugins / check (dein-ui.vim) (push) Has been cancelled
Detach Plugins / check (git.vim) (push) Has been cancelled
Detach Plugins / check (iedit.vim) (push) Has been cancelled
Detach Plugins / check (scrollbar.vim) (push) Has been cancelled
Detach Plugins / check (vim-chat) (push) Has been cancelled
Detach Plugins / check (vim-cheat) (push) Has been cancelled
Detach Plugins / check (vim-todo) (push) Has been cancelled
Detach Plugins / check (xmake.vim) (push) Has been cancelled
test / Linux (nvim, nightly) (push) Has been cancelled
test / Linux (nvim, v0.3.8) (push) Has been cancelled
test / Linux (nvim, v0.4.0) (push) Has been cancelled
test / Linux (nvim, v0.4.2) (push) Has been cancelled
test / Linux (nvim, v0.4.3) (push) Has been cancelled
test / Linux (nvim, v0.4.4) (push) Has been cancelled
test / Linux (nvim, v0.5.0) (push) Has been cancelled
test / Linux (nvim, v0.5.1) (push) Has been cancelled
test / Linux (nvim, v0.6.0) (push) Has been cancelled
test / Linux (nvim, v0.6.1) (push) Has been cancelled
test / Linux (nvim, v0.7.0) (push) Has been cancelled
test / Linux (nvim, v0.7.2) (push) Has been cancelled
test / Linux (nvim, v0.8.0) (push) Has been cancelled
test / Linux (nvim, v0.8.1) (push) Has been cancelled
test / Linux (nvim, v0.8.2) (push) Has been cancelled
test / Linux (nvim, v0.8.3) (push) Has been cancelled
test / Linux (nvim, v0.9.0) (push) Has been cancelled
test / Linux (nvim, v0.9.1) (push) Has been cancelled
test / Linux (true, vim, v7.4.052) (push) Has been cancelled
test / Linux (true, vim, v7.4.1689) (push) Has been cancelled
test / Linux (true, vim, v7.4.629) (push) Has been cancelled
test / Linux (true, vim, v8.0.0027) (push) Has been cancelled
test / Linux (true, vim, v8.0.0183) (push) Has been cancelled
test / Linux (vim, nightly) (push) Has been cancelled
test / Linux (vim, v8.0.0184) (push) Has been cancelled
test / Linux (vim, v8.0.1453) (push) Has been cancelled
test / Linux (vim, v8.1.2269) (push) Has been cancelled
test / Linux (vim, v8.2.2434) (push) Has been cancelled
test / Linux (vim, v8.2.3995) (push) Has been cancelled
test / Windows (nvim, nightly) (push) Has been cancelled
test / Windows (nvim, v0.3.8) (push) Has been cancelled
test / Windows (nvim, v0.4.2) (push) Has been cancelled
test / Windows (nvim, v0.4.3) (push) Has been cancelled
test / Windows (nvim, v0.4.4) (push) Has been cancelled
test / Windows (nvim, v0.5.0) (push) Has been cancelled
test / Windows (nvim, v0.5.1) (push) Has been cancelled
test / Windows (nvim, v0.6.0) (push) Has been cancelled
test / Windows (nvim, v0.6.1) (push) Has been cancelled
test / Windows (nvim, v0.7.0) (push) Has been cancelled
test / Windows (nvim, v0.7.2) (push) Has been cancelled
test / Windows (nvim, v0.8.0) (push) Has been cancelled
test / Windows (nvim, v0.8.1) (push) Has been cancelled
test / Windows (nvim, v0.8.2) (push) Has been cancelled
test / Windows (nvim, v0.8.3) (push) Has been cancelled
test / Windows (nvim, v0.9.0) (push) Has been cancelled
test / Windows (nvim, v0.9.1) (push) Has been cancelled
test / Windows (vim, nightly) (push) Has been cancelled
test / Windows (vim, v7.4.1185) (push) Has been cancelled
test / Windows (vim, v7.4.1689) (push) Has been cancelled
test / Windows (vim, v8.0.0027) (push) Has been cancelled
test / Windows (vim, v8.0.1453) (push) Has been cancelled
test / Windows (vim, v8.1.2269) (push) Has been cancelled
test / Windows (vim, v8.2.2434) (push) Has been cancelled
test / Windows (vim, v8.2.3995) (push) Has been cancelled
docker / docker (push) Has been cancelled
mirror / check (coding) (push) Has been cancelled
mirror / check (gitee) (push) Has been cancelled
mirror / check (gitlab) (push) Has been cancelled
162 lines
4.1 KiB
Lua
162 lines
4.1 KiB
Lua
local AsyncTask = require('cmp_dictionary.kit.Async.AsyncTask')
|
|
|
|
local Async = {}
|
|
|
|
---@type table<thread, integer>
|
|
Async.___threads___ = {}
|
|
|
|
---Alias of AsyncTask.all.
|
|
---@param tasks cmp_dictionary.kit.Async.AsyncTask[]
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.all(tasks)
|
|
return AsyncTask.all(tasks)
|
|
end
|
|
|
|
---Alias of AsyncTask.race.
|
|
---@param tasks cmp_dictionary.kit.Async.AsyncTask[]
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.race(tasks)
|
|
return AsyncTask.race(tasks)
|
|
end
|
|
|
|
---Alias of AsyncTask.resolve(v).
|
|
---@param v any
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.resolve(v)
|
|
return AsyncTask.resolve(v)
|
|
end
|
|
|
|
---Alias of AsyncTask.reject(v).
|
|
---@param v any
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.reject(v)
|
|
return AsyncTask.reject(v)
|
|
end
|
|
|
|
---Alias of AsyncTask.new(...).
|
|
---@param runner fun(resolve: fun(value: any), reject: fun(err: any))
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.new(runner)
|
|
return AsyncTask.new(runner)
|
|
end
|
|
|
|
---Run async function immediately.
|
|
---@generic T: fun(): cmp_dictionary.kit.Async.AsyncTask
|
|
---@param runner T
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.run(runner)
|
|
return Async.async(runner)()
|
|
end
|
|
|
|
---Return current context is async coroutine or not.
|
|
---@return boolean
|
|
function Async.in_context()
|
|
return Async.___threads___[coroutine.running()] ~= nil
|
|
end
|
|
|
|
---Create async function.
|
|
---@generic T: fun(...): cmp_dictionary.kit.Async.AsyncTask
|
|
---@param runner T
|
|
---@return T
|
|
function Async.async(runner)
|
|
return function(...)
|
|
local args = { ... }
|
|
|
|
local thread = coroutine.create(runner)
|
|
return AsyncTask.new(function(resolve, reject)
|
|
Async.___threads___[thread] = 1
|
|
|
|
local function next_step(ok, v)
|
|
if coroutine.status(thread) == 'dead' then
|
|
Async.___threads___[thread] = nil
|
|
if AsyncTask.is(v) then
|
|
v:dispatch(resolve, reject)
|
|
else
|
|
if ok then
|
|
resolve(v)
|
|
else
|
|
reject(v)
|
|
end
|
|
end
|
|
return
|
|
end
|
|
|
|
v:dispatch(function(...)
|
|
next_step(coroutine.resume(thread, true, ...))
|
|
end, function(...)
|
|
next_step(coroutine.resume(thread, false, ...))
|
|
end)
|
|
end
|
|
|
|
next_step(coroutine.resume(thread, unpack(args)))
|
|
end)
|
|
end
|
|
end
|
|
|
|
---Await async task.
|
|
---@param task cmp_dictionary.kit.Async.AsyncTask
|
|
---@return any
|
|
function Async.await(task)
|
|
if not Async.___threads___[coroutine.running()] then
|
|
error('`Async.await` must be called in async context.')
|
|
end
|
|
if not AsyncTask.is(task) then
|
|
error('`Async.await` must be called with AsyncTask.')
|
|
end
|
|
|
|
local ok, res = coroutine.yield(task)
|
|
if not ok then
|
|
error(res, 2)
|
|
end
|
|
return res
|
|
end
|
|
|
|
---Create vim.schedule task.
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.schedule()
|
|
return AsyncTask.new(function(resolve)
|
|
vim.schedule(resolve)
|
|
end)
|
|
end
|
|
|
|
---Create vim.defer_fn task.
|
|
---@param timeout integer
|
|
---@return cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.timeout(timeout)
|
|
return AsyncTask.new(function(resolve)
|
|
vim.defer_fn(resolve, timeout)
|
|
end)
|
|
end
|
|
|
|
---Create async function from callback function.
|
|
---@generic T: ...
|
|
---@param runner fun(...: T)
|
|
---@param option? { schedule?: boolean, callback?: integer }
|
|
---@return fun(...: T): cmp_dictionary.kit.Async.AsyncTask
|
|
function Async.promisify(runner, option)
|
|
option = option or {}
|
|
option.schedule = not vim.is_thread() and (option.schedule or false)
|
|
option.callback = option.callback or nil
|
|
return function(...)
|
|
local args = { ... }
|
|
return AsyncTask.new(function(resolve, reject)
|
|
local max = #args + 1
|
|
local pos = math.min(option.callback or max, max)
|
|
table.insert(args, pos, function(err, ...)
|
|
if option.schedule and vim.in_fast_event() then
|
|
resolve = vim.schedule_wrap(resolve)
|
|
reject = vim.schedule_wrap(reject)
|
|
end
|
|
if err then
|
|
reject(err)
|
|
else
|
|
resolve(...)
|
|
end
|
|
end)
|
|
runner(unpack(args))
|
|
end)
|
|
end
|
|
end
|
|
|
|
return Async
|