forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.lua
More file actions
75 lines (62 loc) · 1.77 KB
/
timer.lua
File metadata and controls
75 lines (62 loc) · 1.77 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---@class TimerOptions
---@field interval number The interval in milliseconds
---@field on_tick function The function to call on each tick
---@field on_stop? function The function to call when the timer stops
---@field repeat_timer? boolean Whether the timer should repeat (default: true)
---@field args? table Optional arguments to pass to the on_tick function
local Timer = {}
Timer.__index = Timer
---@param opts TimerOptions
function Timer.new(opts)
local self = setmetatable({}, Timer)
self.interval = opts.interval
self.on_tick = opts.on_tick
self.on_stop = opts.on_stop
self.repeat_timer = opts.repeat_timer ~= false
self.args = opts.args or {}
self._uv_timer = nil
return self
end
function Timer:start()
self:stop()
local timer = vim.uv.new_timer()
if not timer then
error('failed to create uv timer')
end
self._uv_timer = timer
local on_tick = vim.schedule_wrap(function()
local ok, continue = pcall(self.on_tick, unpack(self.args))
if not ok or not self.repeat_timer or (continue == false) then
self:stop()
end
end)
local ok, err = pcall(function()
local repeat_interval = self.repeat_timer and self.interval or 0
timer:start(self.interval, repeat_interval, on_tick)
end)
if not ok then
pcall(timer.close, timer)
self._uv_timer = nil
error(err)
end
end
--- Start the timer and immediately execute the callback
function Timer:start_and_tick()
self:start()
self.on_tick(unpack(self.args))
end
function Timer:stop()
if not self._uv_timer then
return
end
pcall(self._uv_timer.stop, self._uv_timer)
pcall(self._uv_timer.close, self._uv_timer)
self._uv_timer = nil
if self.on_stop then
pcall(self.on_stop)
end
end
function Timer:is_running()
return self._uv_timer ~= nil
end
return Timer