Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download bios and image files in parallel #823 #1217

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/browser/starter.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,26 +526,23 @@ V86.prototype.continue_init = async function(emulator, options)

var cont = function(index)
{
if(index === total)
{
setTimeout(done.bind(this), 0);
return;
}

var f = files_to_load[index];

dbg_log("Start loading file: " + f.name);
if(f.loadable)
{
return new Promise((resolve) => {
f.loadable.onload = function(e)
{
put_on_settings.call(this, f.name, f.loadable);
cont(index + 1);
dbg_log("File Loaded: " + f.name);
resolve();
}.bind(this);
f.loadable.load();
});
}
else
{
v86util.load_file(f.url, {
return v86util.load_file_async(f.url, {
done: function(result)
{
if(f.url.endsWith(".zst") && f.name !== "initial_state")
Expand All @@ -555,7 +552,7 @@ V86.prototype.continue_init = async function(emulator, options)
}

put_on_settings.call(this, f.name, f.as_json ? result : new v86util.SyncBuffer(result));
cont(index + 1);
dbg_log("File loaded: " + f.name);
}.bind(this),
progress: function progress(e)
{
Expand Down Expand Up @@ -585,9 +582,8 @@ V86.prototype.continue_init = async function(emulator, options)
});
}
}.bind(this);
cont(0);
await Promise.all(files_to_load.map((_, i) => cont(i)));

async function done()
{
//if(settings.initial_state)
//{
Expand Down
26 changes: 26 additions & 0 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,32 @@ else
v86util.load_file = load_file;
}

v86util.load_file_async = load_file_async;

/**
* @param {string} filename
* @param {Object} options
* @param {number=} n_tries
* @return {Promise}
*/
async function load_file_async(filename, options, n_tries) {
const done = options.done;

return new Promise((resolve) => {
options.done = (result) => {
if(done) {
const r = done(result);
if(r instanceof Promise) {
r.then(resolve);
return;
}
}
resolve();
};
v86util.load_file(filename, options, n_tries);
});
}

/**
* @param {string} filename
* @param {Object} options
Expand Down
Loading