telescope-frecency.nvim/lua/frecency/wait.lua
JINNOUCHI Yasushi a6482c2fbf
feat: separate bootstrap logic to launch faster (#250)
* chore: track setup() time

* feat: avoid setup() to be called twice

* chore: track time between Database:start()

* feat: add bootstrap option to load DB in advance

* feat: initialize DB before frecency class starts

* chore: add more logging

* feat!: load DB in Neovim starting

only if the plugin is loaded non-lazily.

* fix: simplify logic for timer

* fix: detect error and safely finish

* chore: remove unnecessary method
2024-08-31 15:02:15 +09:00

48 lines
1011 B
Lua

local async = require "plenary.async"
---@class FrecencyWait
---@field config FrecencyWaitConfig
local Wait = {}
---@class FrecencyWaitConfig
---@field time integer default: 5000
---@field interval integer default: 200
---@alias FrecencyWaitCallback fun(): nil
---@param f FrecencyWaitCallback
---@param opts FrecencyWaitConfig?
Wait.new = function(f, opts)
return setmetatable(
{ f = f, config = vim.tbl_extend("force", { time = 5000, interval = 200 }, opts or {}) },
{ __index = Wait }
)
end
---@async
---@private
Wait.f = function()
error "implement me"
end
---@return boolean ok
---@return nil|-1|-2 status
function Wait:run()
local done = false
async.void(function()
self.f()
done = true
end)()
return vim.wait(self.config.time, function()
return done
end, self.config.interval)
end
---@param f FrecencyWaitCallback
---@param opts FrecencyWaitConfig?
---@return boolean ok
---@return nil|-1|-2 status
return function(f, opts)
return Wait.new(f, opts):run()
end