Skip to content

Commit

Permalink
Update the immersive reader token (#9952)
Browse files Browse the repository at this point in the history
* Update the immersive reader token

* Change launchImmersiveReader to async/await

* Change missed promise to async/await

* Remove the token if there is a problem using it

* Await calls to `launchImmersiveReaderAsync`.
  • Loading branch information
aznhassan authored Apr 10, 2024
1 parent 4a7a769 commit 3979157
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions webapp/src/components/tutorial/TutorialContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from "react";
import { MarkedContent } from "../../marked";
import { Button, Modal, ModalButton } from "../../sui";
import { ImmersiveReaderButton, launchImmersiveReader } from "../../immersivereader";
import { ImmersiveReaderButton, launchImmersiveReaderAsync } from "../../immersivereader";
import { TutorialStepCounter } from "./TutorialStepCounter";
import { TutorialHint } from "./TutorialHint";
import { TutorialResetCode } from "./TutorialResetCode";
Expand Down Expand Up @@ -229,7 +229,7 @@ export function TutorialContainer(props: TutorialContainerProps) {
if (showImmersiveReader) {
modalActions.push({
className: "immersive-reader-button",
onclick: () => { launchImmersiveReader(currentStepInfo.contentMd, tutorialOptions) },
onclick: async () => { await launchImmersiveReaderAsync(currentStepInfo.contentMd, tutorialOptions) },
ariaLabel: lf("Launch Immersive Reader"),
title: lf("Launch Immersive Reader")
})
Expand Down
39 changes: 20 additions & 19 deletions webapp/src/immersivereader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ function beautifyText(content: string): string {
}
}

const IMMERSIVE_READER_ID = "immReaderToken";
function getTokenAsync(): Promise<ImmersiveReaderToken> {
const IMMERSIVE_READER_ID = "immReader";
const storedTokenString = pxt.storage.getLocal(IMMERSIVE_READER_ID);
const cachedToken: ImmersiveReaderToken = pxt.Util.jsonTryParse(storedTokenString);

Expand All @@ -212,7 +212,7 @@ function getTokenAsync(): Promise<ImmersiveReaderToken> {
}
}

export function launchImmersiveReader(content: string, tutorialOptions: pxt.tutorial.TutorialOptions) {
export async function launchImmersiveReaderAsync(content: string, tutorialOptions: pxt.tutorial.TutorialOptions) {
pxt.tickEvent("immersiveReader.launch", {tutorial: tutorialOptions.tutorial, tutorialStep: tutorialOptions.tutorialStep});

const userReaderPref = data.getData<string>(auth.READER) || ""
Expand All @@ -235,19 +235,17 @@ export function launchImmersiveReader(content: string, tutorialOptions: pxt.tuto
preferences: userReaderPref
}

getTokenAsync().then(res =>{
return testConnectionAsync(res);
}).then(res => {
if (Cloud.isOnline()) {
const launchStart = pxt.Util.now();
return ImmersiveReader.launchAsync(res.token, res.subdomain, tutorialData, options).then(res => {
const elapsed = pxt.Util.now() - launchStart;
pxt.tickEvent("immersiveReader.launch.finished", {elapsed: elapsed})
})
} else {
return Promise.reject(new Error("offline"));
try {
let res = await getTokenAsync();
await testConnectionAsync();
if (!Cloud.isOnline()) {
throw new Error("offline");
}
}).catch(e => {
const launchStart = pxt.Util.now();
await ImmersiveReader.launchAsync(res.token, res.subdomain, tutorialData, options)
const elapsed = pxt.Util.now() - launchStart;
pxt.tickEvent("immersiveReader.launch.finished", {elapsed: elapsed})
} catch (e) {
if (e.isOffline) {
core.warningNotification(lf("Immersive Reader cannot be used offline"));
} else {
Expand All @@ -258,6 +256,8 @@ export function launchImmersiveReader(content: string, tutorialOptions: pxt.tuto
}
case "token":
default: {
// If the token is invalid, remove it from storage
pxt.storage.removeLocal(IMMERSIVE_READER_ID);
core.warningNotification(lf("Immersive Reader could not be launched"));
if (typeof e == "string") {
pxt.tickEvent("immersiveReader.error", {message: e});
Expand All @@ -270,10 +270,11 @@ export function launchImmersiveReader(content: string, tutorialOptions: pxt.tuto
}
pxt.reportException(e);
ImmersiveReader.close();
});
};

function testConnectionAsync(token: ImmersiveReaderToken): Promise<ImmersiveReaderToken> {
return pxt.Cloud.privateGetAsync("ping", true).then(() => {return token});
async function testConnectionAsync() {
// Will throw an exception here if there is an error we are not expecting
return pxt.Cloud.privateGetAsync("ping", true);
}
}

Expand All @@ -284,8 +285,8 @@ interface ImmersiveReaderProps {
}

export class ImmersiveReaderButton extends data.Component<ImmersiveReaderProps, {}> {
private buttonClickHandler = () => {
launchImmersiveReader(this.props.content, this.props.tutorialOptions);
private buttonClickHandler = async () => {
await launchImmersiveReaderAsync(this.props.content, this.props.tutorialOptions);
}

render() {
Expand Down
2 changes: 1 addition & 1 deletion webapp/src/tutorial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ export class TutorialHint extends data.Component<ISettingsProps, TutorialHintSta
if (immersiveReaderEnabled) {
actions.push({
className: "immersive-reader-button",
onclick: () => { ImmersiveReader.launchImmersiveReader(fullText, options) },
onclick: async () => { await ImmersiveReader.launchImmersiveReaderAsync(fullText, options) },
ariaLabel: lf("Launch Immersive Reader"),
title: lf("Launch Immersive Reader")
})
Expand Down

0 comments on commit 3979157

Please sign in to comment.