diff --git a/pxtlib/emitter/cloud.ts b/pxtlib/emitter/cloud.ts index 69bc9cdf60eb..29e9ec1d2a9a 100644 --- a/pxtlib/emitter/cloud.ts +++ b/pxtlib/emitter/cloud.ts @@ -125,6 +125,16 @@ namespace pxt.Cloud { }) } + export async function downloadBuiltSimJsInfoAsync(id: string): Promise { + 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 { // 1h check on markdown content if not on development server const MARKDOWN_EXPIRATION = pxt.BrowserUtils.isLocalHostDev() ? 0 : 1 * 60 * 60 * 1000; diff --git a/pxtrunner/runner.ts b/pxtrunner/runner.ts index 7bb57b15a349..d9f56c031e15 100644 --- a/pxtrunner/runner.ts +++ b/pxtrunner/runner.ts @@ -375,7 +375,7 @@ namespace pxt.runner { } export async function simulateAsync(container: HTMLElement, simOptions: SimulateOptions): Promise { - const builtSimJS = simOptions.builtJsInfo || await buildSimJsInfo(simOptions); + const builtSimJS = simOptions.builtJsInfo || await fetchSimJsInfo(simOptions) || await buildSimJsInfo(simOptions); const { js } = builtSimJS; if (!js) { @@ -466,6 +466,16 @@ namespace pxt.runner { simDriver?.postMessage(msg); } + export async function fetchSimJsInfo(simOptions: SimulateOptions): Promise { + 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 { await loadPackageAsync(simOptions.id, simOptions.code, simOptions.dependencies);