Skip to content

Commit e2ca72d

Browse files
authored
Merge pull request #2680 from emmericp/throttle-awaits
Throttle calls to await.delay() in some diagnostics
2 parents 789f06b + 439c1e7 commit e2ca72d

File tree

6 files changed

+32
-5
lines changed

6 files changed

+32
-5
lines changed

script/await.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,28 @@ function m.delay()
176176
return coroutine.yield()
177177
end
178178

179+
local throttledDelayer = {}
180+
throttledDelayer.__index = throttledDelayer
181+
182+
---@async
183+
function throttledDelayer:delay()
184+
if not m._enable then
185+
return
186+
end
187+
self.calls = self.calls + 1
188+
if self.calls == self.factor then
189+
self.calls = 0
190+
return m.delay()
191+
end
192+
end
193+
194+
function m.newThrottledDelayer(factor)
195+
return setmetatable({
196+
factor = factor,
197+
calls = 0,
198+
}, throttledDelayer)
199+
end
200+
179201
--- stop then close
180202
---@async
181203
function m.stop()

script/core/diagnostics/assign-type-mismatch.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ return function (uri, callback)
5252
return
5353
end
5454

55+
local delayer = await.newThrottledDelayer(15)
5556
---@async
5657
guide.eachSourceTypes(state.ast, checkTypes, function (source)
5758
local value = source.value
5859
if not value then
5960
return
6061
end
61-
await.delay()
62+
delayer:delay()
6263
if source.type == 'setlocal' then
6364
local locNode = vm.compileNode(source.node)
6465
if not locNode.hasDefined then

script/core/diagnostics/need-check-nil.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ return function (uri, callback)
1111
return
1212
end
1313

14+
local delayer = await.newThrottledDelayer(500)
1415
---@async
1516
guide.eachSourceType(state.ast, 'getlocal', function (src)
16-
await.delay()
17+
delayer:delay()
1718
local checkNil
1819
local nxt = src.next
1920
if nxt then

script/core/diagnostics/redundant-value.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ return function (uri, callback)
1111
return
1212
end
1313

14+
local delayer = await.newThrottledDelayer(50000)
1415
guide.eachSource(state.ast, function (src) ---@async
15-
await.delay()
16+
delayer:delay()
1617
if src.redundant then
1718
callback {
1819
start = src.start,

script/core/diagnostics/trailing-space.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ return function (uri, callback)
1010
if not state or not text then
1111
return
1212
end
13+
local delayer = await.newThrottledDelayer(5000)
1314
local lines = state.lines
1415
for i = 0, #lines do
15-
await.delay()
16+
delayer:delay()
1617
local startOffset = lines[i]
1718
local finishOffset = text:find('[\r\n]', startOffset) or (#text + 1)
1819
local lastOffset = finishOffset - 1

script/core/diagnostics/unbalanced-assignments.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ return function (uri, callback, code)
4141
end
4242
end
4343

44+
local delayer = await.newThrottledDelayer(1000)
4445
---@async
4546
guide.eachSourceTypes(ast.ast, types, function (source)
46-
await.delay()
47+
delayer:delay()
4748
checkSet(source)
4849
end)
4950
end

0 commit comments

Comments
 (0)