Skip to content

Commit 1686932

Browse files
committed
fix: minor cache module improvements
1 parent 5482bd6 commit 1686932

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

lua/grapple/cache.lua

+17-16
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ function Cache:new()
3333
}, self)
3434
end
3535

36-
function Cache:is_open(id)
36+
function Cache:exists(id)
3737
return self.cache[id] ~= nil
3838
end
3939

4040
---@param id string
4141
---@param opts grapple.cache.options
4242
function Cache:open(id, opts)
43-
if self.cache[id] then
43+
if self:exists(id) then
4444
self:close(id)
4545
end
4646

@@ -60,11 +60,11 @@ end
6060

6161
---@param id string
6262
function Cache:close(id)
63-
local cache_value = self.cache[id]
64-
if not cache_value then
63+
if not self:exists(id) then
6564
return
6665
end
6766

67+
local cache_value = self.cache[id]
6868
if cache_value.watching then
6969
self:unwatch(id)
7070
end
@@ -74,11 +74,11 @@ end
7474

7575
---@param id string
7676
function Cache:watch(id)
77-
local cache_value = self.cache[id]
78-
if not cache_value then
77+
if not self:exists(id) then
7978
return
8079
end
8180

81+
local cache_value = self.cache[id]
8282
if cache_value.watching then
8383
self:unwatch(id)
8484
end
@@ -93,7 +93,7 @@ function Cache:watch(id)
9393
if cache_value.debounce then
9494
cache_value.debouncing = true
9595

96-
local timer = vim.loop.new_timer()
96+
local timer = assert(vim.loop.new_timer())
9797
timer:start(cache_value.debounce, 0, function()
9898
cache_value.debouncing = false
9999
end)
@@ -118,13 +118,13 @@ end
118118

119119
---@param id string
120120
function Cache:unwatch(id)
121-
local cache_value = self.cache[id]
122-
if not cache_value then
121+
if not self:exists(id) then
123122
return
124123
end
125124

126125
self:invalidate(id)
127126

127+
local cache_value = self.cache[id]
128128
if cache_value.au_id then
129129
vim.api.nvim_del_autocmd(cache_value.au_id)
130130
end
@@ -139,10 +139,11 @@ end
139139
---@param id string
140140
---@return any cached_value
141141
function Cache:get(id)
142-
local cache_value = self.cache[id]
143-
if not cache_value then
142+
if not self:exists(id) then
144143
return
145144
end
145+
146+
local cache_value = self.cache[id]
146147
if not cache_value.watching then
147148
self:watch(id)
148149
end
@@ -153,10 +154,11 @@ end
153154
---@param id string
154155
---@param value any
155156
function Cache:store(id, value)
156-
local cache_value = self.cache[id]
157-
if not cache_value then
157+
if not self:exists(id) then
158158
return
159159
end
160+
161+
local cache_value = self.cache[id]
160162
if not cache_value.watching then
161163
self:watch(id)
162164
end
@@ -166,12 +168,11 @@ end
166168

167169
---@param id string
168170
function Cache:invalidate(id)
169-
local cache_value = self.cache[id]
170-
if not cache_value then
171+
if not self:exists(id) then
171172
return
172173
end
173174

174-
cache_value.value = nil
175+
self.cache[id].value = nil
175176
end
176177

177178
return Cache

0 commit comments

Comments
 (0)