max_timer: don't call callback in the same tick anymore

This commit is contained in:
Mitchell Hashimoto
2022-04-29 19:27:52 -07:00
parent 49f28b3bb7
commit dcf0194a2f

View File

@ -47,8 +47,7 @@ pub fn MaxTimer(comptime cb: fn (*libuv.Timer) void) type {
}).callback); }).callback);
// The maximum time can't be less than the interval otherwise this // The maximum time can't be less than the interval otherwise this
// will just constantly fire. // will just constantly fire. if (max < min) return error.MaxShorterThanTimer;
if (max < min) return error.MaxShorterThanTimer;
return Self{ return Self{
.timer = timer, .timer = timer,
.min = min, .min = min,
@ -98,13 +97,13 @@ pub fn MaxTimer(comptime cb: fn (*libuv.Timer) void) type {
// If we are past the max time, we run the timer now. // If we are past the max time, we run the timer now.
try self.timer.stop(); try self.timer.stop();
self.timer.loop().updateTime(); self.timer.loop().updateTime();
if (self.timer.loop().now() - self.last > self.max) { const timeout = if (self.timer.loop().now() - self.last > self.max)
@call(.{ .modifier = .always_inline }, cb, .{&self.timer}); 0
return; else
} self.min;
// We still have time, restart the timer so that it is min time away. // We still have time, restart the timer so that it is min time away.
try self.timer.start(cb, self.min, 0); try self.timer.start(cb, timeout, 0);
} }
}; };
} }