refactor: new cache

This commit is contained in:
Folke Lemaitre 2023-02-13 10:50:39 +01:00
parent e115f5ec17
commit 983e1c5e34
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
6 changed files with 237 additions and 177 deletions

View file

@ -1,4 +1,3 @@
local Cache = require("lazy.core.cache")
local Config = require("lazy.core.config")
local Util = require("lazy.util")
local Plugin = require("lazy.core.plugin")
@ -6,12 +5,11 @@ local Loader = require("lazy.core.loader")
local M = {}
---@type table<string, CacheHash>
---@type table<string, vim.loop.Stat>
M.files = {}
---@type vim.loop.Timer
M.timer = nil
M.root = nil
function M.enable()
if M.timer then
@ -19,7 +17,6 @@ function M.enable()
end
if #Config.spec.modules > 0 then
M.timer = vim.loop.new_timer()
M.root = vim.fn.stdpath("config") .. "/lua"
M.check(true)
M.timer:start(2000, 2000, M.check)
end
@ -32,6 +29,12 @@ function M.disable()
end
end
---@param h1 vim.loop.Stat
---@param h2 vim.loop.Stat
function M.eq(h1, h2)
return h1 and h2 and h1.size == h2.size and h1.mtime.sec == h2.mtime.sec and h1.mtime.nsec == h2.mtime.nsec
end
function M.check(start)
---@type table<string,true>
local checked = {}
@ -44,7 +47,7 @@ function M.check(start)
local hash = vim.loop.fs_stat(modpath)
if hash then
if M.files[modpath] then
if not Cache.eq(M.files[modpath], hash) then
if not M.eq(M.files[modpath], hash) then
M.files[modpath] = hash
table.insert(changes, { file = modpath, what = "changed" })
end