Skip to content

Throttle calls to await.delay() in some diagnostics #2680

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions script/await.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@ function m.delay()
return coroutine.yield()
end

local throttledDelayer = {}
throttledDelayer.__index = throttledDelayer

---@async
function throttledDelayer:delay()
if not m._enable then
return
end
self.calls = self.calls + 1
if self.calls == self.factor then
self.calls = 0
return m.delay()
end
end

function m.newThrottledDelayer(factor)
return setmetatable({
factor = factor,
calls = 0,
}, throttledDelayer)
end

--- stop then close
---@async
function m.stop()
Expand Down
3 changes: 2 additions & 1 deletion script/core/diagnostics/assign-type-mismatch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ return function (uri, callback)
return
end

local delayer = await.newThrottledDelayer(15)
---@async
guide.eachSourceTypes(state.ast, checkTypes, function (source)
local value = source.value
if not value then
return
end
await.delay()
delayer:delay()
if source.type == 'setlocal' then
local locNode = vm.compileNode(source.node)
if not locNode.hasDefined then
Expand Down
3 changes: 2 additions & 1 deletion script/core/diagnostics/need-check-nil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ return function (uri, callback)
return
end

local delayer = await.newThrottledDelayer(500)
---@async
guide.eachSourceType(state.ast, 'getlocal', function (src)
await.delay()
delayer:delay()
local checkNil
local nxt = src.next
if nxt then
Expand Down
3 changes: 2 additions & 1 deletion script/core/diagnostics/redundant-value.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ return function (uri, callback)
return
end

local delayer = await.newThrottledDelayer(50000)
guide.eachSource(state.ast, function (src) ---@async
await.delay()
delayer:delay()
if src.redundant then
callback {
start = src.start,
Expand Down
3 changes: 2 additions & 1 deletion script/core/diagnostics/trailing-space.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ return function (uri, callback)
if not state or not text then
return
end
local delayer = await.newThrottledDelayer(5000)
local lines = state.lines
for i = 0, #lines do
await.delay()
delayer:delay()
local startOffset = lines[i]
local finishOffset = text:find('[\r\n]', startOffset) or (#text + 1)
local lastOffset = finishOffset - 1
Expand Down
3 changes: 2 additions & 1 deletion script/core/diagnostics/unbalanced-assignments.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ return function (uri, callback, code)
end
end

local delayer = await.newThrottledDelayer(1000)
---@async
guide.eachSourceTypes(ast.ast, types, function (source)
await.delay()
delayer:delay()
checkSet(source)
end)
end