Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Make ratelimitedfunc time from the function's end
Browse files Browse the repository at this point in the history
Otherwise any function tghat takes longer than the delay to execute
will become eligible for execution again immediately after
finishing and therefore be able to spin.

This should help with element-hq/element-web#6060
(at least in the respect that it makes ratelimitedfunc do its job)
even if it's not the reason Riot started getting wedged.
  • Loading branch information
dbkr committed Feb 6, 2018
1 parent 127eb61 commit aab57d0
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/ratelimitedfunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ module.exports = function(f, minIntervalMs) {

if (self.lastCall < now - minIntervalMs) {
f.apply(this);
self.lastCall = now;
// get the time again now the function has finished, so if it
// took longer than the delay time to execute, it doesn't
// immediately become eligible to run again.
self.lastCall = Date.now();
} else if (self.scheduledCall === undefined) {
self.scheduledCall = setTimeout(
() => {
self.scheduledCall = undefined;
f.apply(this);
self.lastCall = now;
// get time again as per above
self.lastCall = Date.now();
},
(self.lastCall + minIntervalMs) - now,
);
Expand Down

0 comments on commit aab57d0

Please sign in to comment.