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

[wasm] Make runtime_is_initialized promise callbacks one-shot #70694

Merged
merged 3 commits into from
Jun 14, 2022
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
23 changes: 18 additions & 5 deletions src/mono/wasm/runtime/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ import { mono_on_abort } from "./run";
import { mono_wasm_new_root } from "./roots";
import { init_crypto } from "./crypto-worker";

export let runtime_is_initialized_resolve: Function;
export let runtime_is_initialized_reject: Function;
export const mono_wasm_runtime_is_initialized = new Promise((resolve, reject) => {
runtime_is_initialized_resolve = resolve;
runtime_is_initialized_reject = reject;
/// Given a function name (for diagnostic purposes) and a function, returns a function that invokes the original function
/// the first time it is called, and otherwise throws an exception.
function oneShot<T extends any[], TRes>(name: string, f: (...args: [...T]) => TRes): (...args: [...T]) => TRes {
let wasCalled = false;
function guardedF(...args: [...T]): TRes {
if (wasCalled)
throw new Error(`${name} called more than once`);
wasCalled = true;
return f(...args);
}
return guardedF;
}

export let runtime_is_initialized_resolve: () => void;
export let runtime_is_initialized_reject: (reason?: any) => void;
export const mono_wasm_runtime_is_initialized = new Promise<void>((resolve, reject) => {
runtime_is_initialized_resolve = oneShot("resolve", resolve);
runtime_is_initialized_reject = oneShot("reject", reject);
});

let ctx: DownloadAssetsContext | null = null;
Expand Down