Skip to content

Commit

Permalink
Add get_elapsed_time() to retrieve the elapsed time of a job
Browse files Browse the repository at this point in the history
  • Loading branch information
Zughy committed Aug 25, 2024
1 parent 56123b2 commit 660233d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions builtin/common/after.lua
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ end
local function remove_first_jobs()
local removed_expiry = heap_pop(expiries)
local removed = job_map[removed_expiry]
removed.elapsed_time = removed_expiry
job_map[removed_expiry] = nil
return removed
end
Expand All @@ -117,6 +118,12 @@ local time_next = math.huge
core.register_globalstep(function(dtime)
time = time + dtime

for _, job in pairs(job_map) do
if job.elapsed_time then
job.elapsed_time = job.elapsed_time + dtime
end
end

if time < time_next then
return
end
Expand Down Expand Up @@ -151,6 +158,7 @@ local function dummy_func() end
function job_metatable.__index:cancel()
self.func = dummy_func
self.args = {n = 0}
self.elapsed_time = nil
end

function core.after(after, func, ...)
Expand All @@ -160,10 +168,14 @@ function core.after(after, func, ...)
local new_job = {
mod_origin = core.get_last_run_mod(),
func = func,
elapsed_time = time,
args = {
n = select("#", ...),
...
},
get_elapsed_time = function(self)
return self.elapsed_time
end
}

local expiry = time + after
Expand Down
3 changes: 3 additions & 0 deletions doc/lua_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -6699,6 +6699,9 @@ Timing
* Jobs set for earlier times are executed earlier. If multiple jobs expire
at exactly the same time, then they are executed in registration order.

* `job:get_elapsed_time()`
* Returns the game time passed since the job was launched, in `dtime_s`.
* Returns `nil` if the job is cancelled.
* `job:cancel()`
* Cancels the job function from being called

Expand Down

0 comments on commit 660233d

Please sign in to comment.