From 9881a94e67551d5992dd367eeb44fdf6f1d85446 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sat, 13 Jan 2024 21:18:36 -0800 Subject: [PATCH] Remove cx in more places --- packages/core/src/lib.rs | 2 +- packages/core/src/mutations.rs | 6 +- packages/core/src/virtual_dom.rs | 14 +- packages/desktop/src/edits.rs | 2 +- packages/fullstack/src/hooks/server_future.rs | 194 +++++++++--------- packages/fullstack/src/router.rs | 4 +- packages/liveview/src/eval.rs | 5 +- packages/liveview/src/pool.rs | 10 +- packages/native-core/tests/fuzzing.rs | 4 +- packages/signals/examples/selector.rs | 8 +- 10 files changed, 122 insertions(+), 127 deletions(-) diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index ccf20f07c..b5db41e4e 100644 --- a/packages/core/src/lib.rs +++ b/packages/core/src/lib.rs @@ -74,7 +74,7 @@ pub(crate) mod innerlude { pub use crate::innerlude::{ fc_to_builder, generation, once, schedule_update, schedule_update_any, vdom_is_rendering, AnyValue, Attribute, AttributeValue, CapturedError, Component, DynamicNode, Element, ElementId, - Event, Fragment, IntoDynNode, Mutation, MutationsVec, NoOpMutations, Properties, RenderReturn, + Event, Fragment, IntoDynNode, Mutation, Mutations, NoOpMutations, Properties, RenderReturn, ScopeId, Task, Template, TemplateAttribute, TemplateNode, VComponent, VNode, VNodeInner, VPlaceholder, VText, VirtualDom, WriteMutations, }; diff --git a/packages/core/src/mutations.rs b/packages/core/src/mutations.rs index 7b00bab3e..9991ff947 100644 --- a/packages/core/src/mutations.rs +++ b/packages/core/src/mutations.rs @@ -327,7 +327,7 @@ pub enum Mutation { /// A static list of mutations that can be applied to the DOM. Note: this list does not contain any `Any` attribute values #[derive(Debug, PartialEq, Default)] -pub struct MutationsVec { +pub struct Mutations { /// The list of Scopes that were diffed, created, and removed during the Diff process. pub dirty_scopes: FxHashSet, @@ -340,7 +340,7 @@ pub struct MutationsVec { pub edits: Vec, } -impl MutationsVec { +impl Mutations { /// Rewrites IDs to just be "template", so you can compare the mutations /// /// Used really only for testing @@ -355,7 +355,7 @@ impl MutationsVec { } } -impl WriteMutations for MutationsVec { +impl WriteMutations for Mutations { fn register_template(&mut self, template: Template) { self.templates.push(template) } diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 2ed432a1e..8d8cdcb0b 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -13,7 +13,7 @@ use crate::{ nodes::{Template, TemplateId}, runtime::{Runtime, RuntimeGuard}, scopes::ScopeId, - AttributeValue, Element, Event, MutationsVec, + AttributeValue, Element, Event, Mutations, }; use futures_util::{pin_mut, StreamExt}; use rustc_hash::{FxHashMap, FxHashSet}; @@ -565,8 +565,8 @@ impl VirtualDom { } /// [`VirtualDom::rebuild`] to a vector of mutations for testing purposes - pub fn rebuild_to_vec(&mut self) -> MutationsVec { - let mut mutations = MutationsVec::default(); + pub fn rebuild_to_vec(&mut self) -> Mutations { + let mut mutations = Mutations::default(); self.rebuild(&mut mutations); mutations } @@ -591,8 +591,8 @@ impl VirtualDom { } /// [`Self::render_immediate`] to a vector of mutations for testing purposes - pub fn render_immediate_to_vec(&mut self) -> MutationsVec { - let mut mutations = MutationsVec::default(); + pub fn render_immediate_to_vec(&mut self) -> Mutations { + let mut mutations = Mutations::default(); self.render_immediate(&mut mutations); mutations } @@ -665,8 +665,8 @@ impl VirtualDom { pub async fn render_with_deadline_to_vec( &mut self, deadline: impl Future, - ) -> MutationsVec { - let mut mutations = MutationsVec::default(); + ) -> Mutations { + let mut mutations = Mutations::default(); self.render_with_deadline(deadline, &mut mutations).await; mutations } diff --git a/packages/desktop/src/edits.rs b/packages/desktop/src/edits.rs index 84607ed48..35da2566f 100644 --- a/packages/desktop/src/edits.rs +++ b/packages/desktop/src/edits.rs @@ -140,7 +140,7 @@ impl EditQueue { // max_template_count.fetch_add(1, Ordering::Relaxed); // } -// pub fn create_template_node(channel: &mut Channel, node: &'static TemplateNode<'static>) { +// pub fn create_template_node(channel: &mut Channel, node: &'static TemplateNode) { // use TemplateNode::*; // match node { // Element { diff --git a/packages/fullstack/src/hooks/server_future.rs b/packages/fullstack/src/hooks/server_future.rs index 0712de9c5..d77987c49 100644 --- a/packages/fullstack/src/hooks/server_future.rs +++ b/packages/fullstack/src/hooks/server_future.rs @@ -23,89 +23,87 @@ use std::sync::Arc; /// /// - dependencies: a tuple of references to values that are PartialEq + Clone #[must_use = "Consider using `cx.spawn` to run a future without reading its value"] -pub fn use_server_future( - dependencies: D, - future: impl FnOnce(D::Out) -> F, -) -> Option<&UseServerFuture> +pub fn use_server_future(future: impl FnOnce() -> F) -> Option> where T: 'static + Serialize + DeserializeOwned + Debug, F: Future + 'static, - D: UseFutureDep, { - let state = cx.use_hook(move || UseServerFuture { - update: cx.schedule_update(), - needs_regen: Cell::new(true), - value: Default::default(), - task: Cell::new(None), - dependencies: Vec::new(), - }); + todo!() - let first_run = { state.value.borrow().as_ref().is_none() && state.task.get().is_none() }; + // let state = use_hook(move || UseServerFuture { + // update: schedule_update(), + // needs_regen: Cell::new(true), + // value: Default::default(), + // task: Cell::new(None), + // dependencies: Vec::new(), + // }); - #[cfg(not(feature = "ssr"))] - { - if first_run { - match crate::html_storage::deserialize::take_server_data() { - Some(data) => { - tracing::trace!("Loaded {data:?} from server"); - *state.value.borrow_mut() = Some(Box::new(data)); - state.needs_regen.set(false); - return Some(state); - } - None => { - tracing::trace!("Failed to load from server... running future"); - } - }; - } - } + // let first_run = { state.value.borrow().as_ref().is_none() && state.task.get().is_none() }; - if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() { - // We don't need regen anymore - state.needs_regen.set(false); + // #[cfg(not(feature = "ssr"))] + // { + // if first_run { + // match crate::html_storage::deserialize::take_server_data() { + // Some(data) => { + // tracing::trace!("Loaded {data:?} from server"); + // *state.value.borrow_mut() = Some(Box::new(data)); + // state.needs_regen.set(false); + // return Some(state); + // } + // None => { + // tracing::trace!("Failed to load from server... running future"); + // } + // }; + // } + // } - // Create the new future - let fut = future(dependencies.out()); + // if dependencies.clone().apply(&mut state.dependencies) || state.needs_regen.get() { + // // We don't need regen anymore + // state.needs_regen.set(false); - // Clone in our cells - let value = state.value.clone(); - let schedule_update = state.update.clone(); + // // Create the new future + // let fut = future(dependencies.out()); - // Cancel the current future - if let Some(current) = state.task.take() { - cx.remove_future(current); - } + // // Clone in our cells + // let value = state.value.clone(); + // let schedule_update = state.update.clone(); - state.task.set(Some(cx.push_future(async move { - let data; - #[cfg(feature = "ssr")] - { - data = fut.await; - if first_run { - if let Err(err) = crate::prelude::server_context().push_html_data(&data) { - tracing::error!("Failed to push HTML data: {}", err); - }; - } - } - #[cfg(not(feature = "ssr"))] - { - data = fut.await; - } - *value.borrow_mut() = Some(Box::new(data)); + // // Cancel the current future + // if let Some(current) = state.task.take() { + // cx.remove_future(current); + // } - schedule_update(); - }))); - } + // state.task.set(Some(cx.push_future(async move { + // let data; + // #[cfg(feature = "ssr")] + // { + // data = fut.await; + // if first_run { + // if let Err(err) = crate::prelude::server_context().push_html_data(&data) { + // tracing::error!("Failed to push HTML data: {}", err); + // }; + // } + // } + // #[cfg(not(feature = "ssr"))] + // { + // data = fut.await; + // } + // *value.borrow_mut() = Some(Box::new(data)); - if first_run { - #[cfg(feature = "ssr")] - { - tracing::trace!("Suspending first run of use_server_future"); - cx.suspend(); - } - None - } else { - Some(state) - } + // schedule_update(); + // }))); + // } + + // if first_run { + // #[cfg(feature = "ssr")] + // { + // tracing::trace!("Suspending first run of use_server_future"); + // cx.suspend(); + // } + // None + // } else { + // Some(state) + // } } pub struct UseServerFuture { @@ -117,36 +115,36 @@ pub struct UseServerFuture { } impl UseServerFuture { - /// Restart the future with new dependencies. - /// - /// Will not cancel the previous future, but will ignore any values that it - /// generates. - pub fn restart(&self) { - self.needs_regen.set(true); - (self.update)(); - } + // /// Restart the future with new dependencies. + // /// + // /// Will not cancel the previous future, but will ignore any values that it + // /// generates. + // pub fn restart(&self) { + // self.needs_regen.set(true); + // (self.update)(); + // } - /// Forcefully cancel a future - pub fn cancel(&self) { - if let Some(task) = self.task.take() { - cx.remove_future(task); - } - } + // /// Forcefully cancel a future + // pub fn cancel(&self) { + // if let Some(task) = self.task.take() { + // cx.remove_future(task); + // } + // } - /// Return any value, even old values if the future has not yet resolved. - /// - /// If the future has never completed, the returned value will be `None`. - pub fn value(&self) -> Ref<'_, T> { - Ref::map(self.value.borrow(), |v| v.as_deref().unwrap()) - } + // /// Return any value, even old values if the future has not yet resolved. + // /// + // /// If the future has never completed, the returned value will be `None`. + // pub fn value(&self) -> Ref<'_, T> { + // Ref::map(self.value.borrow(), |v| v.as_deref().unwrap()) + // } - /// Get the ID of the future in Dioxus' internal scheduler - pub fn task(&self) -> Option { - self.task.get() - } + // /// Get the ID of the future in Dioxus' internal scheduler + // pub fn task(&self) -> Option { + // self.task.get() + // } - /// Get the current state of the future. - pub fn reloading(&self) -> bool { - self.task.get().is_some() - } + // /// Get the current state of the future. + // pub fn reloading(&self) -> bool { + // self.task.get().is_some() + // } } diff --git a/packages/fullstack/src/router.rs b/packages/fullstack/src/router.rs index 3f6d2cc4f..398575dce 100644 --- a/packages/fullstack/src/router.rs +++ b/packages/fullstack/src/router.rs @@ -4,7 +4,7 @@ use dioxus::prelude::*; /// Used by the launch macro #[doc(hidden)] -pub fn RouteWithCfg(cx: Scope>) -> Element +pub fn RouteWithCfg(props: FullstackRouterConfig) -> Element where R: dioxus_router::prelude::Routable, ::Err: std::fmt::Display, @@ -14,7 +14,7 @@ where #[cfg(feature = "ssr")] let context = crate::prelude::server_context(); - let cfg = *cx.props; + let cfg = props; render! { dioxus_router::prelude::Router:: { config: move || { diff --git a/packages/liveview/src/eval.rs b/packages/liveview/src/eval.rs index 675b854c2..9783f4d3d 100644 --- a/packages/liveview/src/eval.rs +++ b/packages/liveview/src/eval.rs @@ -1,6 +1,7 @@ #![allow(clippy::await_holding_refcell_ref)] use async_trait::async_trait; +use dioxus_core::prelude::{consume_context, provide_context}; use dioxus_html::prelude::{EvalError, EvalProvider, Evaluator}; use std::{cell::RefCell, rc::Rc}; @@ -8,9 +9,9 @@ use crate::query::{Query, QueryEngine}; /// Provides the DesktopEvalProvider through [`cx.provide_context`]. pub fn init_eval() { - let query = cx.consume_context::().unwrap(); + let query = consume_context::().unwrap(); let provider: Rc = Rc::new(DesktopEvalProvider { query }); - cx.provide_context(provider); + provide_context(provider); } /// Reprents the desktop-target's provider of evaluators. diff --git a/packages/liveview/src/pool.rs b/packages/liveview/src/pool.rs index c466a9e8a..d85b4ed7f 100644 --- a/packages/liveview/src/pool.rs +++ b/packages/liveview/src/pool.rs @@ -5,7 +5,7 @@ use crate::{ query::{QueryEngine, QueryResult}, LiveViewError, }; -use dioxus_core::{prelude::*, BorrowedAttributeValue, Mutations}; +use dioxus_core::{prelude::*, Attribute, AttributeValue, Mutations}; use dioxus_html::{event_bubbles, EventData, HtmlEvent, MountedData, PlatformEventData}; use dioxus_interpreter_js::binary_protocol::Channel; use futures_util::{pin_mut, SinkExt, StreamExt}; @@ -38,7 +38,7 @@ impl LiveViewPool { pub async fn launch( &self, ws: impl LiveViewSocket, - app: fn(Scope<()>) -> Element, + app: fn(()) -> Element, ) -> Result<(), LiveViewError> { self.launch_with_props(ws, app, ()).await } @@ -46,7 +46,7 @@ impl LiveViewPool { pub async fn launch_with_props( &self, ws: impl LiveViewSocket, - app: fn(Scope) -> Element, + app: fn(T) -> Element, props: T, ) -> Result<(), LiveViewError> { self.launch_virtualdom(ws, move || VirtualDom::new_with_props(app, props)) @@ -133,7 +133,7 @@ pub async fn run(mut vdom: VirtualDom, ws: impl LiveViewSocket) -> Result<(), Li let (query_tx, mut query_rx) = tokio::sync::mpsc::unbounded_channel(); let query_engine = QueryEngine::new(query_tx); vdom.base_scope().provide_context(query_engine.clone()); - init_eval(vdom.base_scope()); + init_eval(); // pin the futures so we can use select! pin_mut!(ws); @@ -271,7 +271,7 @@ fn add_template( *max_template_count += 1 } -fn create_template_node(channel: &mut Channel, v: &'static TemplateNode<'static>) { +fn create_template_node(channel: &mut Channel, v: &'static TemplateNode) { use TemplateNode::*; match v { Element { diff --git a/packages/native-core/tests/fuzzing.rs b/packages/native-core/tests/fuzzing.rs index 65ac67a10..e47e0cc47 100644 --- a/packages/native-core/tests/fuzzing.rs +++ b/packages/native-core/tests/fuzzing.rs @@ -40,7 +40,7 @@ fn create_random_template_node( template_idx: &mut usize, attr_idx: &mut usize, depth: usize, -) -> TemplateNode<'static> { +) -> TemplateNode { match rand::random::() % 4 { 0 => { let attrs = { @@ -96,7 +96,7 @@ fn create_random_template_node( } fn generate_paths( - node: &TemplateNode<'static>, + node: &TemplateNode, current_path: &[u8], node_paths: &mut Vec>, attr_paths: &mut Vec>, diff --git a/packages/signals/examples/selector.rs b/packages/signals/examples/selector.rs index bc6daad24..8903ac871 100644 --- a/packages/signals/examples/selector.rs +++ b/packages/signals/examples/selector.rs @@ -15,15 +15,11 @@ fn App() -> Element { onclick: move |_| *signal.write() += 1, "Increase" } - Child { - signal: doubled - } + Child { signal: doubled } } } #[component] fn Child(signal: ReadOnlySignal) -> Element { - render! { - "{signal}" - } + render! { "{signal}" } }