Skip to content

Commit 6986a05

Browse files
authored
Merge pull request fzyzcjy#2609 from fzyzcjy/feat/12807
Support `worker_js_preamble` in web workers
2 parents 1771720 + e875f88 commit 6986a05

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

frb_rust/src/third_party/wasm_bindgen/worker_pool.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use web_sys::{Event, Worker};
2020
pub struct WorkerPool {
2121
state: Rc<PoolState>,
2222
script_src: String,
23+
worker_js_preamble: String,
2324
}
2425

2526
struct PoolState {
@@ -29,6 +30,18 @@ struct PoolState {
2930

3031
#[wasm_bindgen]
3132
impl WorkerPool {
33+
pub fn new(
34+
initial: Option<usize>,
35+
script_src: Option<String>,
36+
worker_js_preamble: Option<String>,
37+
) -> Result<WorkerPool, JsValue> {
38+
Self::new_raw(
39+
initial.unwrap_or_else(get_wasm_hardware_concurrency),
40+
script_src.unwrap_or_else(|| script_path().expect("fail to get script path")),
41+
worker_js_preamble.unwrap_or_default(),
42+
)
43+
}
44+
3245
/// Creates a new `WorkerPool` which immediately creates `initial` workers.
3346
///
3447
/// The pool created here can be used over a long period of time, and it
@@ -40,7 +53,11 @@ impl WorkerPool {
4053
/// Returns any error that may happen while a JS web worker is created and a
4154
/// message is sent to it.
4255
#[wasm_bindgen(constructor)]
43-
pub fn new(initial: usize, script_src: String) -> Result<WorkerPool, JsValue> {
56+
pub fn new_raw(
57+
initial: usize,
58+
script_src: String,
59+
worker_js_preamble: String,
60+
) -> Result<WorkerPool, JsValue> {
4461
let pool = WorkerPool {
4562
script_src,
4663
state: Rc::new(PoolState {
@@ -53,6 +70,7 @@ impl WorkerPool {
5370
}
5471
}),
5572
}),
73+
worker_js_preamble,
5674
};
5775
for _ in 0..initial {
5876
let worker = pool.spawn()?;
@@ -72,9 +90,11 @@ impl WorkerPool {
7290
/// Returns any error that may happen while a JS web worker is created and a
7391
/// message is sent to it.
7492
fn spawn(&self) -> Result<Worker, JsValue> {
75-
let src = &self.script_src;
93+
let worker_js_preamble = &self.worker_js_preamble;
94+
let script_src = &self.script_src;
7695
let script = format!(
77-
"importScripts('{}');
96+
"{worker_js_preamble}
97+
importScripts('{script_src}');
7898
const FRB_ACTION_PANIC = 3;
7999
onmessage = event => {{
80100
let init = wasm_bindgen(...event.data).catch(err => {{
@@ -97,7 +117,6 @@ impl WorkerPool {
97117
}}
98118
}}
99119
}}",
100-
src
101120
);
102121
let blob = Blob::new_with_blob_sequence_and_options(
103122
&Array::from_iter([JsValue::from(script)]).into(),
@@ -225,11 +244,7 @@ impl PoolState {
225244

226245
impl Default for WorkerPool {
227246
fn default() -> Self {
228-
Self::new(
229-
get_wasm_hardware_concurrency(),
230-
script_path().expect("fail to get script path"),
231-
)
232-
.expect("fail to create WorkerPool")
247+
Self::new(None, None, None).expect("fail to create WorkerPool")
233248
}
234249
}
235250

0 commit comments

Comments
 (0)