-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.lua
106 lines (83 loc) · 2.96 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
local cron = {
__VERSION = 'cron.lua 2.0.0',
__DESCRIPTION = 'Time-related functions for lua',
__URL = 'https://github.com/kikito/cron.lua',
__LICENSE = [[
Copyright (c) 2011 Enrique García Cota
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
}
-- Private functions
local function isCallable(callback)
local tc = type(callback)
if tc == 'function' then return true end
if tc == 'table' then
local mt = getmetatable(callback)
return type(mt) == 'table' and type(mt.__call) == 'function'
end
return false
end
local function checkPositiveInteger(name, value)
if type(value) ~= "number" or value < 0 then
error(name .. " must be a positive number")
end
end
local Clock = {}
local Clock_mt = {__index = Clock}
local function newClock(time, callback, update, ...)
checkPositiveInteger('time', time)
assert(isCallable(callback), "callback must be a function")
return setmetatable({
time = time,
callback = callback,
args = {...},
running = 0,
update = update
}, Clock_mt)
end
local function updateAfterClock(self, dt) -- returns true if expired
checkPositiveInteger('dt', dt)
if self.running >= self.time then return true end
self.running = self.running + dt
if self.running >= self.time then
self.callback(unpack(self.args))
return true
end
return false
end
local function updateEveryClock(self, dt)
checkPositiveInteger('dt', dt)
self.running = self.running + dt
while self.running >= self.time do
self.callback(unpack(self.args))
self.running = self.running - self.time
end
return false
end
function Clock:reset(running)
running = running or 0
checkPositiveInteger('running', running)
self.running = running
end
function cron.after(time, callback, ...)
return newClock(time, callback, updateAfterClock, ...)
end
function cron.every(time, callback, ...)
return newClock(time, callback, updateEveryClock, ...)
end
return cron