From 99738bd9e06b31f36d2a9a793e15c6a7c622f02d Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Sun, 3 Jul 2022 00:07:21 +0900 Subject: [PATCH 1/8] Makes Prepared States to be Rc'ed. --- packages/yew-macro/src/use_prepared_state.rs | 7 ++++++- .../use_prepared_state/feat_hydration_ssr.rs | 12 +++++------ .../hooks/use_prepared_state/feat_ssr.rs | 16 +++++++-------- .../feat_hydration_ssr.rs | 6 +++--- .../hooks/use_transitive_state/feat_ssr.rs | 20 +++++++++---------- .../hooks/use_transitive_state/mod.rs | 2 +- 6 files changed, 34 insertions(+), 29 deletions(-) diff --git a/packages/yew-macro/src/use_prepared_state.rs b/packages/yew-macro/src/use_prepared_state.rs index e860277a9f8..a65dab70099 100644 --- a/packages/yew-macro/src/use_prepared_state.rs +++ b/packages/yew-macro/src/use_prepared_state.rs @@ -61,6 +61,7 @@ impl PreparedState { // Async closure is not stable, so we rewrite it to closure + async block #[cfg(not(feature = "nightly"))] pub fn rewrite_to_closure_with_async_block(&self) -> ExprClosure { + use proc_macro2::Span; use syn::parse_quote; let async_token = match &self.closure.asyncness { @@ -68,7 +69,11 @@ impl PreparedState { None => return self.closure.clone(), }; - let move_token = &self.closure.capture; + // The async block always need to be move so input can be moved into it. + let move_token = self + .closure + .capture + .unwrap_or_else(|| Token![move](Span::call_site())); let body = &self.closure.body; let inner = parse_quote! { diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs index 5e2d880a495..2ab10ac7e19 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs @@ -19,13 +19,13 @@ pub fn use_prepared_state( where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> T, + F: FnOnce(Rc) -> T, { struct HookProvider where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> T, + F: FnOnce(Rc) -> T, { deps: D, f: F, @@ -35,7 +35,7 @@ where where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> T, + F: FnOnce(Rc) -> T, { type Output = SuspensionResult>>; @@ -58,14 +58,14 @@ pub fn use_prepared_state_with_suspension( where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> U, + F: FnOnce(Rc) -> U, U: 'static + Future, { struct HookProvider where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> U, + F: FnOnce(Rc) -> U, U: 'static + Future, { deps: D, @@ -76,7 +76,7 @@ where where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> U, + F: FnOnce(Rc) -> U, U: 'static + Future, { type Output = SuspensionResult>>; diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs index fab2331e0ae..4297cea931d 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs @@ -20,13 +20,13 @@ pub fn use_prepared_state( where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> T, + F: FnOnce(Rc) -> T, { struct HookProvider where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> T, + F: FnOnce(Rc) -> T, { deps: D, f: F, @@ -36,7 +36,7 @@ where where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> T, + F: FnOnce(Rc) -> T, { type Output = SuspensionResult>>; @@ -46,7 +46,7 @@ where let state = { let deps = deps.clone(); - use_memo(move |_| f(&deps), ()).run(ctx) + use_memo(move |_| f(deps), ()).run(ctx) }; let state = PreparedStateBase { @@ -75,14 +75,14 @@ pub fn use_prepared_state_with_suspension( where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> U, + F: FnOnce(Rc) -> U, U: 'static + Future, { struct HookProvider where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> U, + F: FnOnce(Rc) -> U, U: 'static + Future, { deps: D, @@ -93,7 +93,7 @@ where where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: FnOnce(&D) -> U, + F: FnOnce(Rc) -> U, U: 'static + Future, { type Output = SuspensionResult>>; @@ -112,7 +112,7 @@ where let deps = deps.clone(); let result = result.clone(); use_state(move || { - let state_f = f(&deps); + let state_f = f(deps.clone()); spawn_local(async move { let state = state_f.await; diff --git a/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs b/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs index 1fbf5d7a1a5..56d8c0a302f 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs @@ -18,13 +18,13 @@ pub fn use_transitive_state( where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { struct HookProvider where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { deps: D, f: F, @@ -34,7 +34,7 @@ where where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { type Output = SuspensionResult>>; diff --git a/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs b/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs index fdb35bef800..fc0304a2090 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs @@ -14,24 +14,24 @@ pub(super) struct TransitiveStateBase where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { pub state_fn: RefCell>, - pub deps: D, + pub deps: Rc, } impl PreparedState for TransitiveStateBase where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { fn prepare(&self) -> String { let f = self.state_fn.borrow_mut().take().unwrap(); - let state = f(&self.deps); + let state = f(self.deps.clone()); - let state = - bincode::serialize(&(Some(&state), Some(&self.deps))).expect("failed to prepare state"); + let state = bincode::serialize(&(Some(&state), Some(&*self.deps))) + .expect("failed to prepare state"); Base64::encode_string(&state) } @@ -45,13 +45,13 @@ pub fn use_transitive_state( where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { struct HookProvider where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { deps: D, f: F, @@ -61,7 +61,7 @@ where where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, - F: 'static + FnOnce(&D) -> T, + F: 'static + FnOnce(Rc) -> T, { type Output = SuspensionResult>>; @@ -71,7 +71,7 @@ where ctx.next_prepared_state(move |_re_render, _| -> TransitiveStateBase { TransitiveStateBase { state_fn: Some(f).into(), - deps: self.deps, + deps: self.deps.into(), } }); diff --git a/packages/yew/src/functional/hooks/use_transitive_state/mod.rs b/packages/yew/src/functional/hooks/use_transitive_state/mod.rs index f8ef487b5cb..b713dcf5fef 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/mod.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/mod.rs @@ -43,7 +43,7 @@ pub use feat_ssr::*; /// where /// D: Serialize + DeserializeOwned + PartialEq + 'static, /// T: Serialize + DeserializeOwned + 'static, -/// F: 'static + FnOnce(&D) -> T, +/// F: 'static + FnOnce(Rc) -> T, /// # { todo!() } /// ``` /// From 04da27175a6114df799756a1f7debf9022a186a5 Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Sun, 3 Jul 2022 00:22:06 +0900 Subject: [PATCH 2/8] Update example. --- packages/yew/src/functional/hooks/use_prepared_state/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/yew/src/functional/hooks/use_prepared_state/mod.rs b/packages/yew/src/functional/hooks/use_prepared_state/mod.rs index c54f9de9f66..fb887ce220a 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/mod.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/mod.rs @@ -43,7 +43,7 @@ pub use feat_ssr::*; /// where /// D: Serialize + DeserializeOwned + PartialEq + 'static, /// T: Serialize + DeserializeOwned + 'static, -/// F: FnOnce(&D) -> T, +/// F: FnOnce(Rc) -> T, /// # { todo!() } /// ``` /// @@ -69,7 +69,7 @@ pub use feat_ssr::*; /// where /// D: Serialize + DeserializeOwned + PartialEq + 'static, /// T: Serialize + DeserializeOwned + 'static, -/// F: FnOnce(&D) -> U, +/// F: FnOnce(Rc) -> U, /// U: 'static + Future, /// # { todo!() } /// ``` From 8e437905e09759152ea1c3dce19c7945d7c7d7bf Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Sun, 3 Jul 2022 01:03:51 +0900 Subject: [PATCH 3/8] Make prepared states work on none runtime as well. --- packages/yew/src/functional/hooks/mod.rs | 8 -------- .../yew/src/functional/hooks/use_prepared_state/mod.rs | 1 - .../yew/src/functional/hooks/use_transitive_state/mod.rs | 1 - 3 files changed, 10 deletions(-) diff --git a/packages/yew/src/functional/hooks/mod.rs b/packages/yew/src/functional/hooks/mod.rs index be1cf62e4ba..5787cb56c98 100644 --- a/packages/yew/src/functional/hooks/mod.rs +++ b/packages/yew/src/functional/hooks/mod.rs @@ -3,14 +3,10 @@ mod use_context; mod use_effect; mod use_force_update; mod use_memo; -#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))] -#[cfg(any(target_arch = "wasm32", feature = "tokio"))] mod use_prepared_state; mod use_reducer; mod use_ref; mod use_state; -#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))] -#[cfg(any(target_arch = "wasm32", feature = "tokio"))] mod use_transitive_state; pub use use_callback::*; @@ -18,14 +14,10 @@ pub use use_context::*; pub use use_effect::*; pub use use_force_update::*; pub use use_memo::*; -#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))] -#[cfg(any(target_arch = "wasm32", feature = "tokio"))] pub use use_prepared_state::*; pub use use_reducer::*; pub use use_ref::*; pub use use_state::*; -#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))] -#[cfg(any(target_arch = "wasm32", feature = "tokio"))] pub use use_transitive_state::*; use crate::functional::HookContext; diff --git a/packages/yew/src/functional/hooks/use_prepared_state/mod.rs b/packages/yew/src/functional/hooks/use_prepared_state/mod.rs index fb887ce220a..ad69308d890 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/mod.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/mod.rs @@ -89,7 +89,6 @@ pub use feat_ssr::*; /// Whilst async closure is an unstable feature, the procedural macro will rewrite this to a /// closure that returns an async block automatically. You can use this hook with async closure /// in stable Rust. -#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))] pub use use_prepared_state_macro as use_prepared_state; // With SSR. #[doc(hidden)] diff --git a/packages/yew/src/functional/hooks/use_transitive_state/mod.rs b/packages/yew/src/functional/hooks/use_transitive_state/mod.rs index b713dcf5fef..893a175bd4f 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/mod.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/mod.rs @@ -55,7 +55,6 @@ pub use feat_ssr::*; /// You MUST denote the return type of the closure with `|deps| -> ReturnType { ... }`. This /// type is used during client side rendering to deserialize the state prepared on the server /// side. -#[cfg_attr(documenting, doc(cfg(any(target_arch = "wasm32", feature = "tokio"))))] pub use use_transitive_state_macro as use_transitive_state; // With SSR. #[doc(hidden)] From d01d478b3acde7edfea79a1851b8571ae386c1d3 Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Sun, 3 Jul 2022 01:45:41 +0900 Subject: [PATCH 4/8] Remove more feature flags. --- packages/yew/src/functional/mod.rs | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/packages/yew/src/functional/mod.rs b/packages/yew/src/functional/mod.rs index 82b6ba1ac09..01133b5cbd5 100644 --- a/packages/yew/src/functional/mod.rs +++ b/packages/yew/src/functional/mod.rs @@ -27,7 +27,6 @@ use std::rc::Rc; use wasm_bindgen::prelude::*; -#[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(all(feature = "hydration", feature = "ssr"))] use crate::html::RenderMode; use crate::html::{AnyScope, BaseComponent, Context, HtmlResult}; @@ -68,7 +67,6 @@ pub use yew_macro::hook; type ReRender = Rc; /// Primitives of a prepared state hook. -#[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(any(feature = "hydration", feature = "ssr"))] pub(crate) trait PreparedState { #[cfg(feature = "ssr")] @@ -83,7 +81,6 @@ pub(crate) trait Effect { /// A hook context to be passed to hooks. pub struct HookContext { pub(crate) scope: AnyScope, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(all(feature = "hydration", feature = "ssr"))] creation_mode: RenderMode, re_render: ReRender, @@ -91,14 +88,11 @@ pub struct HookContext { states: Vec>, effects: Vec>, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(any(feature = "hydration", feature = "ssr"))] prepared_states: Vec>, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(feature = "hydration")] prepared_states_data: Vec>, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(feature = "hydration")] prepared_state_counter: usize, @@ -111,29 +105,22 @@ impl HookContext { fn new( scope: AnyScope, re_render: ReRender, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] - #[cfg(all(feature = "hydration", feature = "ssr"))] - creation_mode: RenderMode, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] - #[cfg(feature = "hydration")] - prepared_state: Option<&str>, + #[cfg(all(feature = "hydration", feature = "ssr"))] creation_mode: RenderMode, + #[cfg(feature = "hydration")] prepared_state: Option<&str>, ) -> RefCell { RefCell::new(HookContext { scope, re_render, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(all(feature = "hydration", feature = "ssr"))] creation_mode, states: Vec::new(), - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(any(feature = "hydration", feature = "ssr"))] prepared_states: Vec::new(), effects: Vec::new(), - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(feature = "hydration")] prepared_states_data: { match prepared_state { @@ -141,7 +128,6 @@ impl HookContext { None => Vec::new(), } }, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(feature = "hydration")] prepared_state_counter: 0, @@ -187,7 +173,6 @@ impl HookContext { t } - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(any(feature = "hydration", feature = "ssr"))] pub(crate) fn next_prepared_state( &mut self, @@ -220,7 +205,6 @@ impl HookContext { #[inline(always)] fn prepare_run(&mut self) { - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(feature = "hydration")] { self.prepared_state_counter = 0; @@ -277,15 +261,11 @@ impl HookContext { } } - #[cfg(any( - not(feature = "ssr"), - not(any(target_arch = "wasm32", feature = "tokio")) - ))] + #[cfg(not(feature = "ssr"))] fn prepare_state(&self) -> Option { None } - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(feature = "ssr")] fn prepare_state(&self) -> Option { if self.prepared_states.is_empty() { @@ -361,10 +341,8 @@ where hook_ctx: HookContext::new( scope, re_render, - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(all(feature = "hydration", feature = "ssr"))] ctx.creation_mode(), - #[cfg(any(target_arch = "wasm32", feature = "tokio"))] #[cfg(feature = "hydration")] ctx.prepared_state(), ), From 9eee1da3e9a46e69a7b07e6064eb163e06261bfb Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Tue, 19 Jul 2022 21:07:44 +0900 Subject: [PATCH 5/8] Users always have to manually construct dependency into Rc. --- .../hooks/use_prepared_state/feat_hydration.rs | 4 ++-- .../hooks/use_prepared_state/feat_hydration_ssr.rs | 8 ++++---- .../hooks/use_prepared_state/feat_none.rs | 4 ++-- .../hooks/use_prepared_state/feat_ssr.rs | 14 ++++++-------- .../use_transitive_state/feat_hydration_ssr.rs | 4 ++-- .../hooks/use_transitive_state/feat_ssr.rs | 8 ++++---- packages/yew/tests/use_prepared_state.rs | 5 +++-- packages/yew/tests/use_transitive_state.rs | 2 +- 8 files changed, 24 insertions(+), 25 deletions(-) diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs index c6e6fde3ccf..6d122ea7068 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs @@ -40,7 +40,7 @@ async fn decode_base64(_s: &str) -> Result, JsValue> { } #[doc(hidden)] -pub fn use_prepared_state(deps: D) -> impl Hook>>> +pub fn use_prepared_state(deps: Rc) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, @@ -51,7 +51,7 @@ where T: Serialize + DeserializeOwned + 'static, { _marker: PhantomData, - deps: D, + deps: Rc, } impl Hook for HookProvider diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs index 2ab10ac7e19..e11e4eda2cd 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs @@ -14,7 +14,7 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] pub fn use_prepared_state( f: F, - deps: D, + deps: Rc, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -27,7 +27,7 @@ where T: Serialize + DeserializeOwned + 'static, F: FnOnce(Rc) -> T, { - deps: D, + deps: Rc, f: F, } @@ -53,7 +53,7 @@ where #[doc(hidden)] pub fn use_prepared_state_with_suspension( f: F, - deps: D, + deps: Rc, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -68,7 +68,7 @@ where F: FnOnce(Rc) -> U, U: 'static + Future, { - deps: D, + deps: Rc, f: F, } diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs index b1fb795e458..afff4ff0f9b 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs @@ -10,7 +10,7 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] #[hook] -pub fn use_prepared_state(_deps: D) -> SuspensionResult>> +pub fn use_prepared_state(_deps: Rc) -> SuspensionResult>> where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, @@ -20,7 +20,7 @@ where #[doc(hidden)] #[hook] -pub fn use_prepared_state_with_suspension(_deps: D) -> SuspensionResult>> +pub fn use_prepared_state_with_suspension(_deps: Rc) -> SuspensionResult>> where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs index 4297cea931d..2599a0ced58 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs @@ -15,7 +15,7 @@ use crate::suspense::{Suspension, SuspensionResult}; #[doc(hidden)] pub fn use_prepared_state( f: F, - deps: D, + deps: Rc, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -28,7 +28,7 @@ where T: Serialize + DeserializeOwned + 'static, F: FnOnce(Rc) -> T, { - deps: D, + deps: Rc, f: F, } @@ -41,8 +41,7 @@ where type Output = SuspensionResult>>; fn run(self, ctx: &mut HookContext) -> Self::Output { - let f = self.f; - let deps = Rc::new(self.deps); + let Self { f, deps } = self; let state = { let deps = deps.clone(); @@ -70,7 +69,7 @@ where #[doc(hidden)] pub fn use_prepared_state_with_suspension( f: F, - deps: D, + deps: Rc, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -85,7 +84,7 @@ where F: FnOnce(Rc) -> U, U: 'static + Future, { - deps: D, + deps: Rc, f: F, } @@ -99,8 +98,7 @@ where type Output = SuspensionResult>>; fn run(self, ctx: &mut HookContext) -> Self::Output { - let f = self.f; - let deps = Rc::new(self.deps); + let Self { f, deps } = self; let result = use_state(|| { let (s, handle) = Suspension::new(); diff --git a/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs b/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs index 56d8c0a302f..d82c4de2ee3 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs @@ -13,7 +13,7 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] pub fn use_transitive_state( f: F, - deps: D, + deps: Rc, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -26,7 +26,7 @@ where T: Serialize + DeserializeOwned + 'static, F: 'static + FnOnce(Rc) -> T, { - deps: D, + deps: Rc, f: F, } diff --git a/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs b/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs index fc0304a2090..cfcea726341 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs @@ -40,7 +40,7 @@ where #[doc(hidden)] pub fn use_transitive_state( f: F, - deps: D, + deps: Rc, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -53,7 +53,7 @@ where T: Serialize + DeserializeOwned + 'static, F: 'static + FnOnce(Rc) -> T, { - deps: D, + deps: Rc, f: F, } @@ -66,12 +66,12 @@ where type Output = SuspensionResult>>; fn run(self, ctx: &mut HookContext) -> Self::Output { - let f = self.f; + let Self { f, deps } = self; ctx.next_prepared_state(move |_re_render, _| -> TransitiveStateBase { TransitiveStateBase { state_fn: Some(f).into(), - deps: self.deps.into(), + deps, } }); diff --git a/packages/yew/tests/use_prepared_state.rs b/packages/yew/tests/use_prepared_state.rs index 2263aff4f46..3de26d51bde 100644 --- a/packages/yew/tests/use_prepared_state.rs +++ b/packages/yew/tests/use_prepared_state.rs @@ -18,7 +18,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); async fn use_prepared_state_works() { #[function_component] fn Comp() -> HtmlResult { - let ctr = use_prepared_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default(); + let ctr = use_prepared_state!(|_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default(); Ok(html! {
@@ -68,7 +68,8 @@ async fn use_prepared_state_works() { async fn use_prepared_state_with_suspension_works() { #[function_component] fn Comp() -> HtmlResult { - let ctr = use_prepared_state!(async move |_| -> u32 { 12345 }, ())?.unwrap_or_default(); + let ctr = + use_prepared_state!(async move |_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default(); Ok(html! {
diff --git a/packages/yew/tests/use_transitive_state.rs b/packages/yew/tests/use_transitive_state.rs index 3e180d2187d..04030c9deb6 100644 --- a/packages/yew/tests/use_transitive_state.rs +++ b/packages/yew/tests/use_transitive_state.rs @@ -17,7 +17,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); async fn use_transitive_state_works() { #[function_component] fn Comp() -> HtmlResult { - let ctr = use_transitive_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default(); + let ctr = use_transitive_state!(|_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default(); Ok(html! {
From d6bb90dc155d2bf87fd18eec4b78b8e07a9cc7a7 Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Tue, 19 Jul 2022 21:12:44 +0900 Subject: [PATCH 6/8] Import Rc. --- packages/yew/tests/use_prepared_state.rs | 1 + packages/yew/tests/use_transitive_state.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/packages/yew/tests/use_prepared_state.rs b/packages/yew/tests/use_prepared_state.rs index 3de26d51bde..4b6f9f3b080 100644 --- a/packages/yew/tests/use_prepared_state.rs +++ b/packages/yew/tests/use_prepared_state.rs @@ -2,6 +2,7 @@ #![cfg(feature = "hydration")] #![cfg_attr(feature = "nightly", feature(async_closure))] +use std::rc::Rc; use std::time::Duration; mod common; diff --git a/packages/yew/tests/use_transitive_state.rs b/packages/yew/tests/use_transitive_state.rs index 04030c9deb6..dade413d78b 100644 --- a/packages/yew/tests/use_transitive_state.rs +++ b/packages/yew/tests/use_transitive_state.rs @@ -1,6 +1,7 @@ #![cfg(feature = "hydration")] #![cfg(target_arch = "wasm32")] +use std::rc::Rc; use std::time::Duration; mod common; From 81f2fda56fdc05f7cda34d56dd063145e3d95c7d Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Tue, 19 Jul 2022 21:53:25 +0900 Subject: [PATCH 7/8] Revert "Users always have to manually construct dependency into Rc." This reverts commit 9eee1da3e9a46e69a7b07e6064eb163e06261bfb. --- .../hooks/use_prepared_state/feat_hydration.rs | 4 ++-- .../hooks/use_prepared_state/feat_hydration_ssr.rs | 8 ++++---- .../hooks/use_prepared_state/feat_none.rs | 4 ++-- .../hooks/use_prepared_state/feat_ssr.rs | 14 ++++++++------ .../use_transitive_state/feat_hydration_ssr.rs | 4 ++-- .../hooks/use_transitive_state/feat_ssr.rs | 8 ++++---- packages/yew/tests/use_prepared_state.rs | 5 ++--- packages/yew/tests/use_transitive_state.rs | 2 +- 8 files changed, 25 insertions(+), 24 deletions(-) diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs index 6d122ea7068..c6e6fde3ccf 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration.rs @@ -40,7 +40,7 @@ async fn decode_base64(_s: &str) -> Result, JsValue> { } #[doc(hidden)] -pub fn use_prepared_state(deps: Rc) -> impl Hook>>> +pub fn use_prepared_state(deps: D) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, @@ -51,7 +51,7 @@ where T: Serialize + DeserializeOwned + 'static, { _marker: PhantomData, - deps: Rc, + deps: D, } impl Hook for HookProvider diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs index e11e4eda2cd..2ab10ac7e19 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_hydration_ssr.rs @@ -14,7 +14,7 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] pub fn use_prepared_state( f: F, - deps: Rc, + deps: D, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -27,7 +27,7 @@ where T: Serialize + DeserializeOwned + 'static, F: FnOnce(Rc) -> T, { - deps: Rc, + deps: D, f: F, } @@ -53,7 +53,7 @@ where #[doc(hidden)] pub fn use_prepared_state_with_suspension( f: F, - deps: Rc, + deps: D, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -68,7 +68,7 @@ where F: FnOnce(Rc) -> U, U: 'static + Future, { - deps: Rc, + deps: D, f: F, } diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs index afff4ff0f9b..b1fb795e458 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_none.rs @@ -10,7 +10,7 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] #[hook] -pub fn use_prepared_state(_deps: Rc) -> SuspensionResult>> +pub fn use_prepared_state(_deps: D) -> SuspensionResult>> where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, @@ -20,7 +20,7 @@ where #[doc(hidden)] #[hook] -pub fn use_prepared_state_with_suspension(_deps: Rc) -> SuspensionResult>> +pub fn use_prepared_state_with_suspension(_deps: D) -> SuspensionResult>> where D: Serialize + DeserializeOwned + PartialEq + 'static, T: Serialize + DeserializeOwned + 'static, diff --git a/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs b/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs index 2599a0ced58..4297cea931d 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/feat_ssr.rs @@ -15,7 +15,7 @@ use crate::suspense::{Suspension, SuspensionResult}; #[doc(hidden)] pub fn use_prepared_state( f: F, - deps: Rc, + deps: D, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -28,7 +28,7 @@ where T: Serialize + DeserializeOwned + 'static, F: FnOnce(Rc) -> T, { - deps: Rc, + deps: D, f: F, } @@ -41,7 +41,8 @@ where type Output = SuspensionResult>>; fn run(self, ctx: &mut HookContext) -> Self::Output { - let Self { f, deps } = self; + let f = self.f; + let deps = Rc::new(self.deps); let state = { let deps = deps.clone(); @@ -69,7 +70,7 @@ where #[doc(hidden)] pub fn use_prepared_state_with_suspension( f: F, - deps: Rc, + deps: D, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -84,7 +85,7 @@ where F: FnOnce(Rc) -> U, U: 'static + Future, { - deps: Rc, + deps: D, f: F, } @@ -98,7 +99,8 @@ where type Output = SuspensionResult>>; fn run(self, ctx: &mut HookContext) -> Self::Output { - let Self { f, deps } = self; + let f = self.f; + let deps = Rc::new(self.deps); let result = use_state(|| { let (s, handle) = Suspension::new(); diff --git a/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs b/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs index d82c4de2ee3..56d8c0a302f 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/feat_hydration_ssr.rs @@ -13,7 +13,7 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] pub fn use_transitive_state( f: F, - deps: Rc, + deps: D, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -26,7 +26,7 @@ where T: Serialize + DeserializeOwned + 'static, F: 'static + FnOnce(Rc) -> T, { - deps: Rc, + deps: D, f: F, } diff --git a/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs b/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs index cfcea726341..fc0304a2090 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/feat_ssr.rs @@ -40,7 +40,7 @@ where #[doc(hidden)] pub fn use_transitive_state( f: F, - deps: Rc, + deps: D, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -53,7 +53,7 @@ where T: Serialize + DeserializeOwned + 'static, F: 'static + FnOnce(Rc) -> T, { - deps: Rc, + deps: D, f: F, } @@ -66,12 +66,12 @@ where type Output = SuspensionResult>>; fn run(self, ctx: &mut HookContext) -> Self::Output { - let Self { f, deps } = self; + let f = self.f; ctx.next_prepared_state(move |_re_render, _| -> TransitiveStateBase { TransitiveStateBase { state_fn: Some(f).into(), - deps, + deps: self.deps.into(), } }); diff --git a/packages/yew/tests/use_prepared_state.rs b/packages/yew/tests/use_prepared_state.rs index 4b6f9f3b080..77d6aef6a35 100644 --- a/packages/yew/tests/use_prepared_state.rs +++ b/packages/yew/tests/use_prepared_state.rs @@ -19,7 +19,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); async fn use_prepared_state_works() { #[function_component] fn Comp() -> HtmlResult { - let ctr = use_prepared_state!(|_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default(); + let ctr = use_prepared_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default(); Ok(html! {
@@ -69,8 +69,7 @@ async fn use_prepared_state_works() { async fn use_prepared_state_with_suspension_works() { #[function_component] fn Comp() -> HtmlResult { - let ctr = - use_prepared_state!(async move |_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default(); + let ctr = use_prepared_state!(async move |_| -> u32 { 12345 }, ())?.unwrap_or_default(); Ok(html! {
diff --git a/packages/yew/tests/use_transitive_state.rs b/packages/yew/tests/use_transitive_state.rs index dade413d78b..4b077f7a12c 100644 --- a/packages/yew/tests/use_transitive_state.rs +++ b/packages/yew/tests/use_transitive_state.rs @@ -18,7 +18,7 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); async fn use_transitive_state_works() { #[function_component] fn Comp() -> HtmlResult { - let ctr = use_transitive_state!(|_| -> u32 { 12345 }, Rc::new(()))?.unwrap_or_default(); + let ctr = use_transitive_state!(|_| -> u32 { 12345 }, ())?.unwrap_or_default(); Ok(html! {
From 75b9851c063f8595148c37d49c4c0455168d824c Mon Sep 17 00:00:00 2001 From: Kaede Hoshikawa Date: Tue, 19 Jul 2022 21:53:38 +0900 Subject: [PATCH 8/8] Revert "Import Rc." This reverts commit d6bb90dc155d2bf87fd18eec4b78b8e07a9cc7a7. --- packages/yew/tests/use_prepared_state.rs | 1 - packages/yew/tests/use_transitive_state.rs | 1 - 2 files changed, 2 deletions(-) diff --git a/packages/yew/tests/use_prepared_state.rs b/packages/yew/tests/use_prepared_state.rs index 77d6aef6a35..2263aff4f46 100644 --- a/packages/yew/tests/use_prepared_state.rs +++ b/packages/yew/tests/use_prepared_state.rs @@ -2,7 +2,6 @@ #![cfg(feature = "hydration")] #![cfg_attr(feature = "nightly", feature(async_closure))] -use std::rc::Rc; use std::time::Duration; mod common; diff --git a/packages/yew/tests/use_transitive_state.rs b/packages/yew/tests/use_transitive_state.rs index 4b077f7a12c..3e180d2187d 100644 --- a/packages/yew/tests/use_transitive_state.rs +++ b/packages/yew/tests/use_transitive_state.rs @@ -1,7 +1,6 @@ #![cfg(feature = "hydration")] #![cfg(target_arch = "wasm32")] -use std::rc::Rc; use std::time::Duration; mod common;