Browse Source

allow custom namespaces

Evan Almloff 2 years ago
parent
commit
351b9fab6f

+ 0 - 1
Cargo.toml

@@ -44,7 +44,6 @@ publish = false
 dioxus = { path = "./packages/dioxus" }
 dioxus = { path = "./packages/dioxus" }
 dioxus-desktop = { path = "./packages/desktop", features = ["transparent"] }
 dioxus-desktop = { path = "./packages/desktop", features = ["transparent"] }
 dioxus-ssr = { path = "./packages/ssr" }
 dioxus-ssr = { path = "./packages/ssr" }
-dioxus-hot-reload = { path = "./packages/hot-reload" }
 dioxus-router = { path = "./packages/router" }
 dioxus-router = { path = "./packages/router" }
 dioxus-signals = { path = "./packages/signals" }
 dioxus-signals = { path = "./packages/signals" }
 fermi = { path = "./packages/fermi" }
 fermi = { path = "./packages/fermi" }

+ 1 - 0
docs/guide/src/en/getting_started/hot_reload.md

@@ -26,6 +26,7 @@ dioxus serve --hot-reload
 # Desktop/Liveview/TUI
 # Desktop/Liveview/TUI
 
 
 For desktop, LiveView, and tui, you can place the hot reload macro at the top of your main function to enable hot reloading.
 For desktop, LiveView, and tui, you can place the hot reload macro at the top of your main function to enable hot reloading.
+Hot reloading is automatically enabled on debug builds.
 
 
 ## Setup
 ## Setup
 
 

+ 1 - 1
packages/dioxus/src/lib.rs

@@ -33,5 +33,5 @@ pub mod prelude {
     pub use dioxus_elements::{prelude::*, GlobalAttributes, SvgAttributes};
     pub use dioxus_elements::{prelude::*, GlobalAttributes, SvgAttributes};
 
 
     #[cfg(feature = "hot-reload")]
     #[cfg(feature = "hot-reload")]
-    pub use dioxus_hot_reload::hot_reload_init;
+    pub use dioxus_hot_reload::{self, hot_reload_init};
 }
 }

+ 11 - 0
packages/hot-reload/README.md

@@ -97,6 +97,17 @@ fn main(){
 }
 }
 ```
 ```
 
 
+If you are using a namespace other than html, you can implement the [HotReloadingContext](https://docs.rs/dioxus-rsx/latest/dioxus_rsx/trait.HotReloadingContext.html) trait to provide a mapping between the rust names of your elements/attributes and the resultsing strings.
+
+You can then provide the Context to the macro to make hot reloading work with your custom namespace:
+
+```rust
+fn main(){
+    hot_reload_init!(@MyNamespace /*more configeration*/);
+    // launch your application
+}
+```
+
 ## Implementing hot reloading for a custom renderer
 ## Implementing hot reloading for a custom renderer
 
 
 To add hot reloading support to your custom renderer you can use the connect function. This will connect to the dev server you just need to provide a way to transfer `Template`s to the `VirtualDom`. Once you implement this your users can use the hot_reload_init function just like any other render.
 To add hot reloading support to your custom renderer you can use the connect function. This will connect to the dev server you just need to provide a way to transfer `Template`s to the `VirtualDom`. Once you implement this your users can use the hot_reload_init function just like any other render.

+ 28 - 11
packages/hot-reload/src/lib.rs

@@ -6,17 +6,26 @@ use std::{
 };
 };
 
 
 use dioxus_core::Template;
 use dioxus_core::Template;
-use dioxus_html::HtmlCtx;
-use dioxus_rsx::hot_reload::{FileMap, UpdateResult};
+use dioxus_rsx::{
+    hot_reload::{FileMap, UpdateResult},
+    HotReloadingContext,
+};
 use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
 use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
 use notify::{RecommendedWatcher, RecursiveMode, Watcher};
 use notify::{RecommendedWatcher, RecursiveMode, Watcher};
 
 
+#[cfg(debug_assertions)]
+pub use dioxus_html::HtmlCtx;
+
 /// Initialize the hot reloading listener on the given path
 /// Initialize the hot reloading listener on the given path
-pub fn init(root_path: &'static str, listening_paths: &'static [&'static str], log: bool) {
+pub fn init<Ctx: HotReloadingContext + Send + 'static>(
+    root_path: &'static str,
+    listening_paths: &'static [&'static str],
+    log: bool,
+) {
     if let Ok(crate_dir) = PathBuf::from_str(root_path) {
     if let Ok(crate_dir) = PathBuf::from_str(root_path) {
         let temp_file = std::env::temp_dir().join("@dioxusin");
         let temp_file = std::env::temp_dir().join("@dioxusin");
         let channels = Arc::new(Mutex::new(Vec::new()));
         let channels = Arc::new(Mutex::new(Vec::new()));
-        let file_map = Arc::new(Mutex::new(FileMap::<HtmlCtx>::new(crate_dir.clone())));
+        let file_map = Arc::new(Mutex::new(FileMap::<Ctx>::new(crate_dir.clone())));
         if let Ok(local_socket_stream) = LocalSocketListener::bind(temp_file.as_path()) {
         if let Ok(local_socket_stream) = LocalSocketListener::bind(temp_file.as_path()) {
             // listen for connections
             // listen for connections
             std::thread::spawn({
             std::thread::spawn({
@@ -159,25 +168,33 @@ pub fn connect(mut f: impl FnMut(Template<'static>) + Send + 'static) {
 /// If no paths are passed, it will listen on the src and examples folders.
 /// If no paths are passed, it will listen on the src and examples folders.
 #[macro_export]
 #[macro_export]
 macro_rules! hot_reload_init {
 macro_rules! hot_reload_init {
-    ($($t: ident)*) => {
+    ($(@ $ctx:ident)? $($t: ident)*) => {
         #[cfg(debug_assertions)]
         #[cfg(debug_assertions)]
-        dioxus_hot_reload::init(core::env!("CARGO_MANIFEST_DIR"), &["src", "examples"], hot_reload_init!(log: $($t)*))
+        dioxus_hot_reload::init::<hot_reload_init!(@ctx: $($ctx)?)>(core::env!("CARGO_MANIFEST_DIR"), &["src", "examples"], hot_reload_init!(@log: $($t)*))
     };
     };
 
 
-    ($($paths: literal),* $(,)? $($t: ident)*) => {
+    ($(@ $ctx:ident)? $($paths: literal),* $(,)? $($t: ident)*) => {
         #[cfg(debug_assertions)]
         #[cfg(debug_assertions)]
-        dioxus_hot_reload::init(core::env!("CARGO_MANIFEST_DIR"), &[$($paths),*], hot_reload_init!(log: $($t)*))
+        dioxus_hot_reload::init::<hot_reload_init!(@ctx: $($ctx)?)>(core::env!("CARGO_MANIFEST_DIR"), &[$($paths),*], hot_reload_init!(@log: $($t)*))
     };
     };
 
 
-    (log:) => {
+    (@log:) => {
         false
         false
     };
     };
 
 
-    (log: enable logging) => {
+    (@log: enable logging) => {
         true
         true
     };
     };
 
 
-    (log: disable logging) => {
+    (@log: disable logging) => {
         false
         false
     };
     };
+
+    (@ctx: $ctx: ident) => {
+        $ctx
+    };
+
+    (@ctx: ) => {
+        dioxus_hot_reload::HtmlCtx
+    };
 }
 }