Skip to content

Commit 713c152

Browse files
authored
[wasm] callback to make waiting on module load easier (#62904)
* callback to make waiting on module load easier
1 parent 1a83f61 commit 713c152

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/mono/wasm/runtime/cjs/dotnet.cjs.extpost.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ var require = require || undefined;
33
let ENVIRONMENT_IS_GLOBAL = typeof globalThis.Module === "object" && globalThis.__dotnet_runtime === __dotnet_runtime;
44
if (ENVIRONMENT_IS_GLOBAL) {
55
createDotnetRuntime(() => { return globalThis.Module; }).then((exports) => exports);
6+
}
7+
else if (typeof globalThis.__onDotnetRuntimeLoaded === "function") {
8+
globalThis.__onDotnetRuntimeLoaded(createDotnetRuntime);
69
}

src/mono/wasm/test-main.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ loadDotnet("./dotnet.js").then((createDotnetRuntime) => {
160160
}))
161161
}).catch(function (err) {
162162
console.error(err);
163-
set_exit_code(1, "failed to load the dotnet.js file");
164-
throw err;
163+
set_exit_code(1, "failed to load the dotnet.js file.\n" + err);
165164
});
166165

167166
const App = {
@@ -361,10 +360,20 @@ async function loadDotnet(file) {
361360
return require(file);
362361
};
363362
} else if (is_browser) { // vanila JS in browser
364-
loadScript = async function (file) {
365-
globalThis.exports = {}; // if we are loading cjs file
366-
const createDotnetRuntime = await import(file);
367-
return typeof createDotnetRuntime === "function" ? createDotnetRuntime : globalThis.exports.createDotnetRuntime;
363+
loadScript = function (file) {
364+
var loaded = new Promise((resolve, reject) => {
365+
globalThis.__onDotnetRuntimeLoaded = (createDotnetRuntime) => {
366+
// this is callback we have in CJS version of the runtime
367+
resolve(createDotnetRuntime);
368+
};
369+
import(file).then(({ default: createDotnetRuntime }) => {
370+
// this would work with ES6 default export
371+
if (createDotnetRuntime) {
372+
resolve(createDotnetRuntime);
373+
}
374+
}, reject);
375+
});
376+
return loaded;
368377
}
369378
}
370379
else if (typeof globalThis.load !== 'undefined') {

0 commit comments

Comments
 (0)