1
0
Эх сурвалжийг харах

chore: use broadcast instead of receiver

Jonathan Kelley 2 жил өмнө
parent
commit
819bd3fc24

+ 3 - 13
packages/desktop/src/desktop_context.rs

@@ -1,4 +1,3 @@
-use std::cell::RefCell;
 use std::rc::Rc;
 
 use crate::eval::EvalResult;
@@ -42,10 +41,7 @@ pub struct DesktopContext {
     pub proxy: ProxyType,
 
     /// The receiver for eval results since eval is async
-    pub(super) eval_sender: tokio::sync::mpsc::UnboundedSender<Value>,
-
-    /// The receiver for eval results since eval is async
-    pub(super) eval_reciever: Rc<RefCell<tokio::sync::mpsc::UnboundedReceiver<Value>>>,
+    pub(super) eval: tokio::sync::broadcast::Sender<Value>,
 
     #[cfg(target_os = "ios")]
     pub(crate) views: Rc<RefCell<Vec<*mut objc::runtime::Object>>>,
@@ -62,14 +58,10 @@ impl std::ops::Deref for DesktopContext {
 
 impl DesktopContext {
     pub(crate) fn new(webview: Rc<WebView>, proxy: ProxyType) -> Self {
-        let (eval_sender, eval_reciever) = tokio::sync::mpsc::unbounded_channel();
-
         Self {
             webview,
             proxy,
-            eval_reciever: Rc::new(RefCell::new(eval_reciever)),
-            eval_sender,
-
+            eval: tokio::sync::broadcast::channel(8).0,
             #[cfg(target_os = "ios")]
             views: Default::default(),
         }
@@ -156,9 +148,7 @@ impl DesktopContext {
             log::warn!("Eval script error: {e}");
         }
 
-        EvalResult {
-            reciever: self.eval_reciever.clone(),
-        }
+        EvalResult::new(self.eval.clone())
     }
 
     /// Push an objc view to the window

+ 10 - 5
packages/desktop/src/eval.rs

@@ -1,4 +1,3 @@
-use std::cell::RefCell;
 use std::rc::Rc;
 
 use crate::use_window;
@@ -10,7 +9,13 @@ use std::pin::Pin;
 
 /// A future that resolves to the result of a JavaScript evaluation.
 pub struct EvalResult {
-    pub(crate) reciever: Rc<RefCell<tokio::sync::mpsc::UnboundedReceiver<serde_json::Value>>>,
+    pub(crate) broadcast: tokio::sync::broadcast::Sender<serde_json::Value>,
+}
+
+impl EvalResult {
+    pub(crate) fn new(sender: tokio::sync::broadcast::Sender<serde_json::Value>) -> Self {
+        Self { broadcast: sender }
+    }
 }
 
 impl IntoFuture for EvalResult {
@@ -20,10 +25,10 @@ impl IntoFuture for EvalResult {
 
     fn into_future(self) -> Self::IntoFuture {
         Box::pin(async move {
-            let mut reciever = self.reciever.borrow_mut();
+            let mut reciever = self.broadcast.subscribe();
             match reciever.recv().await {
-                Some(result) => Ok(result),
-                None => Err(serde_json::Error::custom("No result returned")),
+                Ok(result) => Ok(result),
+                Err(_) => Err(serde_json::Error::custom("No result returned")),
             }
         }) as Pin<Box<dyn Future<Output = Result<serde_json::Value, serde_json::Error>>>>
     }

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

@@ -183,7 +183,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, mut cfg: Conf
                 dom.base_scope()
                     .consume_context::<DesktopContext>()
                     .unwrap()
-                    .eval_sender
+                    .eval
                     .send(msg.params())
                     .unwrap();
             }

+ 1 - 1
packages/html/src/events/drag.rs

@@ -9,7 +9,7 @@ pub type DragEvent = Event<DragData>;
 /// (such as another DOM element). Applications are free to interpret a drag and drop interaction in an
 /// application-specific way.
 #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Debug, Clone, PartialEq, Eq)]
 pub struct DragData {
     /// Inherit mouse data
     pub mouse: MouseData,

+ 1 - 1
packages/html/src/events/mouse.rs

@@ -10,7 +10,7 @@ pub type MouseEvent = Event<MouseData>;
 
 /// A synthetic event that wraps a web-style [`MouseEvent`](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent)
 #[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
-#[derive(Clone, Default, PartialEq)]
+#[derive(Clone, Default, PartialEq, Eq)]
 /// Data associated with a mouse event
 ///
 /// Do not use the deprecated fields; they may change or become private in the future.