Skip to content

Commit

Permalink
Before compiling, try to fetch built js from backend (#9778)
Browse files Browse the repository at this point in the history
  • Loading branch information
eanders-ms authored Nov 13, 2023
1 parent 874936d commit 9ef1988
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 10 additions & 0 deletions pxtlib/emitter/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ namespace pxt.Cloud {
})
}

export async function downloadBuiltSimJsInfoAsync(id: string): Promise<pxtc.BuiltSimJsInfo> {
const targetVersion = pxt.appTarget.versions && pxt.appTarget.versions.target || "";
const url = pxt.U.stringifyQueryString(id + "/js", { v: "v" + targetVersion }) + (id.startsWith("S") ? `&time=${Date.now()}` : "");
const resp = await privateRequestAsync({
url,
forceLiveEndpoint: true,
});
return resp.json;
}

export async function markdownAsync(docid: string, locale?: string, propagateExceptions?: boolean): Promise<string> {
// 1h check on markdown content if not on development server
const MARKDOWN_EXPIRATION = pxt.BrowserUtils.isLocalHostDev() ? 0 : 1 * 60 * 60 * 1000;
Expand Down
12 changes: 11 additions & 1 deletion pxtrunner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ namespace pxt.runner {
}

export async function simulateAsync(container: HTMLElement, simOptions: SimulateOptions): Promise<pxtc.BuiltSimJsInfo> {
const builtSimJS = simOptions.builtJsInfo || await buildSimJsInfo(simOptions);
const builtSimJS = simOptions.builtJsInfo || await fetchSimJsInfo(simOptions) || await buildSimJsInfo(simOptions);
const { js } = builtSimJS;

if (!js) {
Expand Down Expand Up @@ -466,6 +466,16 @@ namespace pxt.runner {
simDriver?.postMessage(msg);
}

export async function fetchSimJsInfo(simOptions: SimulateOptions): Promise<pxtc.BuiltSimJsInfo> {
try {
return await pxt.Cloud.downloadBuiltSimJsInfoAsync(simOptions.id);
} catch (e) {
// This exception will happen in the majority of cases, so we don't want to log it unless for debugging.
pxt.debug(e.toString());
return undefined;
}
}

export async function buildSimJsInfo(simOptions: SimulateOptions): Promise<pxtc.BuiltSimJsInfo> {
await loadPackageAsync(simOptions.id, simOptions.code, simOptions.dependencies);

Expand Down

0 comments on commit 9ef1988

Please sign in to comment.