mirror of
https://github.com/kikito/cron.lua.git
synced 2024-12-19 18:34:20 +00:00
commit
2a1ad28c94
12
cron.lua
12
cron.lua
@ -25,7 +25,7 @@ local function checkTimeAndCallback(time, callback)
|
|||||||
assert(isCallable(callback), "callback must be a function")
|
assert(isCallable(callback), "callback must be a function")
|
||||||
end
|
end
|
||||||
|
|
||||||
local entries = setmetatable({}, {__mode = "k"})
|
local entries = {}
|
||||||
|
|
||||||
local function newEntry(time, callback, update, ...)
|
local function newEntry(time, callback, update, ...)
|
||||||
local entry = {
|
local entry = {
|
||||||
@ -77,15 +77,11 @@ function cron.every(time, callback, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function cron.update(dt)
|
function cron.update(dt)
|
||||||
assert(type(dt) == "number" and dt > 0, "dt must be a positive number")
|
assert(type(dt) == "number" and dt >= 0, "dt must be a non-negative number")
|
||||||
|
|
||||||
local expired = {}
|
for id, entry in pairs(entries) do
|
||||||
|
if entry:update(dt) then entry[id] = nil end
|
||||||
for _, entry in pairs(entries) do
|
|
||||||
if entry:update(dt, runningTime) then table.insert(expired,entry) end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for i=1, #expired do entries[expired[i]] = nil end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return cron
|
return cron
|
||||||
|
@ -15,7 +15,7 @@ context( 'cron', function()
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
context('update', function()
|
context('update', function()
|
||||||
test( 'Should throw an error if dt is not a positive number', function()
|
test( 'Should throw an error if dt is a negative number', function()
|
||||||
assert_error(function() cron.update() end)
|
assert_error(function() cron.update() end)
|
||||||
assert_error(function() cron.update(-1) end)
|
assert_error(function() cron.update(-1) end)
|
||||||
assert_not_error(function() cron.update(1) end)
|
assert_not_error(function() cron.update(1) end)
|
||||||
|
@ -1 +0,0 @@
|
|||||||
../inspect.lua
|
|
Loading…
Reference in New Issue
Block a user