From 2a1a5b722ec045185cdf274aaf2d1e16834c58cf Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Tue, 17 Apr 2018 10:43:26 -0400 Subject: [PATCH] Use promise for initialization. Use postRun hook to resolve. --- src/pyodide.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/pyodide.js b/src/pyodide.js index b5b47670a..25ce8e7e8 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -1,7 +1,6 @@ - -{ +var languagePluginLoader = new Promise((resolve, reject) => { let baseURL = "{{DEPLOY}}"; - let wasmURL = baseURL + 'pyodide.asm.wasm?x=' + Date.now(); + let wasmURL = `${baseURL}pyodide.asm.wasm?x=${Date.now()}`; let wasmXHR = new XMLHttpRequest(); wasmXHR.open('GET', wasmURL, true); wasmXHR.responseType = 'arraybuffer'; @@ -11,14 +10,21 @@ if (wasmXHR.status === 200 || wasmXHR.status === 0) { Module.wasmBinary = wasmXHR.response; } else { - alert("Couldn't download the pyodide.asm.wasm binary. Response was " + wasmXHR.status); + alert(`Couldn't download the pyodide.asm.wasm binary. Response was ${wasmXHR.status}`); + reject(); } Module.baseURL = baseURL; - var script = document.createElement('script'); - script.onload = function() { window.pyodide = pyodide(Module); }; - script.src = baseURL + "pyodide.asm.js"; + Module.postRun = () => { + resolve(); + } + let script = document.createElement('script'); + script.src = `${baseURL}pyodide.asm.js`; + script.onload = () => { + window.pyodide = pyodide(Module); + }; document.body.appendChild(script); }; wasmXHR.send(null); -} +}); +languagePluginLoader