Merge pull request #818 from Demonthos/fix-hot-reloading-cfg

Fix conditional compilation for hot reloading
This commit is contained in:
Jon Kelley 2023-02-07 09:11:25 -08:00 committed by GitHub
commit a1fac25dc6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 14 deletions

View file

@ -9,6 +9,7 @@ use crate::Config;
use crate::WebviewHandler; use crate::WebviewHandler;
use dioxus_core::ScopeState; use dioxus_core::ScopeState;
use dioxus_core::VirtualDom; use dioxus_core::VirtualDom;
#[cfg(all(feature = "hot-reload", debug_assertions))]
use dioxus_hot_reload::HotReloadMsg; use dioxus_hot_reload::HotReloadMsg;
use serde_json::Value; use serde_json::Value;
use slab::Slab; use slab::Slab;
@ -285,6 +286,7 @@ pub enum EventData {
Ipc(IpcMessage), Ipc(IpcMessage),
#[cfg(all(feature = "hot-reload", debug_assertions))]
HotReloadEvent(HotReloadMsg), HotReloadEvent(HotReloadMsg),
NewWindow, NewWindow,

View file

@ -185,6 +185,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, cfg: Config)
} }
Event::UserEvent(event) => match event.0 { Event::UserEvent(event) => match event.0 {
#[cfg(all(feature = "hot-reload", debug_assertions))]
EventData::HotReloadEvent(msg) => match msg { EventData::HotReloadEvent(msg) => match msg {
dioxus_hot_reload::HotReloadMsg::UpdateTemplate(template) => { dioxus_hot_reload::HotReloadMsg::UpdateTemplate(template) => {
for webview in webviews.values_mut() { for webview in webviews.values_mut() {

View file

@ -134,7 +134,7 @@ where
#[cfg(all(feature = "hot-reload", debug_assertions))] #[cfg(all(feature = "hot-reload", debug_assertions))]
let hot_reload_wait = hot_reload_rx.recv(); let hot_reload_wait = hot_reload_rx.recv();
#[cfg(not(all(feature = "hot-reload", debug_assertions)))] #[cfg(not(all(feature = "hot-reload", debug_assertions)))]
let hot_reload_wait = std::future::pending(); let hot_reload_wait: std::future::Pending<Option<()>> = std::future::pending();
tokio::select! { tokio::select! {
// poll any futures or suspense // poll any futures or suspense
@ -157,17 +157,18 @@ where
} }
} }
msg = hot_reload_wait => { Some(msg) = hot_reload_wait => {
if let Some(msg) = msg { #[cfg(all(feature = "hot-reload", debug_assertions))]
match msg{ match msg{
dioxus_hot_reload::HotReloadMsg::UpdateTemplate(new_template) => { dioxus_hot_reload::HotReloadMsg::UpdateTemplate(new_template) => {
vdom.replace_template(new_template); vdom.replace_template(new_template);
}
dioxus_hot_reload::HotReloadMsg::Shutdown => {
std::process::exit(0);
},
} }
dioxus_hot_reload::HotReloadMsg::Shutdown => {
std::process::exit(0);
},
} }
#[cfg(not(all(feature = "hot-reload", debug_assertions)))]
let () = msg;
} }
} }

View file

@ -22,7 +22,7 @@ use std::{
use std::{io, time::Duration}; use std::{io, time::Duration};
use taffy::Taffy; use taffy::Taffy;
pub use taffy::{geometry::Point, prelude::*}; pub use taffy::{geometry::Point, prelude::*};
use tokio::{select, sync::mpsc::unbounded_channel}; use tokio::select;
use tui::{backend::CrosstermBackend, layout::Rect, Terminal}; use tui::{backend::CrosstermBackend, layout::Rect, Terminal};
mod config; mod config;
@ -148,7 +148,7 @@ fn render_vdom(
#[cfg(all(feature = "hot-reload", debug_assertions))] #[cfg(all(feature = "hot-reload", debug_assertions))]
let mut hot_reload_rx = { let mut hot_reload_rx = {
let (hot_reload_tx, hot_reload_rx) = let (hot_reload_tx, hot_reload_rx) =
unbounded_channel::<dioxus_hot_reload::HotReloadMsg>(); tokio::sync::mpsc::unbounded_channel::<dioxus_hot_reload::HotReloadMsg>();
dioxus_hot_reload::connect(move |msg| { dioxus_hot_reload::connect(move |msg| {
let _ = hot_reload_tx.send(msg); let _ = hot_reload_tx.send(msg);
}); });
@ -233,13 +233,14 @@ fn render_vdom(
} }
} }
#[cfg(all(feature = "hot-reload", debug_assertions))]
let mut hot_reload_msg = None; let mut hot_reload_msg = None;
{ {
let wait = vdom.wait_for_work(); let wait = vdom.wait_for_work();
#[cfg(all(feature = "hot-reload", debug_assertions))] #[cfg(all(feature = "hot-reload", debug_assertions))]
let hot_reload_wait = hot_reload_rx.recv(); let hot_reload_wait = hot_reload_rx.recv();
#[cfg(not(all(feature = "hot-reload", debug_assertions)))] #[cfg(not(all(feature = "hot-reload", debug_assertions)))]
let hot_reload_wait = std::future::pending(); let hot_reload_wait: std::future::Pending<Option<()>> = std::future::pending();
pin_mut!(wait); pin_mut!(wait);
@ -269,12 +270,18 @@ fn render_vdom(
} }
}, },
Some(msg) = hot_reload_wait => { Some(msg) = hot_reload_wait => {
hot_reload_msg = Some(msg); #[cfg(all(feature = "hot-reload", debug_assertions))]
{
hot_reload_msg = Some(msg);
}
#[cfg(not(all(feature = "hot-reload", debug_assertions)))]
let () = msg;
} }
} }
} }
// if we have a new template, replace the old one // if we have a new template, replace the old one
#[cfg(all(feature = "hot-reload", debug_assertions))]
if let Some(msg) = hot_reload_msg { if let Some(msg) = hot_reload_msg {
match msg { match msg {
dioxus_hot_reload::HotReloadMsg::UpdateTemplate(template) => { dioxus_hot_reload::HotReloadMsg::UpdateTemplate(template) => {