1.2.0 #1
@@ -9,24 +9,29 @@ but that misses the biggest benefit, which is the timer library itself managing
|
|||||||
timers, including discarding expired timers:
|
timers, including discarding expired timers:
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
timer.after(time, function, ...) -- runs once after time
|
timer.after(deadline, function, ...) -- runs once after time
|
||||||
timer.every(time, function, ...) -- runs repeatedly, every time
|
timer.every(interval, function, ...) -- runs repeatedly, every time
|
||||||
|
|
||||||
timer.update(delta) -- increment, run functions, discard expired..
|
timer.update(delta) -- increment, run functions, discard expired..
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Functions are completely optional, so you can use the mechanism to track time
|
||||||
|
rather than execute functions. `timer:get_run_time()` returns the amount of time
|
||||||
|
since the last interval. (Note: This is only useful for repeating timers.
|
||||||
|
One-run timers will only count to their `deadline` once, unless manually run.)
|
||||||
|
|
||||||
A timer can be excluded from `timer.update`, in which case it must be manually
|
A timer can be excluded from `timer.update`, in which case it must be manually
|
||||||
updated:
|
updated:
|
||||||
```lua
|
```lua
|
||||||
local my_timer = timer.every(time, function, ...)
|
local my_timer = timer.every(interval, function, ...)
|
||||||
my_timer.manual = true
|
my_timer:set_manual(true)
|
||||||
|
|
||||||
my_timer:update(delta)
|
my_timer:update(delta)
|
||||||
```
|
```
|
||||||
|
|
||||||
`timer.update` returns a table of expired timers. Example:
|
`timer.update` returns a table of expired timers. Example:
|
||||||
```lua
|
```lua
|
||||||
local my_timer = timer.after(time, function, ...)
|
local my_timer = timer.after(deadline, function, ...)
|
||||||
|
|
||||||
local expired = timer.update(delta)
|
local expired = timer.update(delta)
|
||||||
if expired[my_timer] then
|
if expired[my_timer] then
|
||||||
@@ -34,16 +39,22 @@ if expired[my_timer] then
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
Timers can have their time set to a new value with `my_timer:reset(time)`. If
|
Timers can have their run time set to a new value with `my_timer:reset(time)`.
|
||||||
nothing is passed, they will be set to `0`.
|
If nothing is passed, they will be set to `0`.
|
||||||
|
|
||||||
|
- One-run timers can have their `deadline` changed with
|
||||||
|
`my_timer:set_deadline(time)`.
|
||||||
|
- Repeating timers can have their `interval` changed with
|
||||||
|
`my_timer:set_interval(time)`.
|
||||||
|
- You can get deadlines/intervals with
|
||||||
|
`my_timer:get_deadline()` / `my_timer:get_interval()`.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
Incompatibilities / Changes from kikito's cron.lua:
|
Incompatibilities / Changes from kikito's cron.lua:
|
||||||
- Number values are not checked or restricted.
|
- There are no checks or restrictions on what can be set.
|
||||||
- Different error message for callback not being callable.
|
- If a one-run timer is run by `timer.update`, it will *no longer be updated* by
|
||||||
- If a one-time timer is run by `timer.update`, it will *no longer be updated*
|
`timer.update`, even if reset.
|
||||||
by `timer.update`, even if reset.
|
|
||||||
|
|
||||||
## Releases
|
## Releases
|
||||||
GitHub unpublishes my releases frequently, check tags to find them!
|
GitHub unpublishes my releases frequently, check tags to find them!
|
||||||
@@ -51,5 +62,5 @@ GitHub unpublishes my releases frequently, check tags to find them!
|
|||||||
## Ideas / Tasks
|
## Ideas / Tasks
|
||||||
- [ ] Make an import/export format and add save/load functions - resumable timers.
|
- [ ] Make an import/export format and add save/load functions - resumable timers.
|
||||||
- [ ] Make it possible to save/load with a unix timestamp. (Dangerous.)
|
- [ ] Make it possible to save/load with a unix timestamp. (Dangerous.)
|
||||||
- [ ] Make an access function `get_run_time`
|
- [x] Make an access function `get_run_time`
|
||||||
- [ ] Make functions optional
|
- [x] Make functions optional
|
||||||
|
|||||||
@@ -1,17 +1,12 @@
|
|||||||
local is_callable = function(callback)
|
-- This file is public domain. Created by Tangent, the fox, the legend.
|
||||||
local _type = type(callback)
|
|
||||||
if _type == "function" then return true end
|
|
||||||
if _type == "table" then
|
|
||||||
local metatable = getmetatable(callback)
|
|
||||||
return (type(metatable) == "table") and (type(metatable.__call) == "function")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local update_after = function(self, delta)
|
local update_after = function(self, delta)
|
||||||
if self.run_time >= self.deadline then return true end
|
if self.run_time >= self.deadline then return true end
|
||||||
self.run_time = self.run_time + delta
|
self.run_time = self.run_time + delta
|
||||||
if self.run_time >= self.deadline then
|
if self.run_time >= self.deadline then
|
||||||
|
if self.callback then
|
||||||
self.callback(unpack(self.args))
|
self.callback(unpack(self.args))
|
||||||
|
end
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -19,24 +14,48 @@ end
|
|||||||
local update_every = function(self, delta)
|
local update_every = function(self, delta)
|
||||||
self.run_time = self.run_time + delta
|
self.run_time = self.run_time + delta
|
||||||
while self.run_time >= self.interval do
|
while self.run_time >= self.interval do
|
||||||
|
if self.callback then
|
||||||
self.callback(unpack(self.args))
|
self.callback(unpack(self.args))
|
||||||
|
end
|
||||||
self.run_time = self.run_time - self.interval
|
self.run_time = self.run_time - self.interval
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local timer_metatable = {
|
local timer_metatable = {
|
||||||
__index = {
|
__index = {
|
||||||
|
get_deadline = function(self)
|
||||||
|
return self.deadline
|
||||||
|
end,
|
||||||
|
get_interval = function(self)
|
||||||
|
return self.interval
|
||||||
|
end,
|
||||||
|
get_run_time = function(self)
|
||||||
|
return self.run_time
|
||||||
|
end,
|
||||||
reset = function(self, time)
|
reset = function(self, time)
|
||||||
self.run_time = time or 0
|
self.run_time = time or 0
|
||||||
end,
|
end,
|
||||||
|
set_deadline = function(self, deadline)
|
||||||
|
assert(self.deadline, "set_deadline() is only for timer.after() timers.")
|
||||||
|
self.deadline = deadline
|
||||||
|
end,
|
||||||
|
set_interval = function(self, interval)
|
||||||
|
assert(self.interval, "set_interval() is only for timer.every() timers.")
|
||||||
|
self.interval = interval
|
||||||
|
end,
|
||||||
|
set_manual = function(self, manual)
|
||||||
|
if manual then
|
||||||
|
self.manual = true
|
||||||
|
else
|
||||||
|
self.manual = nil
|
||||||
|
end
|
||||||
|
end,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
local timers = {}
|
local timers = {}
|
||||||
|
|
||||||
local new_timer = function(callback, update, ...)
|
local new_timer = function(callback, update, ...)
|
||||||
assert(is_callable(callback), "callback function must be callable")
|
|
||||||
|
|
||||||
local timer = setmetatable({
|
local timer = setmetatable({
|
||||||
callback = callback,
|
callback = callback,
|
||||||
update = update,
|
update = update,
|
||||||
@@ -49,7 +68,7 @@ local new_timer = function(callback, update, ...)
|
|||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
version = "v1.1.0",
|
version = "v1.2.0",
|
||||||
after = function(deadline, callback, ...)
|
after = function(deadline, callback, ...)
|
||||||
local timer = new_timer(callback, update_after, ...)
|
local timer = new_timer(callback, update_after, ...)
|
||||||
timer.deadline = deadline
|
timer.deadline = deadline
|
||||||
|
|||||||
Reference in New Issue
Block a user