Merge pull request #253 from DioxusLabs/jk/async-channel

Use futures-channel instead of async-channel in rIC/rAF code
This commit is contained in:
Jonathan Kelley 2022-02-15 11:17:43 -05:00 committed by GitHub
commit 0d17da1e7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 17 deletions

View file

@ -23,13 +23,13 @@ wasm-logger = "0.2.0"
console_error_panic_hook = { version = "0.1.7", optional = true }
wasm-bindgen-test = "0.3.29"
once_cell = "1.9.0"
async-channel = "1.6.1"
anyhow = "1.0.53"
gloo-timers = { version = "0.2.3", features = ["futures"] }
futures-util = "0.3.19"
smallstr = "0.2.0"
dioxus-interpreter-js = { path = "../interpreter", version = "^0.0.0", features = ["web"] }
serde-wasm-bindgen = "0.4.2"
futures-channel = "0.3.21"
[dependencies.web-sys]
version = "0.3.56"

View file

@ -87,14 +87,11 @@ impl WebsysDom {
}
});
// a match here in order to avoid some error during runtime browser test
let document = load_document();
let root = match document.get_element_by_id(&cfg.rootname) {
Some(root) => root,
// a match here in order to avoid some error during runtime browser test
None => {
let body = document.create_element("body").ok().unwrap();
body
}
None => document.create_element("body").ok().unwrap(),
};
Self {

View file

@ -211,7 +211,7 @@ pub async fn run_with_props<T: 'static + Send>(root: Component<T>, root_props: T
websys_dom.apply_edits(edits.edits);
}
let work_loop = ric_raf::RafLoop::new();
let mut work_loop = ric_raf::RafLoop::new();
loop {
log::trace!("waiting for work");

View file

@ -7,6 +7,7 @@
//! Because RIC doesn't work on Safari, we polyfill using the "ricpolyfill.js" file and use some basic detection to see
//! if RIC is available.
use futures_util::StreamExt;
use gloo_timers::future::TimeoutFuture;
use js_sys::Function;
use wasm_bindgen::{prelude::Closure, JsCast, JsValue};
@ -14,21 +15,21 @@ use web_sys::{window, Window};
pub(crate) struct RafLoop {
window: Window,
ric_receiver: async_channel::Receiver<u32>,
raf_receiver: async_channel::Receiver<()>,
ric_receiver: futures_channel::mpsc::UnboundedReceiver<u32>,
raf_receiver: futures_channel::mpsc::UnboundedReceiver<()>,
ric_closure: Closure<dyn Fn(JsValue)>,
raf_closure: Closure<dyn Fn(JsValue)>,
}
impl RafLoop {
pub fn new() -> Self {
let (raf_sender, raf_receiver) = async_channel::unbounded();
let (raf_sender, raf_receiver) = futures_channel::mpsc::unbounded();
let raf_closure: Closure<dyn Fn(JsValue)> = Closure::wrap(Box::new(move |_v: JsValue| {
raf_sender.try_send(()).unwrap()
raf_sender.unbounded_send(()).unwrap()
}));
let (ric_sender, ric_receiver) = async_channel::unbounded();
let (ric_sender, ric_receiver) = futures_channel::mpsc::unbounded();
let has_idle_callback = {
let bo = window().unwrap().dyn_into::<js_sys::Object>().unwrap();
@ -45,7 +46,7 @@ impl RafLoop {
10
};
ric_sender.try_send(time_remaining).unwrap()
ric_sender.unbounded_send(time_remaining).unwrap()
}));
// execute the polyfill for safari
@ -64,16 +65,16 @@ impl RafLoop {
}
}
/// waits for some idle time and returns a timeout future that expires after the idle time has passed
pub async fn wait_for_idle_time(&self) -> TimeoutFuture {
pub async fn wait_for_idle_time(&mut self) -> TimeoutFuture {
let ric_fn = self.ric_closure.as_ref().dyn_ref::<Function>().unwrap();
let _cb_id: u32 = self.window.request_idle_callback(ric_fn).unwrap();
let deadline = self.ric_receiver.recv().await.unwrap();
let deadline = self.ric_receiver.next().await.unwrap();
TimeoutFuture::new(deadline)
}
pub async fn wait_for_raf(&self) {
pub async fn wait_for_raf(&mut self) {
let raf_fn = self.raf_closure.as_ref().dyn_ref::<Function>().unwrap();
let _id: i32 = self.window.request_animation_frame(raf_fn).unwrap();
self.raf_receiver.recv().await.unwrap();
self.raf_receiver.next().await.unwrap();
}
}