telescope-frecency.nvim/lua/frecency/database/table.lua
JINNOUCHI Yasushi c140e6ff9c
feat: make query() faster and more lazier (#241)
* refactor: simplify logic to load web_devicons

* refactor: make register() asynchronous

* fix: load lazily modules outside this plugin

* refactor: simplify logic to wait initialization

* refactor: use uv.hrtime() instead of os.clock()

* fix: avoid errors in calling plenary.log in async

* test: store elapsed time to check in tests

* test: fix module names

This becomes a problem only in Ubuntu because macOS and Windows does not
care cases in filenames.

* test: fix types and unused modules

* style: fix by stylua

* refactor: make recency / entry_maker loaded lazily
2024-08-25 19:28:52 +09:00

61 lines
1.5 KiB
Lua

local Timer = require "frecency.timer"
local lazy_require = require "frecency.lazy_require"
local async = lazy_require "plenary.async" --[[@as FrecencyPlenaryAsync]]
---@class FrecencyDatabaseRecordValue
---@field count integer
---@field timestamps integer[]
---@class FrecencyDatabaseRawTable
---@field version string
---@field records table<string,FrecencyDatabaseRecordValue>
---@class FrecencyDatabaseTable: FrecencyDatabaseRawTable
---@field private is_ready boolean
local Table = {}
---@param version string
---@return FrecencyDatabaseTable
Table.new = function(version)
return setmetatable({ is_ready = false, version = version }, { __index = Table.__index })
end
---@async
---@param key string
function Table:__index(key)
if key == "records" and not rawget(self, "is_ready") then
Table.wait_ready(self)
end
return vim.F.if_nil(rawget(self, key), Table[key])
end
function Table:raw()
return { version = self.version, records = self.records }
end
---@param raw_table? FrecencyDatabaseRawTable
---@return nil
function Table:set(raw_table)
local tbl = raw_table or { version = self.version, records = {} }
if self.version ~= tbl.version then
error "Invalid version"
end
self.is_ready = true
self.records = tbl.records
end
---This is for internal or testing use only.
---@async
---@return nil
function Table:wait_ready()
local timer = Timer.new "wait_ready()"
local t = 0.2
while not rawget(self, "is_ready") do
async.util.sleep(t)
t = t * 2
end
timer:finish()
end
return Table