diff --git a/README.textile b/README.textile index 476131f..c63ba3b 100644 --- a/README.textile +++ b/README.textile @@ -4,8 +4,8 @@ h1. cron.lua h1. Examples of use -* @cron.after(time, f)@ will execute f after the given amount of time units. -* @cron.every(time, f)@ will repeat the same action periodically. +* @cron.after(time, callback)@ will execute callback after the given amount of time units. +* @cron.every(time, callback)@ will repeat the same action periodically. * @cron.cancel(id)@ will stop a timed event from happening. @id@ is returned by @cron.after@ and @cron.every@ respectively. It will stop a timed event from happening, or stop a periodic action. * @cron.reset()@ removes all timed and periodic actions, and resets the time passed back to 0. * @cron.update(dt)@ is needed to be executed on the main program loop. @dt@ is the amount of time that has passed since the last iteration. When @cron.update@ is executed, cron will check the list of pending actions and execute them if needed. diff --git a/cron.lua b/cron.lua index 7cb5382..3f3a3f8 100644 --- a/cron.lua +++ b/cron.lua @@ -6,22 +6,25 @@ ----------------------------------------------------------------------------------------------------------------------- -local entries = {} - -local TimedEntry = {} - -function TimedEntry:new(time, callback, ...) - return setmetatable( { - time = time, - callback = callback, - args = {...}, - running = 0 - }, - { __index = TimedEntry } - ) +local function checkTimeAndCallback(time, callback) + assert(type(time) == "number" and time > 0, "time must be a positive number") + assert(type(callback) == "function", "callback must be a function") end -function TimedEntry:update(dt) +local entries = {} + +local function newEntry(time, callback, update, ...) + local entry = { + time = time, + callback = callback, + args = {...}, + running = 0, + update = update + } + entries[entry] = entry +end + +local function updateTimedEntry(self, dt) self.running = self.running + dt if self.running >= self.time then self.callback(unpack(self.args)) @@ -29,6 +32,7 @@ function TimedEntry:update(dt) end end + local cron = {} function cron.reset() @@ -36,11 +40,14 @@ function cron.reset() end function cron.after(time, callback, ...) - assert(type(time) == "number" and time > 0, "time must be a positive number") - assert(type(callback) == "function", "callback must be a function") + checkTimeAndCallback(time, callback) + + return newEntry(time, callback, updateTimedEntry, ...) +end + +function cron.every(time, callback, ...) + checkTimeAndCallback(time, callback) - local entry = TimedEntry:new(time, callback, ...) - entries[entry] = entry end function cron.update(dt) diff --git a/spec/cron_spec.lua b/spec/cron_spec.lua index cea5e6f..4664b92 100644 --- a/spec/cron_spec.lua +++ b/spec/cron_spec.lua @@ -34,7 +34,7 @@ context( 'cron', function() end) context( 'after', function() - test( 'Should throw error if time is not a positive number, or f is not function', function() + test( 'Should throw error if time is not a positive number, or callback is not function', function() assert_error(function() cron.after('error', count) end) assert_error(function() cron.after(2, 'error') end) assert_error(function() cron.after(-2, count) end) @@ -61,6 +61,15 @@ context( 'cron', function() end) end) + context( 'every', function() + test( 'Should throw error if time is not a positive number, or callback is not function', function() + assert_error(function() cron.every('error', count) end) + assert_error(function() cron.every(2, 'error') end) + assert_error(function() cron.every(-2, count) end) + assert_not_error(function() cron.every(2, count) end) + end) + end) + end)