-
Notifications
You must be signed in to change notification settings - Fork 7
/
Cron.lua
171 lines (141 loc) · 2.78 KB
/
Cron.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
--[[
Cron.lua
Timed Tasks Manager
Copyright (c) 2021 psiberx
]]
local Cron = { version = '1.0.2' }
local timers = {}
local counter = 0
---@param timeout number
---@param recurring boolean
---@param callback function
---@param args
---@return any
local function addTimer(timeout, recurring, callback, args)
if type(timeout) ~= 'number' then
return
end
if timeout < 0 then
return
end
if type(recurring) ~= 'boolean' then
return
end
if type(callback) ~= 'function' then
if type(args) == 'function' then
callback, args = args, callback
else
return
end
end
if type(args) ~= 'table' then
args = { arg = args }
end
counter = counter + 1
local timer = {
id = counter,
callback = callback,
recurring = recurring,
timeout = timeout,
active = true,
delay = timeout,
args = args,
}
if args.id == nil then
args.id = timer.id
end
if args.interval == nil then
args.interval = timer.timeout
end
if args.Halt == nil then
args.Halt = Cron.Halt
end
if args.Pause == nil then
args.Pause = Cron.Pause
end
if args.Resume == nil then
args.Resume = Cron.Resume
end
table.insert(timers, timer)
return timer.id
end
---@param timeout number
---@param callback function
---@param data
---@return any
function Cron.After(timeout, callback, data)
return addTimer(timeout, false, callback, data)
end
---@param timeout number
---@param callback function
---@param data
---@return any
function Cron.Every(timeout, callback, data)
return addTimer(timeout, true, callback, data)
end
---@param callback function
---@param data
---@return any
function Cron.NextTick(callback, data)
return addTimer(0, false, callback, data)
end
---@param timerId any
---@return void
function Cron.Halt(timerId)
if type(timerId) == 'table' then
timerId = timerId.id
end
for i, timer in ipairs(timers) do
if timer.id == timerId then
table.remove(timers, i)
break
end
end
end
---@param timerId any
---@return void
function Cron.Pause(timerId)
if type(timerId) == 'table' then
timerId = timerId.id
end
for _, timer in ipairs(timers) do
if timer.id == timerId then
timer.active = false
break
end
end
end
---@param timerId any
---@return void
function Cron.Resume(timerId)
if type(timerId) == 'table' then
timerId = timerId.id
end
for _, timer in ipairs(timers) do
if timer.id == timerId then
timer.active = true
break
end
end
end
---@param delta number
---@return void
function Cron.Update(delta)
if #timers > 0 then
for i, timer in ipairs(timers) do
if timer.active then
timer.delay = timer.delay - delta
if timer.delay <= 0 then
if timer.recurring then
timer.delay = timer.delay + timer.timeout
else
table.remove(timers, i)
i = i - 1
end
timer.callback(timer.args)
end
end
end
end
end
return Cron