Skip to content

Commit 813fc94

Browse files
committed
feat(checker): checker will now save last check time and only check at configured frequency even after restarting Neovim
1 parent 835731f commit 813fc94

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

lua/lazy/core/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ M.defaults = {
136136
-- only generate markdown helptags for plugins that dont have docs
137137
skip_if_doc_exists = true,
138138
},
139+
state = vim.fn.stdpath("state") .. "/lazy/state.json", -- state info for checker and other things
139140
debug = false,
140141
}
141142

lua/lazy/manage/checker.lua

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local Manage = require("lazy.manage")
33
local Util = require("lazy.util")
44
local Plugin = require("lazy.core.plugin")
55
local Git = require("lazy.manage.git")
6+
local State = require("lazy.state")
67

78
local M = {}
89

@@ -12,7 +13,14 @@ M.reported = {}
1213

1314
function M.start()
1415
M.fast_check()
15-
M.check()
16+
M.schedule()
17+
end
18+
19+
function M.schedule()
20+
State.read() -- update state
21+
local next_check = State.checker.last_check + Config.options.checker.frequency - os.time()
22+
next_check = math.max(next_check, 0)
23+
vim.defer_fn(M.check, next_check * 1000)
1624
end
1725

1826
---@param opts? {report:boolean} report defaults to true
@@ -32,6 +40,8 @@ function M.fast_check(opts)
3240
end
3341

3442
function M.check()
43+
State.checker.last_check = os.time()
44+
State.write() -- update state
3545
local errors = false
3646
for _, plugin in pairs(Config.plugins) do
3747
if Plugin.has_errors(plugin) then
@@ -40,14 +50,14 @@ function M.check()
4050
end
4151
end
4252
if errors then
43-
vim.defer_fn(M.check, Config.options.checker.frequency * 1000)
53+
M.schedule()
4454
else
4555
Manage.check({
4656
show = false,
4757
concurrency = Config.options.checker.concurrency,
4858
}):wait(function()
4959
M.report()
50-
vim.defer_fn(M.check, Config.options.checker.frequency * 1000)
60+
M.schedule()
5161
end)
5262
end
5363
end

lua/lazy/state.lua

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
local Util = require("lazy.util")
2+
local Config = require("lazy.core.config")
3+
4+
---@type LazyState
5+
local M = {}
6+
7+
---@class LazyState
8+
local defaults = {
9+
checker = {
10+
last_check = 0,
11+
},
12+
}
13+
14+
---@type LazyState
15+
local data = nil
16+
17+
function M.read()
18+
pcall(function()
19+
---@diagnostic disable-next-line: cast-local-type
20+
data = vim.json.decode(Util.read_file(Config.options.state))
21+
end)
22+
data = vim.tbl_deep_extend("force", {}, defaults, data or {})
23+
end
24+
25+
function M.write()
26+
vim.fn.mkdir(vim.fn.fnamemodify(Config.options.state, ":p:h"), "p")
27+
Util.write_file(Config.options.state, vim.json.encode(data))
28+
end
29+
30+
function M.__index(_, key)
31+
if not data then
32+
M.read()
33+
end
34+
return data[key]
35+
end
36+
37+
function M.__setindex(_, key, value)
38+
if not data then
39+
M.read()
40+
end
41+
---@diagnostic disable-next-line: no-unknown
42+
data[key] = value
43+
end
44+
45+
return setmetatable(M, M)

0 commit comments

Comments
 (0)