From 2d64b400fd7f32bf58ab33b14d425a77c8b411e2 Mon Sep 17 00:00:00 2001 From: gen740 Date: Fri, 13 Oct 2023 15:59:43 +0900 Subject: [PATCH] Refactoring timer.lua --- lua/smoothcursor/callbacks/timer.lua | 37 ++++++++++++++++++---------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lua/smoothcursor/callbacks/timer.lua b/lua/smoothcursor/callbacks/timer.lua index 8fd0a68..894e9ba 100644 --- a/lua/smoothcursor/callbacks/timer.lua +++ b/lua/smoothcursor/callbacks/timer.lua @@ -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, }