-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtimer.lua
More file actions
77 lines (63 loc) · 1.8 KB
/
Copy pathtimer.lua
File metadata and controls
77 lines (63 loc) · 1.8 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
76
77
local core = require "core"
local Object = require "core.object"
---Timer class
---@class lsp.timer : core.object
---@field public interval integer
---@field public single_shot boolean
---@field private started boolean
---@field private last_run integer
local Timer = Object:extend()
---Constructor
---@param interval integer The interval in milliseconds
---@param single_shot boolean Indicates if timer should only run once
function Timer:new(interval, single_shot)
Timer.super.new(self)
self.single_shot = single_shot or false
self.started = false
self.last_run = 0
self:set_interval(interval or 1000)
end
---Starts a non running timer.
function Timer:start()
if self.started then return end
self.started = true
local this = self
core.add_thread(function()
while true do
this:reset()
local now = system.get_time()
while (this.last_run + this.interval) > now do
if not this.started then return end
coroutine.yield((this.last_run + this.interval) - now)
now = system.get_time()
end
if not this.started then return end
this:on_timer()
if this.single_shot then break end
end
this.started = false
end)
end
---Stops a running timer.
function Timer:stop()
self.started = false
end
---Resets the timer countdown for execution.
function Timer:reset()
if self.started then
self.last_run = system.get_time()
end
end
---Check if the timer is running.
---@return boolean
function Timer:running()
return self.started
end
---Appropriately set the timer interval by converting milliseconds to seconds.
---@param interval integer The interval in milliseconds
function Timer:set_interval(interval)
self.interval = interval / 1000
end
---To be overwritten by the instantiated timer objects
function Timer:on_timer() end
return Timer