remove async-channel now that context and eval is global

This commit is contained in:
Evan Almloff committed 2024-01-15 11:46:00 -06:00
1 parent 9a38331826
commit cfebb6c900
9 files changed
+33 -52

No files matched your search

Generated
+2 -16
View File
@@ -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",
]
+6 -3
View File
@@ -17,8 +17,11 @@ impl DesktopEvalProvider {
}
impl EvalProvider for DesktopEvalProvider {
fn new_evaluator(&self, js: String) -> Result<Rc<dyn Evaluator>, EvalError> {
Ok(Rc::new(DesktopEvaluator::new(self.desktop_ctx.clone(), js)))
fn new_evaluator(&self, js: String) -> Result<Box<dyn Evaluator>, 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<serde_json::Value, EvalError> {
async fn recv(&mut self) -> Result<serde_json::Value, EvalError> {
self.query
.borrow_mut()
.recv()
+1 -9
View File
@@ -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<RefCell<Vec<MemoryLocation>>>,
}
impl Default for Store {
fn default() -> Self {
Self {
recycled: Default::default(),
}
}
}
impl Store {
fn recycle(&self, location: MemoryLocation) {
location.drop();
+1 -1
View File
@@ -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]
+5 -6
View File
@@ -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<Rc<dyn Evaluator>, EvalError>;
fn new_evaluator(&self, js: String) -> Result<Box<dyn Evaluator>, 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<serde_json::Value, EvalError>;
async fn recv(&mut self) -> Result<serde_json::Value, EvalError>;
/// Gets the return value of the JavaScript
async fn join(&self) -> Result<serde_json::Value, EvalError>;
}
@@ -54,14 +54,13 @@ pub fn eval(script: &str) -> Result<UseEval, EvalError> {
}
/// A wrapper around the target platform's evaluator.
#[derive(Clone)]
pub struct UseEval {
evaluator: Rc<dyn Evaluator + 'static>,
evaluator: Box<dyn Evaluator + 'static>,
}
impl UseEval {
/// Creates a new UseEval
pub fn new(evaluator: Rc<dyn Evaluator + 'static>) -> Self {
pub fn new(evaluator: Box<dyn Evaluator + 'static>) -> 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<serde_json::Value, EvalError> {
pub async fn recv(&mut self) -> Result<serde_json::Value, EvalError> {
self.evaluator.recv().await
}
+3 -3
View File
@@ -20,8 +20,8 @@ pub struct DesktopEvalProvider {
}
impl EvalProvider for DesktopEvalProvider {
fn new_evaluator(&self, js: String) -> Result<Rc<dyn Evaluator>, EvalError> {
Ok(Rc::new(DesktopEvaluator::new(self.query.clone(), js)))
fn new_evaluator(&self, js: String) -> Result<Box<dyn Evaluator>, 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<serde_json::Value, EvalError> {
async fn recv(&mut self) -> Result<serde_json::Value, EvalError> {
self.query
.borrow_mut()
.recv()
-1
View File
@@ -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"
+5 -4
View File
@@ -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"] }
+10 -9
View File
@@ -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<Rc<dyn Evaluator>, EvalError> {
WebEvaluator::new(js).map(|eval| Rc::new(eval) as Rc<dyn Evaluator + 'static>)
fn new_evaluator(&self, js: String) -> Result<Box<dyn Evaluator>, EvalError> {
WebEvaluator::new(js).map(|eval| Box::new(eval) as Box<dyn Evaluator + 'static>)
}
}
@@ -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<serde_json::Value>,
channel_receiver: futures_channel::mpsc::UnboundedReceiver<serde_json::Value>,
result: RefCell<Option<serde_json::Value>>,
}
impl WebEvaluator {
/// Creates a new evaluator for web-based targets.
pub fn new(js: String) -> Result<Self, EvalError> {
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::<dyn FnMut(JsValue)>::new(move |data| {
match serde_wasm_bindgen::from_value::<serde_json::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<serde_json::Value, EvalError> {
async fn recv(&mut self) -> Result<serde_json::Value, EvalError> {
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()))
}
}