From cfebb6c900928c24b13b5061aa9e41c06c4015e4 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 15 Jan 2024 11:46:00 -0600 Subject: [PATCH] remove async-channel now that context and eval is global --- Cargo.lock | 18 ++---------------- packages/desktop/src/eval.rs | 9 ++++++--- packages/generational-box/src/lib.rs | 10 +--------- packages/html/Cargo.toml | 2 +- packages/html/src/eval.rs | 11 +++++------ packages/liveview/src/eval.rs | 6 +++--- packages/router/Cargo.toml | 1 - packages/web/Cargo.toml | 9 +++++---- packages/web/src/eval.rs | 19 ++++++++++--------- 9 files changed, 33 insertions(+), 52 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c922003cf..2d31cab18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -318,17 +318,6 @@ dependencies = [ "futures-core", ] -[[package]] -name = "async-channel" -version = "1.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" -dependencies = [ - "concurrent-queue", - "event-listener 2.5.3", - "futures-core", -] - [[package]] name = "async-channel" version = "2.1.1" @@ -1078,7 +1067,7 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a37913e8dc4ddcc604f0c6d3bf2887c995153af3611de9e23c352b44c1b9118" dependencies = [ - "async-channel 2.1.1", + "async-channel", "async-lock 3.3.0", "async-task", "fastrand 2.0.1", @@ -2671,13 +2660,13 @@ dependencies = [ name = "dioxus-html" version = "0.4.3" dependencies = [ - "async-channel 1.9.0", "async-trait", "dioxus-core", "dioxus-html-internal-macro", "dioxus-rsx", "enumset", "euclid", + "futures-channel", "keyboard-types", "rfd", "serde", @@ -2833,7 +2822,6 @@ dependencies = [ "urlencoding", "wasm-bindgen", "wasm-bindgen-test", - "wasm-logger", "web-sys", ] @@ -2913,7 +2901,6 @@ dependencies = [ name = "dioxus-web" version = "0.4.3" dependencies = [ - "async-channel 1.9.0", "async-trait", "console_error_panic_hook", "dioxus", @@ -2936,7 +2923,6 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", - "wasm-logger", "web-sys", ] diff --git a/packages/desktop/src/eval.rs b/packages/desktop/src/eval.rs index 256e3791d..6faf4a1eb 100644 --- a/packages/desktop/src/eval.rs +++ b/packages/desktop/src/eval.rs @@ -17,8 +17,11 @@ impl DesktopEvalProvider { } impl EvalProvider for DesktopEvalProvider { - fn new_evaluator(&self, js: String) -> Result, EvalError> { - Ok(Rc::new(DesktopEvaluator::new(self.desktop_ctx.clone(), js))) + fn new_evaluator(&self, js: String) -> Result, EvalError> { + Ok(Box::new(DesktopEvaluator::new( + self.desktop_ctx.clone(), + js, + ))) } } @@ -58,7 +61,7 @@ impl Evaluator for DesktopEvaluator { } /// Gets an UnboundedReceiver to receive messages from the evaluated JavaScript. - async fn recv(&self) -> Result { + async fn recv(&mut self) -> Result { self.query .borrow_mut() .recv() diff --git a/packages/generational-box/src/lib.rs b/packages/generational-box/src/lib.rs index 51aea0909..e050ee330 100644 --- a/packages/generational-box/src/lib.rs +++ b/packages/generational-box/src/lib.rs @@ -611,19 +611,11 @@ impl Drop for GenerationalRefMutBorrowInfo { } /// Handles recycling generational boxes that have been dropped. Your application should have one store or one store per thread. -#[derive(Clone)] +#[derive(Clone, Default)] pub struct Store { recycled: Rc>>, } -impl Default for Store { - fn default() -> Self { - Self { - recycled: Default::default(), - } - } -} - impl Store { fn recycle(&self, location: MemoryLocation) { location.drop(); diff --git a/packages/html/Cargo.toml b/packages/html/Cargo.toml index 78d0a3caa..88874b162 100644 --- a/packages/html/Cargo.toml +++ b/packages/html/Cargo.toml @@ -23,7 +23,7 @@ async-trait = "0.1.58" serde-value = { version = "0.7.0", optional = true } tokio = { workspace = true, features = ["fs", "io-util"], optional = true } rfd = { version = "0.12", optional = true } -async-channel = "1.8.0" +futures-channel = { workspace = true } serde_json = { version = "1", optional = true } [dependencies.web-sys] diff --git a/packages/html/src/eval.rs b/packages/html/src/eval.rs index 7d5484dca..7c8433baa 100644 --- a/packages/html/src/eval.rs +++ b/packages/html/src/eval.rs @@ -9,7 +9,7 @@ use std::rc::Rc; /// A struct that implements EvalProvider is sent through [`ScopeState`]'s provide_context function /// so that [`use_eval`] can provide a platform agnostic interface for evaluating JavaScript code. pub trait EvalProvider { - fn new_evaluator(&self, js: String) -> Result, EvalError>; + fn new_evaluator(&self, js: String) -> Result, EvalError>; } /// The platform's evaluator. @@ -18,7 +18,7 @@ pub trait Evaluator { /// Sends a message to the evaluated JavaScript. fn send(&self, data: serde_json::Value) -> Result<(), EvalError>; /// Receive any queued messages from the evaluated JavaScript. - async fn recv(&self) -> Result; + async fn recv(&mut self) -> Result; /// Gets the return value of the JavaScript async fn join(&self) -> Result; } @@ -54,14 +54,13 @@ pub fn eval(script: &str) -> Result { } /// A wrapper around the target platform's evaluator. -#[derive(Clone)] pub struct UseEval { - evaluator: Rc, + evaluator: Box, } impl UseEval { /// Creates a new UseEval - pub fn new(evaluator: Rc) -> Self { + pub fn new(evaluator: Box) -> Self { Self { evaluator } } @@ -71,7 +70,7 @@ impl UseEval { } /// Gets an UnboundedReceiver to receive messages from the evaluated JavaScript. - pub async fn recv(&self) -> Result { + pub async fn recv(&mut self) -> Result { self.evaluator.recv().await } diff --git a/packages/liveview/src/eval.rs b/packages/liveview/src/eval.rs index 3a654f08d..860dc96a4 100644 --- a/packages/liveview/src/eval.rs +++ b/packages/liveview/src/eval.rs @@ -20,8 +20,8 @@ pub struct DesktopEvalProvider { } impl EvalProvider for DesktopEvalProvider { - fn new_evaluator(&self, js: String) -> Result, EvalError> { - Ok(Rc::new(DesktopEvaluator::new(self.query.clone(), js))) + fn new_evaluator(&self, js: String) -> Result, EvalError> { + Ok(Box::new(DesktopEvaluator::new(self.query.clone(), js))) } } @@ -65,7 +65,7 @@ impl Evaluator for DesktopEvaluator { /// /// # Panics /// This will panic if the query is currently being awaited. - async fn recv(&self) -> Result { + async fn recv(&mut self) -> Result { self.query .borrow_mut() .recv() diff --git a/packages/router/Cargo.toml b/packages/router/Cargo.toml index 0064b5300..bd0d929bc 100644 --- a/packages/router/Cargo.toml +++ b/packages/router/Cargo.toml @@ -62,4 +62,3 @@ dioxus-router = { path = ".", features = ["web"] } dioxus-web = { workspace = true } gloo = "0.8.0" wasm-bindgen-test = "0.3.33" -wasm-logger = "0.2.0" diff --git a/packages/web/Cargo.toml b/packages/web/Cargo.toml index 4473ef1fe..4d61aa246 100644 --- a/packages/web/Cargo.toml +++ b/packages/web/Cargo.toml @@ -27,9 +27,8 @@ futures-util = { workspace = true, features = ["std", "async-await", "async-awai futures-channel = { workspace = true } serde_json = { version = "1.0" } serde = { version = "1.0" } -serde-wasm-bindgen = "0.5.0" -async-trait = "0.1.58" -async-channel = "1.8.0" +serde-wasm-bindgen = { version = "0.5.0", optional = true } +async-trait = { version = "0.1.58", optional = true } [dependencies.web-sys] version = "0.3.56" @@ -60,6 +59,7 @@ file_engine = [ "web-sys/File", "web-sys/FileList", "web-sys/FileReader", + "async-trait" ] hot_reload = [ "web-sys/MessageEvent", @@ -68,13 +68,14 @@ hot_reload = [ ] eval = [ "dioxus-html/eval", + "serde-wasm-bindgen", + "async-trait" ] [dev-dependencies] dioxus = { workspace = true } wasm-bindgen-test = "0.3.29" dioxus-ssr = { workspace = true} -wasm-logger = "0.2.0" gloo-timers = "0.2.3" gloo-dialogs = "0.1.1" dioxus-web = { path = ".", features = ["hydrate"] } diff --git a/packages/web/src/eval.rs b/packages/web/src/eval.rs index 278e4df93..c1b4a7c6b 100644 --- a/packages/web/src/eval.rs +++ b/packages/web/src/eval.rs @@ -1,6 +1,7 @@ use async_trait::async_trait; use dioxus_core::ScopeId; use dioxus_html::prelude::{EvalError, EvalProvider, Evaluator}; +use futures_util::StreamExt; use js_sys::Function; use serde_json::Value; use std::{cell::RefCell, rc::Rc, str::FromStr}; @@ -15,8 +16,8 @@ pub fn init_eval() { /// Represents the web-target's provider of evaluators. pub struct WebEvalProvider; impl EvalProvider for WebEvalProvider { - fn new_evaluator(&self, js: String) -> Result, EvalError> { - WebEvaluator::new(js).map(|eval| Rc::new(eval) as Rc) + fn new_evaluator(&self, js: String) -> Result, EvalError> { + WebEvaluator::new(js).map(|eval| Box::new(eval) as Box) } } @@ -31,19 +32,19 @@ const PROMISE_WRAPPER: &str = r#" /// Represents a web-target's JavaScript evaluator. pub struct WebEvaluator { dioxus: Dioxus, - channel_receiver: async_channel::Receiver, + channel_receiver: futures_channel::mpsc::UnboundedReceiver, result: RefCell>, } impl WebEvaluator { /// Creates a new evaluator for web-based targets. pub fn new(js: String) -> Result { - let (channel_sender, channel_receiver) = async_channel::unbounded(); + let (mut channel_sender, channel_receiver) = futures_channel::mpsc::unbounded(); // This Rc cloning mess hurts but it seems to work.. let recv_value = Closure::::new(move |data| { match serde_wasm_bindgen::from_value::(data) { - Ok(data) => _ = channel_sender.send_blocking(data), + Ok(data) => _ = channel_sender.start_send(data), Err(e) => { // Can't really do much here. tracing::error!("failed to serialize JsValue to serde_json::Value (eval communication) - {}", e); @@ -85,7 +86,7 @@ impl WebEvaluator { Ok(Self { dioxus, - channel_receiver, + channel_receiver: channel_receiver, result: RefCell::new(Some(result)), }) } @@ -110,11 +111,11 @@ impl Evaluator for WebEvaluator { } /// Gets an UnboundedReceiver to receive messages from the evaluated JavaScript. - async fn recv(&self) -> Result { + async fn recv(&mut self) -> Result { self.channel_receiver - .recv() + .next() .await - .map_err(|_| EvalError::Communication("failed to receive data from js".to_string())) + .ok_or_else(|| EvalError::Communication("failed to receive data from js".to_string())) } }