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:
4 files changed
+15
-17
No files matched your search
@@ -23,13 +23,13 @@ wasm-logger = "0.2.0"
|
|||||||
console_error_panic_hook = { version = "0.1.7", optional = true }
|
console_error_panic_hook = { version = "0.1.7", optional = true }
|
||||||
wasm-bindgen-test = "0.3.29"
|
wasm-bindgen-test = "0.3.29"
|
||||||
once_cell = "1.9.0"
|
once_cell = "1.9.0"
|
||||||
async-channel = "1.6.1"
|
|
||||||
anyhow = "1.0.53"
|
anyhow = "1.0.53"
|
||||||
gloo-timers = { version = "0.2.3", features = ["futures"] }
|
gloo-timers = { version = "0.2.3", features = ["futures"] }
|
||||||
futures-util = "0.3.19"
|
futures-util = "0.3.19"
|
||||||
smallstr = "0.2.0"
|
smallstr = "0.2.0"
|
||||||
dioxus-interpreter-js = { path = "../interpreter", version = "^0.0.0", features = ["web"] }
|
dioxus-interpreter-js = { path = "../interpreter", version = "^0.0.0", features = ["web"] }
|
||||||
serde-wasm-bindgen = "0.4.2"
|
serde-wasm-bindgen = "0.4.2"
|
||||||
|
futures-channel = "0.3.21"
|
||||||
|
|
||||||
[dependencies.web-sys]
|
[dependencies.web-sys]
|
||||||
version = "0.3.56"
|
version = "0.3.56"
|
||||||
|
|||||||
@@ -87,14 +87,11 @@ impl WebsysDom {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// a match here in order to avoid some error during runtime browser test
|
||||||
let document = load_document();
|
let document = load_document();
|
||||||
let root = match document.get_element_by_id(&cfg.rootname) {
|
let root = match document.get_element_by_id(&cfg.rootname) {
|
||||||
Some(root) => root,
|
Some(root) => root,
|
||||||
// a match here in order to avoid some error during runtime browser test
|
None => document.create_element("body").ok().unwrap(),
|
||||||
None => {
|
|
||||||
let body = document.create_element("body").ok().unwrap();
|
|
||||||
body
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@@ -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);
|
websys_dom.apply_edits(edits.edits);
|
||||||
}
|
}
|
||||||
|
|
||||||
let work_loop = ric_raf::RafLoop::new();
|
let mut work_loop = ric_raf::RafLoop::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
log::trace!("waiting for work");
|
log::trace!("waiting for work");
|
||||||
|
|||||||
+11
-10
@@ -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
|
//! 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.
|
//! if RIC is available.
|
||||||
|
|
||||||
|
use futures_util::StreamExt;
|
||||||
use gloo_timers::future::TimeoutFuture;
|
use gloo_timers::future::TimeoutFuture;
|
||||||
use js_sys::Function;
|
use js_sys::Function;
|
||||||
use wasm_bindgen::{prelude::Closure, JsCast, JsValue};
|
use wasm_bindgen::{prelude::Closure, JsCast, JsValue};
|
||||||
@@ -14,21 +15,21 @@ use web_sys::{window, Window};
|
|||||||
|
|
||||||
pub(crate) struct RafLoop {
|
pub(crate) struct RafLoop {
|
||||||
window: Window,
|
window: Window,
|
||||||
ric_receiver: async_channel::Receiver<u32>,
|
ric_receiver: futures_channel::mpsc::UnboundedReceiver<u32>,
|
||||||
raf_receiver: async_channel::Receiver<()>,
|
raf_receiver: futures_channel::mpsc::UnboundedReceiver<()>,
|
||||||
ric_closure: Closure<dyn Fn(JsValue)>,
|
ric_closure: Closure<dyn Fn(JsValue)>,
|
||||||
raf_closure: Closure<dyn Fn(JsValue)>,
|
raf_closure: Closure<dyn Fn(JsValue)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RafLoop {
|
impl RafLoop {
|
||||||
pub fn new() -> Self {
|
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| {
|
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 has_idle_callback = {
|
||||||
let bo = window().unwrap().dyn_into::<js_sys::Object>().unwrap();
|
let bo = window().unwrap().dyn_into::<js_sys::Object>().unwrap();
|
||||||
@@ -45,7 +46,7 @@ impl RafLoop {
|
|||||||
10
|
10
|
||||||
};
|
};
|
||||||
|
|
||||||
ric_sender.try_send(time_remaining).unwrap()
|
ric_sender.unbounded_send(time_remaining).unwrap()
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// execute the polyfill for safari
|
// 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
|
/// 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 ric_fn = self.ric_closure.as_ref().dyn_ref::<Function>().unwrap();
|
||||||
let _cb_id: u32 = self.window.request_idle_callback(ric_fn).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)
|
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 raf_fn = self.raf_closure.as_ref().dyn_ref::<Function>().unwrap();
|
||||||
let _id: i32 = self.window.request_animation_frame(raf_fn).unwrap();
|
let _id: i32 = self.window.request_animation_frame(raf_fn).unwrap();
|
||||||
self.raf_receiver.recv().await.unwrap();
|
self.raf_receiver.next().await.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user