Skip to content

Commit

Permalink
channel: Retry polling in case of error.
Browse files Browse the repository at this point in the history
If the polling is expected to continue and an error occurs in
the current poll, we retry after 5 seconds.
  • Loading branch information
kuv2707 committed Jun 20, 2024
1 parent 5390e68 commit 964ef30
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions frontend/src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const GAME_EVENTS = {
};

let jwt: string = '';
let polling = false;

let gameEventsDispatcher: (event: types.AppEvent) => void = () => {};

Expand Down Expand Up @@ -60,20 +61,26 @@ function pollLoop() {
pollLoop();
})
.catch((e) => {
if (!polling) {
console.log('Polling stopped - interrupted current poll');
return;
}
console.error('Error polling:', e);
// setTimeout(() => {
// console.log('Retrying polling');
// pollLoop();
// }, 5000);
setTimeout(() => {
console.log('Retrying polling');
pollLoop();
}, 5000);
});
}

export function startPolling(_jwt: string) {
jwt = _jwt;
polling = true;
pollLoop();
}

export function stopPolling() {
polling = false;
if (abortController) {
abortController.abort();
}
Expand Down

0 comments on commit 964ef30

Please sign in to comment.