Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow web worker resource to be relative #2086

Merged
merged 8 commits into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/yew-agent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ pub trait Agent: Sized + 'static {
"main.js"
}

/// Indicates whether the name of the resource is relative.
///
/// The default implementation returns `false`, which will cause the result
/// returned by [`Self::name_of_resource`] to be interpreted as an absolute
/// URL. If `true` is returned, it will be interpreted as a relative URL.
fn resource_path_is_relative() -> bool {
false
}

/// Signifies if resource is a module.
/// This has pending browser support.
fn is_module() -> bool {
Expand Down
22 changes: 18 additions & 4 deletions packages/yew-agent/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,29 @@ where
let msg = msg.pack();
worker.post_message_vec(msg);
}

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

let prefix = if resource_is_relative {
pathname
.rfind(|c| c == '/')
.map(|i| &pathname[..i])
.unwrap_or_default()
} 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::resource_path_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::resource_path_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