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

fix(api-base): resolve resource promises in parallel #3

Merged
merged 3 commits into from
Jan 6, 2024
Merged
Changes from 1 commit
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
14 changes: 9 additions & 5 deletions lib/api-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export class BaseApplication {

async _fetchResources() {
// Async import, to allow inlining some of the resources within resource file.
console.log(`[YoWASP runtime] Fetching resource bundle ${this.resourceFileURL}`);
const { modules, filesystem } = await import(this.resourceFileURL);
return {
modules: await this._fetchObject(modules, this._fetchWebAssembly),
Expand All @@ -60,16 +59,21 @@ export class BaseApplication {

async _fetchObject(obj, fetchFn) {
// Mutate the object being fetched, to avoid re-fetches within the same session.
// Do this in parallel to avoid HOL blocking.
let promises = [];
whitequark marked this conversation as resolved.
Show resolved Hide resolved
for (const [key, value] of Object.entries(obj)) {
if (value instanceof URL) {
console.log(`[YoWASP runtime] Fetching resource file ${value}`);
obj[key] = await fetchFn(value);
promises.push(fetchFn(value).then((fetched) => [key, fetched]));
} else if (typeof value === "string" || value instanceof Uint8Array) {
obj[key] = value;
promises.push(Promise.resolve([key, value]));
} else {
obj[key] = await this._fetchObject(value, fetchFn);
promises.push(this._fetchObject(value, fetchFn).then((fetched) => [key, fetched]));
}
}

for (const [key, value] of await Promise.all(promises))
obj[key] = value;

return obj;
}

Expand Down