Skip to content

Commit

Permalink
Refactoring timer.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
gen740 committed Oct 13, 2023
1 parent 40e1cd8 commit 2d64b40
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions lua/smoothcursor/callbacks/timer.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
-- import 'config' module
local config = require('smoothcursor.config')

-- sc_timer --------------------------------------------------------------------
-- Hold unique uv timer.
local uv = vim.loop
---@class ScTimer
---@field public is_running boolean
---@field public timer unknown
local ScTimer = {}
ScTimer.__index = ScTimer

local sc_timer = {
is_running = false,
timer = uv.new_timer(),
}

-- post if timer is stop
--- Post if timer is stopped.
---@param func function
function sc_timer:post(func)
if self.is_runnig then
function ScTimer:post(func)
if self.is_running then
return
end
uv.timer_start(self.timer, 0, config.config.intervals, vim.schedule_wrap(func))
vim.loop.timer_start(self.timer, 0, config.config.intervals, vim.schedule_wrap(func))
self.is_running = true
end

function sc_timer:abort()
--- Abort the timer.
function ScTimer:abort()
self.timer:stop()
self.is_running = false
end

-- Initialize ScTimer object.
---@return ScTimer
local function newScTimer()
local self = setmetatable({}, ScTimer)
self.is_running = false
self.timer = vim.uv.new_timer()
return self
end

---@type ScTimer
local sc_timer = newScTimer()

return {
sc_timer = sc_timer,
}

0 comments on commit 2d64b40

Please sign in to comment.