]> git.lizzy.rs Git - dragonfireclient.git/blob - builtin/common/after.lua
Improve chatcommand params consistency (#5985)
[dragonfireclient.git] / builtin / common / after.lua
1 local jobs = {}
2 local time = 0.0
3
4 core.register_globalstep(function(dtime)
5         time = time + dtime
6
7         if #jobs < 1 then
8                 return
9         end
10
11         -- Iterate backwards so that we miss any new timers added by
12         -- a timer callback, and so that we don't skip the next timer
13         -- in the list if we remove one.
14         for i = #jobs, 1, -1 do
15                 local job = jobs[i]
16                 if time >= job.expire then
17                         core.set_last_run_mod(job.mod_origin)
18                         job.func(unpack(job.arg))
19                         table.remove(jobs, i)
20                 end
21         end
22 end)
23
24 function core.after(after, func, ...)
25         assert(tonumber(after) and type(func) == "function",
26                 "Invalid core.after invocation")
27         jobs[#jobs + 1] = {
28                 func = func,
29                 expire = time + after,
30                 arg = {...},
31                 mod_origin = core.get_last_run_mod()
32         }
33 end