Skip to content

Commit

Permalink
improve non_blocking provider
Browse files Browse the repository at this point in the history
  • Loading branch information
kokoISnoTarget committed Sep 5, 2024
1 parent a88f239 commit 407942e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion packages/dioxus-blitz/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<Doc: DocumentLike> ApplicationHandler<BlitzEvent> for Application<Doc> {

// Initialise pending windows
for window_config in self.pending_windows.drain(..) {
let mut view = View::init(window_config, event_loop, &self.proxy, &self.rt);
let mut view = View::init(window_config, event_loop, &self.proxy);
view.resume(&self.rt);
if !view.renderer.is_active() {
continue;
Expand Down
4 changes: 1 addition & 3 deletions packages/dioxus-blitz/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use blitz_dom::util::Resource;
use blitz_net::AsyncProvider;
use std::sync::Arc;
use std::task::Waker;
use tokio::runtime::Runtime;
use winit::dpi::LogicalSize;
use winit::event::{ElementState, MouseButton};
use winit::event_loop::{ActiveEventLoop, EventLoopProxy};
Expand Down Expand Up @@ -85,11 +84,10 @@ impl<Doc: DocumentLike> View<Doc> {
config: WindowConfig<Doc>,
event_loop: &ActiveEventLoop,
proxy: &EventLoopProxy<BlitzEvent>,
rt: &Runtime,
) -> Self {
let winit_window = Arc::from(event_loop.create_window(config.attributes).unwrap());

rt.spawn(Arc::clone(&config.net).resolve(proxy.clone(), winit_window.id()));
Arc::clone(&config.net).resolve(proxy.clone(), winit_window.id());

// TODO: make this conditional on text input focus
winit_window.set_ime_allowed(true);
Expand Down
2 changes: 1 addition & 1 deletion packages/net/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ tracing = "0.1.40"
thiserror = "1.0.63"

[features]
default = ["non_blocking"]
default = []
blocking = ["dep:ureq"]
non_blocking = ["dep:tokio", "dep:winit", "dep:futures-util", "dep:reqwest"]
38 changes: 20 additions & 18 deletions packages/net/src/provider/non_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,40 +19,42 @@ pub struct AsyncProvider<T> {
client: Client,
futures: Mutex<FuturesUnordered<TaskHandle<T>>>,
}
impl<T: Send + Sync> AsyncProvider<T> {
impl<T: Send + Sync + 'static> AsyncProvider<T> {
pub fn new(rt: &Runtime) -> Self {
Self {
rt: rt.handle().clone(),
client: Client::new(),
futures: Mutex::new(FuturesUnordered::new()),
}
}
pub async fn resolve<P: From<(WindowId, T)>>(
pub fn resolve<P: From<(WindowId, T)> + Send>(
self: Arc<Self>,
event_loop_proxy: EventLoopProxy<P>,
window_id: WindowId,
) {
let mut interval = tokio::time::interval(Duration::from_millis(100));
self.rt.clone().spawn(async move {
let mut interval = tokio::time::interval(Duration::from_millis(100));

'thread: loop {
interval.tick().await;
while let Some(ir) = self.futures.lock().await.next().await {
match ir {
Ok(Ok(t)) => {
let e = event_loop_proxy.send_event((window_id, t).into());
if e.is_err() {
break 'thread;
'thread: loop {
interval.tick().await;
while let Some(ir) = self.futures.lock().await.next().await {
match ir {
Ok(Ok(t)) => {
let e = event_loop_proxy.send_event((window_id, t).into());
if e.is_err() {
break 'thread;
}
}
Ok(Err(e)) => {
tracing::error!("Fetch failed with {e:?}")
}
Err(e) => {
tracing::error!("Fetch thread failed with {e}")
}
}
Ok(Err(e)) => {
tracing::error!("Fetch failed with {e:?}")
}
Err(e) => {
tracing::error!("Fetch thread failed with {e}")
}
}
}
}
});
}
}
impl<T: Send + 'static> AsyncProvider<T> {
Expand Down

0 comments on commit 407942e

Please sign in to comment.