chore: use broadcast instead of receiver
This commit is contained in:
1 parent
1518f07d7e
commit
819bd3fc24
5 files changed
+16
-21
No files matched your search
@@ -1,4 +1,3 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::eval::EvalResult;
|
use crate::eval::EvalResult;
|
||||||
@@ -42,10 +41,7 @@ pub struct DesktopContext {
|
|||||||
pub proxy: ProxyType,
|
pub proxy: ProxyType,
|
||||||
|
|
||||||
/// The receiver for eval results since eval is async
|
/// The receiver for eval results since eval is async
|
||||||
pub(super) eval_sender: tokio::sync::mpsc::UnboundedSender<Value>,
|
pub(super) eval: tokio::sync::broadcast::Sender<Value>,
|
||||||
|
|
||||||
/// The receiver for eval results since eval is async
|
|
||||||
pub(super) eval_reciever: Rc<RefCell<tokio::sync::mpsc::UnboundedReceiver<Value>>>,
|
|
||||||
|
|
||||||
#[cfg(target_os = "ios")]
|
#[cfg(target_os = "ios")]
|
||||||
pub(crate) views: Rc<RefCell<Vec<*mut objc::runtime::Object>>>,
|
pub(crate) views: Rc<RefCell<Vec<*mut objc::runtime::Object>>>,
|
||||||
@@ -62,14 +58,10 @@ impl std::ops::Deref for DesktopContext {
|
|||||||
|
|
||||||
impl DesktopContext {
|
impl DesktopContext {
|
||||||
pub(crate) fn new(webview: Rc<WebView>, proxy: ProxyType) -> Self {
|
pub(crate) fn new(webview: Rc<WebView>, proxy: ProxyType) -> Self {
|
||||||
let (eval_sender, eval_reciever) = tokio::sync::mpsc::unbounded_channel();
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
webview,
|
webview,
|
||||||
proxy,
|
proxy,
|
||||||
eval_reciever: Rc::new(RefCell::new(eval_reciever)),
|
eval: tokio::sync::broadcast::channel(8).0,
|
||||||
eval_sender,
|
|
||||||
|
|
||||||
#[cfg(target_os = "ios")]
|
#[cfg(target_os = "ios")]
|
||||||
views: Default::default(),
|
views: Default::default(),
|
||||||
}
|
}
|
||||||
@@ -156,9 +148,7 @@ impl DesktopContext {
|
|||||||
log::warn!("Eval script error: {e}");
|
log::warn!("Eval script error: {e}");
|
||||||
}
|
}
|
||||||
|
|
||||||
EvalResult {
|
EvalResult::new(self.eval.clone())
|
||||||
reciever: self.eval_reciever.clone(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Push an objc view to the window
|
/// Push an objc view to the window
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
use crate::use_window;
|
use crate::use_window;
|
||||||
@@ -10,7 +9,13 @@ use std::pin::Pin;
|
|||||||
|
|
||||||
/// A future that resolves to the result of a JavaScript evaluation.
|
/// A future that resolves to the result of a JavaScript evaluation.
|
||||||
pub struct EvalResult {
|
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 {
|
impl IntoFuture for EvalResult {
|
||||||
@@ -20,10 +25,10 @@ impl IntoFuture for EvalResult {
|
|||||||
|
|
||||||
fn into_future(self) -> Self::IntoFuture {
|
fn into_future(self) -> Self::IntoFuture {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
let mut reciever = self.reciever.borrow_mut();
|
let mut reciever = self.broadcast.subscribe();
|
||||||
match reciever.recv().await {
|
match reciever.recv().await {
|
||||||
Some(result) => Ok(result),
|
Ok(result) => Ok(result),
|
||||||
None => Err(serde_json::Error::custom("No result returned")),
|
Err(_) => Err(serde_json::Error::custom("No result returned")),
|
||||||
}
|
}
|
||||||
}) as Pin<Box<dyn Future<Output = Result<serde_json::Value, serde_json::Error>>>>
|
}) as Pin<Box<dyn Future<Output = Result<serde_json::Value, serde_json::Error>>>>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -183,7 +183,7 @@ pub fn launch_with_props<P: 'static>(root: Component<P>, props: P, mut cfg: Conf
|
|||||||
dom.base_scope()
|
dom.base_scope()
|
||||||
.consume_context::<DesktopContext>()
|
.consume_context::<DesktopContext>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.eval_sender
|
.eval
|
||||||
.send(msg.params())
|
.send(msg.params())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
/// (such as another DOM element). Applications are free to interpret a drag and drop interaction in an
|
||||||
/// application-specific way.
|
/// application-specific way.
|
||||||
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct DragData {
|
pub struct DragData {
|
||||||
/// Inherit mouse data
|
/// Inherit mouse data
|
||||||
pub mouse: MouseData,
|
pub mouse: MouseData,
|
||||||
|
|||||||
@@ -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)
|
/// 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))]
|
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
|
||||||
#[derive(Clone, Default, PartialEq)]
|
#[derive(Clone, Default, PartialEq, Eq)]
|
||||||
/// Data associated with a mouse event
|
/// Data associated with a mouse event
|
||||||
///
|
///
|
||||||
/// Do not use the deprecated fields; they may change or become private in the future.
|
/// Do not use the deprecated fields; they may change or become private in the future.
|
||||||
|
|||||||
Reference in new issue
Block a user