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")
|
||||
end
|
||||
|
||||
local entries = setmetatable({}, {__mode = "k"})
|
||||
local entries = {}
|
||||
|
||||
local function newEntry(time, callback, update, ...)
|
||||
local entry = {
|
||||
@ -77,15 +77,11 @@ function cron.every(time, callback, ...)
|
||||
end
|
||||
|
||||
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 _, entry in pairs(entries) do
|
||||
if entry:update(dt, runningTime) then table.insert(expired,entry) end
|
||||
for id, entry in pairs(entries) do
|
||||
if entry:update(dt) then entry[id] = nil end
|
||||
end
|
||||
|
||||
for i=1, #expired do entries[expired[i]] = nil end
|
||||
end
|
||||
|
||||
return cron
|
||||
|
@ -15,7 +15,7 @@ context( 'cron', function()
|
||||
end)
|
||||
|
||||
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(-1) end)
|
||||
assert_not_error(function() cron.update(1) end)
|
||||
|
@ -1 +0,0 @@
|
||||
../inspect.lua
|
Loading…
Reference in New Issue
Block a user