From 7c3a26069ea66b7f3278f48ea753bbe2434c46f9 Mon Sep 17 00:00:00 2001 From: Michael Droettboom Date: Thu, 10 May 2018 15:18:32 -0400 Subject: [PATCH] Fix promise in loadPackage so it doesn't fire early. --- src/pyodide.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/pyodide.js b/src/pyodide.js index c6691303c..0fb0f366f 100644 --- a/src/pyodide.js +++ b/src/pyodide.js @@ -33,35 +33,35 @@ var languagePluginLoader = new Promise((resolve, reject) => { } let promise = new Promise((resolve, reject) => { - var n = toLoad.size; - - if (n === 0) { + if (toLoad.size === 0) { resolve('No new packages to load'); } + pyodide.monitorRunDependencies = (n) => { + if (n === 0) { + loadedPackages.add.apply(loadedPackages, toLoad); + delete pyodide.monitorRunDependencies; + const packageList = Array.from(toLoad.keys()).join(', '); + resolve(`Loaded ${packageList}`); + } + }; + toLoad.forEach((package) => { let script = document.createElement('script'); script.src = `${baseURL}${package}.js`; console.log(script.src); - script.onload = (e) => { - n--; - loadedPackages.add(package); - if (n <= 0) { - // All of the requested packages are now loaded. - // We have to invalidate Python's import caches, or it won't - // see the new files. - window.pyodide.runPython( - 'import importlib as _importlib\n' + - '_importlib.invalidate_caches()\n'); - const packageList = Array.from(toLoad.keys()).join(', '); - resolve(`Loaded ${packageList}`); - } - }; script.onerror = (e) => { reject(e); }; document.body.appendChild(script); }); + + // We have to invalidate Python's import caches, or it won't + // see the new files. This is done here so it happens in parallel + // with the fetching over the network. + window.pyodide.runPython( + 'import importlib as _importlib\n' + + '_importlib.invalidate_caches()\n'); }); return promise;