Skip to content

Commit

Permalink
Use wasm_bindgen::link_to! in wasm-bindgen-futures
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaslihotzki committed Sep 5, 2022
1 parent df9ce6f commit 1fad8d2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
18 changes: 3 additions & 15 deletions crates/futures/src/task/wait_async_polyfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,13 @@
* when possible. The worker communicates with its parent using postMessage.
*/

use js_sys::{encode_uri_component, Array, Promise};
use js_sys::{Array, Promise};
use std::cell::RefCell;
use std::sync::atomic::AtomicI32;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{MessageEvent, Worker};

const HELPER_CODE: &'static str = "
onmessage = function (ev) {
let [ia, index, value] = ev.data;
ia = new Int32Array(ia.buffer);
let result = Atomics.wait(ia, index, value);
postMessage(result);
};
";

thread_local! {
static HELPERS: RefCell<Vec<Worker>> = RefCell::new(vec![]);
}
Expand All @@ -62,11 +53,8 @@ fn alloc_helper() -> Worker {
return helper;
}

let mut initialization_string = "data:application/javascript,".to_owned();
let encoded: String = encode_uri_component(HELPER_CODE).into();
initialization_string.push_str(&encoded);

Worker::new(&initialization_string).unwrap_or_else(|js| wasm_bindgen::throw_val(js))
let worker_url = wasm_bindgen::link_to!(module = "/src/task/worker.js").unwrap();
Worker::new(&worker_url).unwrap_or_else(|js| wasm_bindgen::throw_val(js))
})
}

Expand Down
6 changes: 6 additions & 0 deletions crates/futures/src/task/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
onmessage = function (ev) {
let [ia, index, value] = ev.data;
ia = new Int32Array(ia.buffer);
let result = Atomics.wait(ia, index, value);
postMessage(result);
};

0 comments on commit 1fad8d2

Please sign in to comment.