Skip to content

Commit

Permalink
fix: ensure we only wait for online once when refreshing token
Browse files Browse the repository at this point in the history
  • Loading branch information
freaktechnik committed Apr 29, 2021
1 parent 02ffa68 commit 50dc7c3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,13 @@ const HEX = 16,
SAFETY_OFFSET: 100,
hasUpdateLoop: false,
redirectUri: browser.identity.getRedirectURL(),
waitingForOnline: false,
async refreshToken() {
if(!navigator.onLine) {
if(this.waitingForOnline) {
return;
}
this.waitingForOnline = true;
await waitForOnline();
}
try {
Expand All @@ -134,12 +139,18 @@ const HEX = 16,
});
if(response.ok) {
const data = await response.json();
this.waitingForOnline = false;
return this.storeToken(data.access_token, data.expires_in * S_TO_MS, data.refresh_token);
}
if(response.status === OFFLINE) {
await waitForOnline();
return this.refreshToken();
if(!this.waitingForOnline) {
this.waitingForOnline = true;
await waitForOnline();
return this.refreshToken();
}
return;
}
this.waitingForOnline = false;
throw new Error("Could not fetch new token");
}
catch(error) {
Expand Down

0 comments on commit 50dc7c3

Please sign in to comment.