Skip to content

Commit

Permalink
[stable8.5] Cherry-pick built sim JS changes (#9812)
Browse files Browse the repository at this point in the history
* Before compiling, try to fetch built js from backend (#9778)

* Track js compilation time (#9794)

---------

Co-authored-by: Eric Anderson <[email protected]>
  • Loading branch information
aznhassan and eanders-ms authored Jan 5, 2024
1 parent cfb0ab6 commit 775aba4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 19 additions & 0 deletions pxtlib/emitter/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,25 @@ namespace pxt.Cloud {
})
}

export function downloadScriptMetaAsync(id: string): Promise<JsonScriptMeta> {
return privateRequestAsync({
url: id + (id.startsWith("S") ? `?time=${Date.now()}` : ""),
forceLiveEndpoint: true,
}).then(resp => {
return JSON.parse(resp.text).meta;
})
}

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): 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
24 changes: 23 additions & 1 deletion pxtrunner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ namespace pxt.runner {

function initInnerAsync() {
pxt.setAppTarget((window as any).pxtTargetBundle)
pxt.analytics.enable(pxt.Util.userLanguage());
Util.assert(!!pxt.appTarget);

const href = window.location.href;
Expand Down Expand Up @@ -375,7 +376,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,7 +467,24 @@ namespace pxt.runner {
simDriver?.postMessage(msg);
}

export async function fetchSimJsInfo(simOptions: SimulateOptions): Promise<pxtc.BuiltSimJsInfo> {
try {
const start = Date.now();
const result = await pxt.Cloud.downloadBuiltSimJsInfoAsync(simOptions.id);
pxt.tickEvent("perfMeasurement", {
durationMs: Date.now() - start,
operation: "fetchSimJsInfo",
});
return result;
} 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> {
const start = Date.now();
await loadPackageAsync(simOptions.id, simOptions.code, simOptions.dependencies);

let didUpgrade = false;
Expand Down Expand Up @@ -529,6 +547,10 @@ namespace pxt.runner {

const res = pxtc.buildSimJsInfo(compileResult);
res.parts = compileResult.usedParts;
pxt.tickEvent("perfMeasurement", {
durationMs: Date.now() - start,
operation: "buildSimJsInfo",
});
return res;
}

Expand Down

0 comments on commit 775aba4

Please sign in to comment.