-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathtimeout.lua
55 lines (54 loc) · 1.3 KB
/
timeout.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
---@type overseer.ComponentFileDefinition
local comp = {
desc = "Cancel task if it exceeds a timeout",
params = {
timeout = {
desc = "Time to wait (in seconds) before canceling",
default = 120,
type = "integer",
validate = function(v)
return v > 0
end,
},
},
constructor = function(opts)
opts = opts or {}
vim.validate({
timeout = { opts.timeout, "n" },
})
return {
timer = nil,
canceled = false,
on_start = function(self, task)
self.timer = vim.loop.new_timer()
self.timer:start(
1000 * opts.timeout,
0,
vim.schedule_wrap(function()
self.canceled = task:stop()
end)
)
end,
on_reset = function(self, task)
self.canceled = false
if self.timer then
self.timer:close()
self.timer = nil
end
end,
on_dispose = function(self, task)
if self.timer then
self.timer:close()
self.timer = nil
end
end,
render = function(self, task, lines, highlights, detail)
if self.canceled then
table.insert(lines, "Task timed out")
table.insert(highlights, { "DiagnosticWarn", #lines, 0, -1 })
end
end,
}
end,
}
return comp