mirror of
https://github.com/kikito/cron.lua.git
synced 2024-12-19 18:34:20 +00:00
refactorized pseudo-class out
This commit is contained in:
parent
d74a00145e
commit
39cee89b94
@ -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.
|
||||
|
33
cron.lua
33
cron.lua
@ -6,22 +6,25 @@
|
||||
-----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
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
|
||||
|
||||
local entries = {}
|
||||
|
||||
local TimedEntry = {}
|
||||
|
||||
function TimedEntry:new(time, callback, ...)
|
||||
return setmetatable( {
|
||||
local function newEntry(time, callback, update, ...)
|
||||
local entry = {
|
||||
time = time,
|
||||
callback = callback,
|
||||
args = {...},
|
||||
running = 0
|
||||
},
|
||||
{ __index = TimedEntry }
|
||||
)
|
||||
running = 0,
|
||||
update = update
|
||||
}
|
||||
entries[entry] = entry
|
||||
end
|
||||
|
||||
function TimedEntry:update(dt)
|
||||
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)
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user