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

feat(event streaming): configurable worker path, use SharedWorker #2080

Merged
merged 12 commits into from
Mar 7, 2024
9 changes: 9 additions & 0 deletions mm2src/mm2_event_stream/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use serde::Deserialize;
use std::collections::HashMap;
#[cfg(target_arch = "wasm32")] use std::path::PathBuf;

#[cfg(target_arch = "wasm32")]
const DEFAULT_WORKER_PATH: &str = "event_streaming_worker.js";

/// Multi-purpose/generic event type that can easily be used over the event streaming
pub struct Event {
Expand Down Expand Up @@ -34,6 +38,9 @@ pub struct EventStreamConfiguration {
pub access_control_allow_origin: String,
#[serde(default)]
active_events: HashMap<String, EventConfig>,
/// The path to the worker script for event streaming.
#[cfg(target_arch = "wasm32")]
pub worker_path: PathBuf,
}

/// Represents the configuration for a specific event within the event stream.
Expand All @@ -51,6 +58,8 @@ impl Default for EventStreamConfiguration {
Self {
access_control_allow_origin: String::from("*"),
active_events: Default::default(),
#[cfg(target_arch = "wasm32")]
worker_path: PathBuf::from(DEFAULT_WORKER_PATH),
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion mm2src/mm2_net/src/wasm_event_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub async fn handle_worker_stream(ctx: MmArc) {
"message": event.message(),
});

let worker = web_sys::Worker::new("worker.js").expect("Missing worker.js");
let worker_path = config
.worker_path
.to_str()
.expect("worker_path contains invalid UTF-8 characters");
let worker = web_sys::Worker::new(worker_path).unwrap_or_else(|_| panic!("Missing {}", worker_path));
let message_js = wasm_bindgen::JsValue::from_str(&data.to_string());

worker.post_message(&message_js)
Expand Down
Loading