Browse Source

set multiple rsx calls at once to prevent duplicated errors

Evan Almloff 3 years ago
parent
commit
75e13749ea
3 changed files with 21 additions and 6 deletions
  1. 3 3
      packages/desktop/src/controller.rs
  2. 15 0
      packages/rsx_interpreter/src/lib.rs
  3. 3 3
      packages/web/src/lib.rs

+ 3 - 3
packages/desktop/src/controller.rs

@@ -54,7 +54,7 @@ impl DesktopController {
                 #[cfg(feature = "hot_reload")]
                 {
                     use dioxus_rsx_interpreter::{
-                        error::Error, ErrorHandler, SetRsxMessage, RSX_CONTEXT,
+                        error::Error, ErrorHandler, SetManyRsxMessage, RSX_CONTEXT,
                     };
                     use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
                     use std::io::{BufRead, BufReader, Write};
@@ -120,9 +120,9 @@ impl DesktopController {
                                 let mut buf = String::new();
                                 match conn.read_line(&mut buf) {
                                     Ok(_) => {
-                                        let message: SetRsxMessage =
+                                        let msgs: SetManyRsxMessage =
                                             serde_json::from_str(&buf).unwrap();
-                                        RSX_CONTEXT.insert(message.location, message.new_text);
+                                        RSX_CONTEXT.extend(msgs);
                                     }
                                     Err(err) => {
                                         if err.kind() != std::io::ErrorKind::WouldBlock {

+ 15 - 0
packages/rsx_interpreter/src/lib.rs

@@ -125,6 +125,17 @@ impl RsxContext {
         }
     }
 
+    /// Set the text for many rsx calls
+    pub fn extend(&self, msg: SetManyRsxMessage) {
+        let mut write = self.data.write().unwrap();
+        for rsx in msg.0 {
+            write.hm.insert(rsx.location, rsx.new_text);
+        }
+        if let Some(channel) = &mut write.scheduler_channel {
+            channel.unbounded_send(SchedulerMsg::DirtyAll).unwrap()
+        }
+    }
+
     fn read(&self) -> RwLockReadGuard<RsxData> {
         self.data.read().unwrap()
     }
@@ -157,3 +168,7 @@ pub struct SetRsxMessage {
     pub location: CodeLocation,
     pub new_text: String,
 }
+
+/// Set many rsx texts at once to avoid duplicate errors
+#[derive(Serialize, Deserialize, Clone, Debug)]
+pub struct SetManyRsxMessage(pub Vec<SetRsxMessage>);

+ 3 - 3
packages/web/src/lib.rs

@@ -220,7 +220,7 @@ pub async fn run_with_props<T: 'static + Send>(root: Component<T>, root_props: T
     #[cfg(feature = "hot_reload")]
     {
         use dioxus_rsx_interpreter::error::Error;
-        use dioxus_rsx_interpreter::{ErrorHandler, SetRsxMessage, RSX_CONTEXT};
+        use dioxus_rsx_interpreter::{ErrorHandler, SetManyRsxMessage, RSX_CONTEXT};
         use futures_channel::mpsc::unbounded;
         use futures_channel::mpsc::UnboundedSender;
         use futures_util::StreamExt;
@@ -246,8 +246,8 @@ pub async fn run_with_props<T: 'static + Send>(root: Component<T>, root_props: T
         // change the rsx when new data is received
         let cl = Closure::wrap(Box::new(|e: MessageEvent| {
             if let Ok(text) = e.data().dyn_into::<js_sys::JsString>() {
-                let msg: SetRsxMessage = serde_json::from_str(&format!("{text}")).unwrap();
-                RSX_CONTEXT.insert(msg.location, msg.new_text);
+                let msgs: SetManyRsxMessage = serde_json::from_str(&format!("{text}")).unwrap();
+                RSX_CONTEXT.extend(msgs);
             }
         }) as Box<dyn FnMut(MessageEvent)>);