Skip to content

Commit

Permalink
fix(compilers): do not reload pyodide if still loading
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Oct 16, 2023
1 parent 4cff525 commit c86d8ee
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/livecodes/languages/python-wasm/lang-python-wasm-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@ window.addEventListener('load', async () => {
scripts.forEach((script) => (code += script.innerHTML + '\n'));

async function main() {
// already loaded
if (livecodes.pyodideLoading === false) return;
livecodes.pyodide = await loadPyodide({
indexURL: pyodideBaseUrl,
// still loading
if (livecodes.pyodideLoading) {
await livecodes.pyodideLoading;
return;
}
// start loading
livecodes.pyodideLoading = new Promise<void>(async (resolve) => {
livecodes.pyodide = await loadPyodide({
indexURL: pyodideBaseUrl,
});
await livecodes.pyodide.loadPackage('micropip');
livecodes.micropip = livecodes.pyodide.pyimport('micropip');
livecodes.pyodideLoading = false;
resolve();
});
await livecodes.pyodide.loadPackage('micropip');
livecodes.micropip = livecodes.pyodide.pyimport('micropip');
livecodes.pyodideLoading = false;
await livecodes.pyodideLoading;
}

async function cleanUp() {
Expand All @@ -34,12 +45,13 @@ window.addEventListener('load', async () => {
livecodes.pyodide.pyodide_py._state.restore_state(livecodes.pyodideState);
} catch (err) {
// if restoring state fails, reload pyodide
livecodes.pyodideLoading = true;
livecodes.pyodideLoading = undefined;
await main();
}
}

async function prepareEnv() {
await pyodideReady;
const patchInput = `
from js import prompt
def input(p):
Expand Down

0 comments on commit c86d8ee

Please sign in to comment.