File tree 1 file changed +13
-5
lines changed
1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change 1
1
local jobs = {}
2
2
local time = 0.0
3
+ local time_next = math.huge
3
4
4
5
core .register_globalstep (function (dtime )
5
6
time = time + dtime
6
7
7
- if # jobs < 1 then
8
+ if time < time_next then
8
9
return
9
10
end
10
11
12
+ time_next = math.huge
13
+
11
14
-- 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.
15
+ -- a timer callback.
14
16
for i = # jobs , 1 , - 1 do
15
17
local job = jobs [i ]
16
18
if time >= job .expire then
17
19
core .set_last_run_mod (job .mod_origin )
18
20
job .func (unpack (job .arg ))
19
- table.remove (jobs , i )
21
+ local jobs_l = # jobs
22
+ jobs [i ] = jobs [jobs_l ]
23
+ jobs [jobs_l ] = nil
24
+ elseif job .expire < time_next then
25
+ time_next = job .expire
20
26
end
21
27
end
22
28
end )
23
29
24
30
function core .after (after , func , ...)
25
31
assert (tonumber (after ) and type (func ) == " function" ,
26
32
" Invalid minetest.after invocation" )
33
+ local expire = time + after
27
34
jobs [# jobs + 1 ] = {
28
35
func = func ,
29
- expire = time + after ,
36
+ expire = expire ,
30
37
arg = {... },
31
38
mod_origin = core .get_last_run_mod ()
32
39
}
40
+ time_next = math.min (time_next , expire )
33
41
end
You can’t perform that action at this time.
0 commit comments