Skip to content

Commit

Permalink
Store the custom worker URL in a static variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-F-Bryan committed Nov 21, 2023
1 parent 6cee243 commit 816e16a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 22 deletions.
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ mod tasks;
mod utils;
mod ws;

use std::sync::Mutex;

pub use crate::{
container::{Container, Manifest, Volume},
facade::{SpawnConfig, Wasmer, WasmerConfig},
Expand All @@ -25,10 +27,12 @@ pub use crate::{
runtime::Runtime,
};

use once_cell::sync::Lazy;
use wasm_bindgen::prelude::wasm_bindgen;

pub(crate) const USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
pub(crate) const DEFAULT_RUST_LOG: &[&str] = &["warn"];
pub(crate) static CUSTOM_WORKER_URL: Lazy<Mutex<Option<String>>> = Lazy::new(Mutex::default);

#[wasm_bindgen]
pub fn wat2wasm(wat: String) -> Result<js_sys::Uint8Array, utils::Error> {
Expand All @@ -43,5 +47,5 @@ fn on_start() {

#[wasm_bindgen(js_name = setWorkerUrl)]
pub fn set_worker_url(url: js_sys::JsString) {
crate::utils::set_worker_url(url);
*CUSTOM_WORKER_URL.lock().unwrap() = Some(url.into());
}
19 changes: 4 additions & 15 deletions src/tasks/worker_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,24 +148,13 @@ static WORKER_URL: Lazy<String> = Lazy::new(|| {
extern "C" {
#[wasm_bindgen(js_namespace = ["import", "meta"], js_name = url)]
static IMPORT_META_URL: String;

#[wasm_bindgen(js_namespace = ["globalThis"], js_name = customWorkerUrl)]
static CUSTOM_WORKER_URL: Option<String>;

}

tracing::trace!(import_url = IMPORT_META_URL.as_str());
tracing::trace!(
custom_worker_url = CUSTOM_WORKER_URL
.as_ref()
.unwrap_or(&"".to_string())
.as_str()
);
let import_url: String = CUSTOM_WORKER_URL
.to_owned()
.unwrap_or(IMPORT_META_URL.to_string());
let import_url = crate::CUSTOM_WORKER_URL.lock().unwrap();
let import_url = import_url.as_deref().unwrap_or(IMPORT_META_URL.as_str());
tracing::trace!(import_url);

let script = include_str!("worker.js").replace("$IMPORT_META_URL", &import_url);
let script = include_str!("worker.js").replace("$IMPORT_META_URL", import_url);

let blob = web_sys::Blob::new_with_u8_array_sequence_and_options(
Array::from_iter([Uint8Array::from(script.as_bytes())]).as_ref(),
Expand Down
7 changes: 1 addition & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
num::NonZeroUsize,
};

use js_sys::{global, JsString, Promise};
use js_sys::{JsString, Promise};

use wasm_bindgen::{JsCast, JsValue};
use web_sys::{Window, WorkerGlobalScope};
Expand Down Expand Up @@ -252,8 +252,3 @@ pub(crate) fn js_record_of_strings(obj: &js_sys::Object) -> Result<Vec<(String,

Ok(parsed)
}

pub(crate) fn set_worker_url(url: JsString) {
js_sys::Reflect::set(&global(), &JsString::from("customWorkerUrl"), &url)
.expect("Can't set worker url in the global JS scope");
}

0 comments on commit 816e16a

Please sign in to comment.