Skip to content

Commit

Permalink
frontend/utils: change refresh_access_token to respond with a shared …
Browse files Browse the repository at this point in the history
…promise. That makes it easier to synchronize multiple calls to refresh access tokens.
  • Loading branch information
ffreddow committed Dec 12, 2024
1 parent 0898483 commit 5352890
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/Frame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class Frame extends Component {
window.addEventListener("appResumed", async () => {
this.worker.terminate();
this.worker = null;
await refreshPromise;
await refresh_access_token();
this.startWorker();
this.show_spinner.value = true;

Expand Down
13 changes: 10 additions & 3 deletions frontend/src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,16 @@ window.addEventListener("keydown", (e: KeyboardEvent) => {
})

// This promise is used to synchronize the timeout and Frame component refreshing the access token
export let refreshPromise: Promise<void>;
let refreshPromise: Promise<void>;
let refreshPromiseResolved = true;

// This function must be called only at one place. Use the promise instead if you need to ensure that you have a valid token.
export async function refresh_access_token() {
export function refresh_access_token() {
if (refreshPromiseResolved) {
refreshPromiseResolved = false;
} else {
return refreshPromise;
}
refreshPromise = new Promise(async (resolve, reject) => {
if (window.location.pathname == "/recovery") {
loggedIn.value = AppState.Recovery;
Expand All @@ -106,7 +112,8 @@ export async function refresh_access_token() {
localStorage.removeItem("secretKey");
loggedIn.value = AppState.LoggedOut;
}
refreshPromiseResolved = true;
resolve();
});
await refreshPromise;
return refreshPromise
}

0 comments on commit 5352890

Please sign in to comment.