fix attempted document access in web workers

This commit is contained in:
ading2210 2024-03-03 17:28:41 -05:00
parent d0daa89f9c
commit 550d8759b1
3 changed files with 22 additions and 7 deletions

View file

@ -51,6 +51,13 @@ document.addEventListener("libcurl_load", ()=>{
});
```
Alternatively, the `libcurl.onload` callback can be used.
```js
libcurl.onload = () => {
console.log("libcurl.js ready!");
}
```
Once loaded, there will be a `window.libcurl` object which includes all the API functions. The `libcurl.ready` property can also be used to know if the WASM has loaded.
### Making HTTP Requests:

View file

@ -30,6 +30,7 @@ var event_loop = null;
var active_requests = 0;
var wasm_ready = false;
var version_dict = null;
var api = null;
const libcurl_version = "__library_version__";
function check_loaded(check_websocket) {
@ -267,7 +268,7 @@ async function libcurl_fetch(url, params={}) {
function set_websocket_url(url) {
websocket_url = url;
if (!Module.websocket) {
if (!Module.websocket && ENVIRONMENT_IS_WEB) {
document.addEventListener("libcurl_load", () => {
set_websocket_url(url);
});
@ -292,9 +293,12 @@ function main() {
_init_curl();
set_websocket_url(websocket_url);
if (ENVIRONMENT_IS_WEB) {
let load_event = new Event("libcurl_load");
document.dispatchEvent(load_event);
}
api.onload();
}
function load_wasm(url) {
wasmBinaryFile = url;
@ -303,7 +307,7 @@ function load_wasm(url) {
}
Module.onRuntimeInitialized = main;
return {
api = {
fetch: libcurl_fetch,
set_websocket: set_websocket_url,
load_wasm: load_wasm,
@ -319,7 +323,11 @@ return {
get stdout() {return out},
set stdout(callback) {out = callback},
get stderr() {return err},
set stderr(callback) {err = callback}
}
set stderr(callback) {err = callback},
onload() {}
};
return api;
})()

View file

@ -1,6 +1,6 @@
{
"name": "libcurl.js",
"version": "0.3.8",
"version": "0.3.9",
"description": "An experimental port of libcurl to WebAssembly for use in the browser.",
"main": "libcurl.mjs",
"scripts": {