Skip to content

Commit 09d03ab

Browse files
committed
Use wasm_bindgen::link_to! in wasm-bindgen-futures
1 parent 2da1c9c commit 09d03ab

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

crates/futures/src/task/wait_async_polyfill.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,13 @@
3636
* when possible. The worker communicates with its parent using postMessage.
3737
*/
3838

39-
use js_sys::{encode_uri_component, Array, Promise};
39+
use js_sys::{Array, Promise};
4040
use std::cell::RefCell;
4141
use std::sync::atomic::AtomicI32;
4242
use wasm_bindgen::prelude::*;
4343
use wasm_bindgen::JsCast;
4444
use web_sys::{MessageEvent, Worker};
4545

46-
const HELPER_CODE: &'static str = "
47-
onmessage = function (ev) {
48-
let [ia, index, value] = ev.data;
49-
ia = new Int32Array(ia.buffer);
50-
let result = Atomics.wait(ia, index, value);
51-
postMessage(result);
52-
};
53-
";
54-
5546
thread_local! {
5647
static HELPERS: RefCell<Vec<Worker>> = RefCell::new(vec![]);
5748
}
@@ -62,11 +53,15 @@ fn alloc_helper() -> Worker {
6253
return helper;
6354
}
6455

65-
let mut initialization_string = "data:application/javascript,".to_owned();
66-
let encoded: String = encode_uri_component(HELPER_CODE).into();
67-
initialization_string.push_str(&encoded);
68-
69-
Worker::new(&initialization_string).unwrap_or_else(|js| wasm_bindgen::throw_val(js))
56+
let worker_url = wasm_bindgen::link_to!(inline_js = "
57+
onmessage = function (ev) {
58+
let [ia, index, value] = ev.data;
59+
ia = new Int32Array(ia.buffer);
60+
let result = Atomics.wait(ia, index, value);
61+
postMessage(result);
62+
};
63+
").unwrap();
64+
Worker::new(&worker_url).unwrap_or_else(|js| wasm_bindgen::throw_val(js))
7065
})
7166
}
7267

0 commit comments

Comments
 (0)