Skip to content

Commit

Permalink
restructured draft for async load using promises
Browse files Browse the repository at this point in the history
  • Loading branch information
impact-maker committed Apr 8, 2024
1 parent b47fc03 commit 99b995e
Showing 1 changed file with 66 additions and 42 deletions.
108 changes: 66 additions & 42 deletions src/library_browser.js
Expand Up @@ -766,57 +766,81 @@ var LibraryBrowser = {
safeSetTimeout(() => _emscripten_run_script(script), millis);
},

// TODO: currently not callable from a pthread, but immediately calls onerror() if not on main thread.
emscripten_async_load_script__deps: ['$UTF8ToString'],
emscripten_async_load_script: (url, onload, onerror) => {
emscripten_async_run_script__deps: ['emscripten_run_script', '$safeSetTimeout'],
emscripten_async_run_script: (script, millis) => {
// TODO: cache these to avoid generating garbage
safeSetTimeout(() => _emscripten_run_script(script), millis);
},


emscripten_async_load_script_promise__deps: ['$UTF8ToString'],
emscripten_async_load_script_promise: (url) => {
url = UTF8ToString(url);
onload = {{{ makeDynCall('v', 'onload') }}};
onerror = {{{ makeDynCall('v', 'onerror') }}};

#if PTHREADS
if (ENVIRONMENT_IS_PTHREAD) {
err(`emscripten_async_load_script("${url}") failed, emscripten_async_load_script is currently not available in pthreads!`);
return onerror ? onerror() : undefined;
}
#endif
#if ASSERTIONS
assert(runDependencies === 0, 'async_load_script must be run when no other dependencies are active');
#endif
{{{ runtimeKeepalivePush() }}}
// Creating a promise for script loading functionality
return new Promise((resolve, reject) => {

#if PTHREADS
if (ENVIRONMENT_IS_PTHREAD) {
err(`emscripten_async_load_script("${url}") failed, emscripten_async_load_script is currently not available in pthreads!`);
return reject('emscripten_async_load_script is not available for pthreads');
}
#endif

#if ASSERTIONS
assert(runDependencies === 0, 'async_load_script must be run when no other dependencies are active');
#endif

{{{ runtimeKeepalivePush() }}}

var loadDone = () => {
{{{ runtimeKeepalivePop() }}}
if (onload) {
if (runDependencies > 0) {
dependenciesFulfilled = onload;
} else {
onload();
}
var loadDone = () => {
{{{ runtimeKeepalivePop() }}}
resolve();
}
}

var loadError = () => {
{{{ runtimeKeepalivePop() }}}
onerror?.();
};
var loadError = () => {
{{{ runtimeKeepalivePop() }}}
reject();
};

#if ENVIRONMENT_MAY_BE_NODE && DYNAMIC_EXECUTION
if (ENVIRONMENT_IS_NODE) {
readAsync(url, (data) => {
eval(data);
loadDone();
}, loadError, false);
return;
}
#endif
#if ENVIRONMENT_MAY_BE_NODE && DYNAMIC_EXECUTION
if (ENVIRONMENT_IS_NODE) {
readAsync(url, (data) => {
eval(data);
loadDone();
}, loadError, false);
return;
}
#endif

var script = document.createElement('script');
script.onload = loadDone;
script.onerror = loadError;
script.src = url;
document.body.appendChild(script);

var script = document.createElement('script');
script.onload = loadDone;
script.onerror = loadError;
script.src = url;
document.body.appendChild(script);
});
},


// Refactored the function to implement the promise based functionality and backward compatibility with callbacks
emscripten_async_load_script__deps: ['emscripten_async_load_script_promise', '$UTF8ToString'],
emscripten_async_load_script: function(url, onload, onerror) {

emscripten_async_load_script_promise(url).then(() => {
if (onload){
onload();
}
}).catch((error) => {
if (onerror) {
onerror();
} else {
err('emscripten_async_load_script error: ${error.message}');
}
});

},

// Runs natively in pthread, no __proxy needed.
emscripten_get_main_loop_timing: (mode, value) => {
if (mode) {{{ makeSetValue('mode', 0, 'Browser.mainLoop.timingMode', 'i32') }}};
Expand Down

0 comments on commit 99b995e

Please sign in to comment.