Merge pull request #2 from hahawoo/master

A few changes!
This commit is contained in:
Enrique García 2012-10-22 14:56:11 -07:00
commit 2a1ad28c94
3 changed files with 5 additions and 10 deletions

View File

@ -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

View File

@ -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)

View File

@ -1 +0,0 @@
../inspect.lua