Skip to content

Commit

Permalink
allow web worker resource to be relative
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Sep 26, 2021
1 parent 0e4be56 commit bfcd71d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions packages/yew-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ pub trait Agent: Sized + 'static {
"main.js"
}

/// Indicates whether the name of the resource is relative.
fn name_of_resource_is_relative() -> bool {
false
}

/// Signifies if resource is a module.
/// This has pending browser support.
fn is_module() -> bool {
Expand Down
19 changes: 16 additions & 3 deletions packages/yew-agent/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,23 @@ where
worker.post_message_vec(msg);
}

fn worker_new(name_of_resource: &str, is_module: bool) -> Worker {
fn worker_new(name_of_resource: &str, name_is_relative: bool, is_module: bool) -> Worker {
let origin = yew::utils::origin().unwrap();
let script_url = format!("{}/{}", origin, name_of_resource);
let wasm_url = format!("{}/{}", origin, name_of_resource.replace(".js", "_bg.wasm"));
let pathname = yew::utils::window().location().pathname().unwrap();

let prefix = if name_is_relative {
// Location pathname always contains initial '/', so unwrap will never fail.
pathname.rsplit_once('/').unwrap().0
} else {
""
};
let script_url = format!("{}{}/{}", origin, prefix, name_of_resource);
let wasm_url = format!(
"{}{}/{}",
origin,
prefix,
name_of_resource.replace(".js", "_bg.wasm")
);
let array = Array::new();
array.push(
&format!(
Expand Down
3 changes: 2 additions & 1 deletion packages/yew-agent/src/worker/private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ where
};

let name_of_resource = AGN::name_of_resource();
let is_relative = AGN::name_of_resource_is_relative();
let handler_cell = Rc::new(RefCell::new(Some(handler)));

let worker = {
let handler_cell = handler_cell.clone();
let worker = worker_new(name_of_resource, AGN::is_module());
let worker = worker_new(name_of_resource, is_relative, AGN::is_module());
let worker_clone = worker.clone();
worker.set_onmessage_closure(move |data: Vec<u8>| {
if let Some(handler) = handler_cell.borrow().as_ref() {
Expand Down
3 changes: 2 additions & 1 deletion packages/yew-agent/src/worker/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ where
}
};
let name_of_resource = AGN::name_of_resource();
let is_relative = AGN::name_of_resource_is_relative();
let worker = {
let worker = worker_new(name_of_resource, AGN::is_module());
let worker = worker_new(name_of_resource, is_relative, AGN::is_module());
let worker_clone = worker.clone();
worker.set_onmessage_closure(move |data: Vec<u8>| {
handler(data, &worker_clone);
Expand Down

0 comments on commit bfcd71d

Please sign in to comment.