Fix promise in loadPackage so it doesn't fire early.

This commit is contained in:
Michael Droettboom 2018-05-10 15:18:32 -04:00
parent d861855034
commit 7c3a26069e
1 changed files with 17 additions and 17 deletions

View File

@ -33,35 +33,35 @@ var languagePluginLoader = new Promise((resolve, reject) => {
} }
let promise = new Promise((resolve, reject) => { let promise = new Promise((resolve, reject) => {
var n = toLoad.size; if (toLoad.size === 0) {
if (n === 0) {
resolve('No new packages to load'); 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) => { toLoad.forEach((package) => {
let script = document.createElement('script'); let script = document.createElement('script');
script.src = `${baseURL}${package}.js`; script.src = `${baseURL}${package}.js`;
console.log(script.src); 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) => { script.onerror = (e) => {
reject(e); reject(e);
}; };
document.body.appendChild(script); 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; return promise;