Skip to content

Commit

Permalink
Add throttling to save state in subathon widget
Browse files Browse the repository at this point in the history
  • Loading branch information
justlx committed Nov 3, 2024
1 parent 562686a commit 03b7100
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion StreamMarathon/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ let paused = false;
let pausedSeconds = 0;
let start;

const throttle = (func, limit) => {
let inThrottle;
let waitingExecution = false;

return function(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => {
inThrottle = false;
if (waitingExecution) {
waitingExecution = false;
func.apply(this, args);
}
}, limit);
} else {
waitingExecution = true;
}
};
}

function countdown(seconds, forced = false) {
if (seconds === 0 && !forced) return;
let toCountDown = start;
Expand Down Expand Up @@ -38,7 +59,7 @@ function countdown(seconds, forced = false) {
if (paused) {
$('#countdown').countdown('pause');
}
saveState();
throttledSaveState();
}

const pauseTimer = () => {
Expand Down Expand Up @@ -169,6 +190,7 @@ function saveState() {
paused: paused
});
}
const throttledSaveState = throttle(() => saveState(), 3000);

function loadState() {
SE_API.store.get('marathon').then(obj => {
Expand Down

0 comments on commit 03b7100

Please sign in to comment.