소스 검색

change rebuild_handler to rebuild_command

Evan Almloff 2 년 전
부모
커밋
31e21aaa79
2개의 변경된 파일14개의 추가작업 그리고 3개의 파일을 삭제
  1. 9 0
      packages/hot-reload/README.md
  2. 5 3
      packages/hot-reload/src/lib.rs

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

@@ -97,6 +97,15 @@ fn main(){
 }
 ```
 
+To rebuild the application when the logic changes, you can use the `with_rebuild_command` function on the config builder. This command will be called when hot reloading fails to quickly update the rsx:
+
+```rust
+fn main(){
+    hot_reload_init!(Config::new().with_rebuild_command("cargo run"));
+    // launch your application
+}
+```
+
 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 resulting strings.
 
 You can then provide the Context to the builder to make hot reloading work with your custom namespace:

+ 5 - 3
packages/hot-reload/src/lib.rs

@@ -80,7 +80,7 @@ impl<Ctx: HotReloadingContext> Config<Ctx> {
     /// Set the command to run to rebuild the project
     ///
     /// For example to restart the application after a change is made, you could use `cargo run`
-    pub const fn with_rebuild_handler(self, rebuild_with: &'static str) -> Self {
+    pub const fn with_rebuild_command(self, rebuild_with: &'static str) -> Self {
         Self {
             rebuild_with: Some(rebuild_with),
             ..self
@@ -198,7 +198,8 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
                         }
                         execute::shell(rebuild_command)
                             .spawn()
-                            .expect("Failed to rebuild the application. Is cargo installed?");
+                            .expect("Failed to spawn the rebuild command");
+
                         for channel in &mut *channels.lock().unwrap() {
                             send_msg(HotReloadMsg::Shutdown, channel);
                         }
@@ -233,11 +234,12 @@ pub fn init<Ctx: HotReloadingContext + Send + 'static>(cfg: Config<Ctx>) {
 
                             // Give time for the change to take effect before reading the file
                             if !real_paths.is_empty() {
-                                std::thread::sleep(std::time::Duration::from_millis(100));
+                                std::thread::sleep(std::time::Duration::from_millis(10));
                             }
 
                             let mut channels = channels.lock().unwrap();
                             for path in real_paths {
+                                println!("File changed: {:?}", path);
                                 // if this file type cannot be hot reloaded, rebuild the application
                                 if path.extension().and_then(|p| p.to_str()) != Some("rs") {
                                     rebuild();