From 26fe11daf8850f7211ff95f4aacf73084db639c1 Mon Sep 17 00:00:00 2001 From: tangent Date: Tue, 22 Sep 2026 16:04:37 -0600 Subject: [PATCH 1/6] v1.2.0 --- timer.lua | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/timer.lua b/timer.lua index eada69e..04aa776 100644 --- a/timer.lua +++ b/timer.lua @@ -1,17 +1,12 @@ -local is_callable = function(callback) - 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 +-- This file is public domain. Created by Tangent, the fox, the legend. local update_after = function(self, delta) if self.run_time >= self.deadline then return true end self.run_time = self.run_time + delta if self.run_time >= self.deadline then - self.callback(unpack(self.args)) + if self.callback then + self.callback(unpack(self.args)) + end return true end end @@ -19,24 +14,35 @@ end local update_every = function(self, delta) self.run_time = self.run_time + delta while self.run_time >= self.interval do - self.callback(unpack(self.args)) + if self.callback then + self.callback(unpack(self.args)) + end self.run_time = self.run_time - self.interval end end local timer_metatable = { __index = { + get_run_time = function(self) + return self.run_time + end, reset = function(self, time) self.run_time = time or 0 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, }, } local timers = {} local new_timer = function(callback, update, ...) - assert(is_callable(callback), "callback function must be callable") - local timer = setmetatable({ callback = callback, update = update, @@ -49,7 +55,7 @@ local new_timer = function(callback, update, ...) end return { - version = "v1.1.0", + version = "v1.2.0", after = function(deadline, callback, ...) local timer = new_timer(callback, update_after, ...) timer.deadline = deadline -- 2.54.0 From 09e3023f76ecb36e68e1b1ea8b47a8a616c9fef4 Mon Sep 17 00:00:00 2001 From: tangent Date: Tue, 22 Sep 2026 16:15:44 -0600 Subject: [PATCH 2/6] Update ReadMe.md --- ReadMe.md | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 33c5d36..e8962c6 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -9,24 +9,29 @@ but that misses the biggest benefit, which is the timer library itself managing timers, including discarding expired timers: ```lua -timer.after(time, function, ...) -- runs once after time -timer.every(time, function, ...) -- runs repeatedly, every time +timer.after(deadline, function, ...) -- runs once after time +timer.every(interval, function, ...) -- runs repeatedly, every time 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 updated: ```lua -local my_timer = timer.every(time, function, ...) -my_timer.manual = true +local my_timer = timer.every(interval, function, ...) +my_timer:set_manual(true) my_timer:update(delta) ``` `timer.update` returns a table of expired timers. Example: ```lua -local my_timer = timer.after(time, function, ...) +local my_timer = timer.after(deadline, function, ...) local expired = timer.update(delta) if expired[my_timer] then @@ -34,14 +39,18 @@ if expired[my_timer] then end ``` -Timers can have their time set to a new value with `my_timer:reset(time)`. If -nothing is passed, they will be set to `0`. +Timers can have their run time set to a new value with `my_timer:reset(time)`. +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)`. --- Incompatibilities / Changes from kikito's cron.lua: -- Number values are not checked or restricted. -- Different error message for callback not being callable. +- There are no checks or restrictions on what can be set. - If a one-time timer is run by `timer.update`, it will *no longer be updated* by `timer.update`, even if reset. @@ -51,5 +60,5 @@ GitHub unpublishes my releases frequently, check tags to find them! ## Ideas / Tasks - [ ] Make an import/export format and add save/load functions - resumable timers. - [ ] Make it possible to save/load with a unix timestamp. (Dangerous.) -- [ ] Make an access function `get_run_time` -- [ ] Make functions optional +- [x] Make an access function `get_run_time` +- [x] Make functions optional -- 2.54.0 From e086ed46fa1aecfbfa816ecdde180765917f85ff Mon Sep 17 00:00:00 2001 From: tangent Date: Tue, 22 Sep 2026 16:16:48 -0600 Subject: [PATCH 3/6] set_manual() --- timer.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/timer.lua b/timer.lua index 04aa776..e9849b5 100644 --- a/timer.lua +++ b/timer.lua @@ -37,6 +37,13 @@ local timer_metatable = { 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, }, } -- 2.54.0 From a36b02acc5ef29ef34c16dc2a8b1524533a193bb Mon Sep 17 00:00:00 2001 From: tangent Date: Tue, 22 Sep 2026 16:17:44 -0600 Subject: [PATCH 4/6] Update ReadMe.md --- ReadMe.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index e8962c6..0ff8027 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -51,8 +51,8 @@ If nothing is passed, they will be set to `0`. Incompatibilities / Changes from kikito's cron.lua: - There are no checks or restrictions on what can be set. -- If a one-time timer is run by `timer.update`, it will *no longer be updated* - by `timer.update`, even if reset. +- If a one-run timer is run by `timer.update`, it will *no longer be updated* by + `timer.update`, even if reset. ## Releases GitHub unpublishes my releases frequently, check tags to find them! -- 2.54.0 From 15788a1c0d335e7001d140fe72030cb3089f0637 Mon Sep 17 00:00:00 2001 From: tangent Date: Tue, 22 Sep 2026 17:22:53 -0600 Subject: [PATCH 5/6] getters --- timer.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/timer.lua b/timer.lua index e9849b5..aa6ab3d 100644 --- a/timer.lua +++ b/timer.lua @@ -23,6 +23,12 @@ end local timer_metatable = { __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, -- 2.54.0 From c2142c197d4d2f0eafeb63325db9ded6f5196cb5 Mon Sep 17 00:00:00 2001 From: tangent Date: Tue, 22 Sep 2026 17:24:05 -0600 Subject: [PATCH 6/6] document the getters --- ReadMe.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ReadMe.md b/ReadMe.md index 0ff8027..f4e1dd7 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -46,6 +46,8 @@ If nothing is passed, they will be set to `0`. `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()`. --- -- 2.54.0