From 42e1890f0322e6e965615588bb8c488fe59924d5 Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Sat, 19 Aug 2023 17:58:36 +0500 Subject: [PATCH] Update signature of use_prepared_state/use_transitive_state (#3376) * Update signature of use_prepared_state * Update signature of use_transitive_state * Migration guide + example * bless trybuild tests * please fmt * there's one usage here as well * use_prepared_state_with_suspension needs updates * updated tests --- examples/simple_ssr/src/lib.rs | 2 +- packages/yew-macro/src/use_prepared_state.rs | 24 +++---- .../yew-macro/src/use_transitive_state.rs | 22 +++--- .../hook_macro/use_prepared_state-fail.rs | 10 ++- .../hook_macro/use_prepared_state-fail.stderr | 72 +++++++++++++------ .../hook_macro/use_transitive_state-fail.rs | 8 +++ .../use_transitive_state-fail.stderr | 64 ++++++++++++----- .../use_prepared_state/feat_hydration_ssr.rs | 8 +-- .../hooks/use_prepared_state/feat_ssr.rs | 4 +- .../hooks/use_prepared_state/mod.rs | 4 +- .../feat_hydration_ssr.rs | 4 +- .../hooks/use_transitive_state/feat_ssr.rs | 2 +- .../hooks/use_transitive_state/mod.rs | 4 +- packages/yew/tests/use_prepared_state.rs | 4 +- packages/yew/tests/use_transitive_state.rs | 2 +- tools/benchmark-ssr/src/main.rs | 9 +-- .../yew/from-0_20_0-to-next.mdx | 6 ++ 17 files changed, 162 insertions(+), 87 deletions(-) diff --git a/examples/simple_ssr/src/lib.rs b/examples/simple_ssr/src/lib.rs index 1204af904..8611b7c0c 100644 --- a/examples/simple_ssr/src/lib.rs +++ b/examples/simple_ssr/src/lib.rs @@ -18,7 +18,7 @@ async fn fetch_uuid() -> Uuid { #[function_component] fn Content() -> HtmlResult { - let uuid = use_prepared_state!(async move |_| -> Uuid { fetch_uuid().await }, ())?.unwrap(); + let uuid = use_prepared_state!((), async move |_| -> Uuid { fetch_uuid().await })?.unwrap(); Ok(html! {
{"Random UUID: "}{uuid}
diff --git a/packages/yew-macro/src/use_prepared_state.rs b/packages/yew-macro/src/use_prepared_state.rs index 737c2368c..ec1a87712 100644 --- a/packages/yew-macro/src/use_prepared_state.rs +++ b/packages/yew-macro/src/use_prepared_state.rs @@ -12,13 +12,8 @@ pub struct PreparedState { impl Parse for PreparedState { fn parse(input: ParseStream) -> syn::Result { - // Reads a closure. - let expr: Expr = input.parse()?; - - let closure = match expr { - Expr::Closure(m) => m, - other => return Err(syn::Error::new_spanned(other, "expected closure")), - }; + // Reads the deps. + let deps = input.parse()?; input.parse::().map_err(|e| { syn::Error::new( @@ -27,6 +22,14 @@ impl Parse for PreparedState { ) })?; + // Reads a closure. + let expr: Expr = input.parse()?; + + let closure = match expr { + Expr::Closure(m) => m, + other => return Err(syn::Error::new_spanned(other, "expected closure")), + }; + let return_type = match &closure.output { ReturnType::Default => { return Err(syn::Error::new_spanned( @@ -38,9 +41,6 @@ impl Parse for PreparedState { ReturnType::Type(_rarrow, ty) => *ty.to_owned(), }; - // Reads the deps. - let deps = input.parse()?; - if !input.is_empty() { let maybe_trailing_comma = input.lookahead1(); @@ -107,10 +107,10 @@ impl PreparedState { match &self.closure.asyncness { Some(_) => quote! { - ::yew::functional::use_prepared_state_with_suspension::<#rt, _, _, _>(#closure, #deps) + ::yew::functional::use_prepared_state_with_suspension::<#rt, _, _, _>(#deps, #closure) }, None => quote! { - ::yew::functional::use_prepared_state::<#rt, _, _>(#closure, #deps) + ::yew::functional::use_prepared_state::<#rt, _, _>(#deps, #closure) }, } } diff --git a/packages/yew-macro/src/use_transitive_state.rs b/packages/yew-macro/src/use_transitive_state.rs index 08bf9fc82..57af33f3a 100644 --- a/packages/yew-macro/src/use_transitive_state.rs +++ b/packages/yew-macro/src/use_transitive_state.rs @@ -12,13 +12,8 @@ pub struct TransitiveState { impl Parse for TransitiveState { fn parse(input: ParseStream) -> syn::Result { - // Reads a closure. - let expr: Expr = input.parse()?; - - let closure = match expr { - Expr::Closure(m) => m, - other => return Err(syn::Error::new_spanned(other, "expected closure")), - }; + // Reads the deps. + let deps = input.parse()?; input.parse::().map_err(|e| { syn::Error::new( @@ -27,6 +22,14 @@ impl Parse for TransitiveState { ) })?; + // Reads a closure. + let expr: Expr = input.parse()?; + + let closure = match expr { + Expr::Closure(m) => m, + other => return Err(syn::Error::new_spanned(other, "expected closure")), + }; + let return_type = match &closure.output { ReturnType::Default => { return Err(syn::Error::new_spanned( @@ -38,9 +41,6 @@ impl Parse for TransitiveState { ReturnType::Type(_rarrow, ty) => *ty.to_owned(), }; - // Reads the deps. - let deps = input.parse()?; - if !input.is_empty() { let maybe_trailing_comma = input.lookahead1(); @@ -64,7 +64,7 @@ impl TransitiveState { let closure = &self.closure; quote! { - ::yew::functional::use_transitive_state::<#rt, _, _>(#closure, #deps) + ::yew::functional::use_transitive_state::<#rt, _, _>(#deps, #closure) } } diff --git a/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.rs b/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.rs index e12ee98fe..b7181f04d 100644 --- a/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.rs +++ b/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.rs @@ -5,12 +5,16 @@ use yew_macro::{use_prepared_state_with_closure, use_prepared_state_without_clos fn Comp() -> HtmlResult { use_prepared_state_with_closure!(123)?; - use_prepared_state_with_closure!(|_| { todo!() }, 123)?; + use_prepared_state_with_closure!(123, |_| { todo!() })?; use_prepared_state_with_closure!(|_| -> u32 { todo!() })?; + use_prepared_state_with_closure!(|_| -> u32 { todo!() }, 123)?; + use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?; + use_prepared_state_with_closure!(|_| { todo!() }, 123)?; + Ok(Html::default()) } @@ -18,10 +22,14 @@ fn Comp() -> HtmlResult { fn Comp2() -> HtmlResult { use_prepared_state_without_closure!(123)?; + use_prepared_state_without_closure!(123, |_| { todo!() })?; + use_prepared_state_without_closure!(|_| { todo!() }, 123)?; use_prepared_state_without_closure!(|_| -> u32 { todo!() })?; + use_prepared_state_without_closure!(|_| -> u32 { todo!() }, 123)?; + use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?; Ok(Html::default()) diff --git a/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr b/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr index 62ba0033a..775071c00 100644 --- a/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr +++ b/packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr @@ -1,14 +1,16 @@ -error: expected closure - --> tests/hook_macro/use_prepared_state-fail.rs:6:38 +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_prepared_state-fail.rs:6:5 | 6 | use_prepared_state_with_closure!(123)?; - | ^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle. - --> tests/hook_macro/use_prepared_state-fail.rs:8:38 + --> tests/hook_macro/use_prepared_state-fail.rs:8:43 | -8 | use_prepared_state_with_closure!(|_| { todo!() }, 123)?; - | ^^^^^^^^^^^^^^^ +8 | use_prepared_state_with_closure!(123, |_| { todo!() })?; + | ^^^^^^^^^^^^^^^ error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_prepared_state-fail.rs:10:5 @@ -18,38 +20,64 @@ error: this hook takes 2 arguments but 1 argument was supplied | = note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) -error: this hook takes 2 arguments but 1 argument was supplied - --> tests/hook_macro/use_prepared_state-fail.rs:12:5 +error: expected closure + --> tests/hook_macro/use_prepared_state-fail.rs:12:62 | -12 | use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?; +12 | use_prepared_state_with_closure!(|_| -> u32 { todo!() }, 123)?; + | ^^^ + +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_prepared_state-fail.rs:14:5 + | +14 | use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the macro `use_prepared_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) error: expected closure - --> tests/hook_macro/use_prepared_state-fail.rs:19:41 + --> tests/hook_macro/use_prepared_state-fail.rs:16:55 | -19 | use_prepared_state_without_closure!(123)?; - | ^^^ - -error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle. - --> tests/hook_macro/use_prepared_state-fail.rs:21:41 - | -21 | use_prepared_state_without_closure!(|_| { todo!() }, 123)?; - | ^^^^^^^^^^^^^^^ +16 | use_prepared_state_with_closure!(|_| { todo!() }, 123)?; + | ^^^ error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_prepared_state-fail.rs:23:5 | -23 | use_prepared_state_without_closure!(|_| -> u32 { todo!() })?; +23 | use_prepared_state_without_closure!(123)?; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `use_prepared_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle. + --> tests/hook_macro/use_prepared_state-fail.rs:25:46 + | +25 | use_prepared_state_without_closure!(123, |_| { todo!() })?; + | ^^^^^^^^^^^^^^^ + +error: expected closure + --> tests/hook_macro/use_prepared_state-fail.rs:27:58 + | +27 | use_prepared_state_without_closure!(|_| { todo!() }, 123)?; + | ^^^ + +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_prepared_state-fail.rs:29:5 + | +29 | use_prepared_state_without_closure!(|_| -> u32 { todo!() })?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the macro `use_prepared_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info) -error: this hook takes 2 arguments but 1 argument was supplied - --> tests/hook_macro/use_prepared_state-fail.rs:25:5 +error: expected closure + --> tests/hook_macro/use_prepared_state-fail.rs:31:65 | -25 | use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?; +31 | use_prepared_state_without_closure!(|_| -> u32 { todo!() }, 123)?; + | ^^^ + +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_prepared_state-fail.rs:33:5 + | +33 | use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the macro `use_prepared_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info) diff --git a/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.rs b/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.rs index 113b0404e..42d6e42a8 100644 --- a/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.rs +++ b/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.rs @@ -7,8 +7,12 @@ fn Comp() -> HtmlResult { use_transitive_state_with_closure!(|_| { todo!() }, 123)?; + use_transitive_state_with_closure!(123, |_| { todo!() })?; + use_transitive_state_with_closure!(|_| -> u32 { todo!() })?; + use_transitive_state_with_closure!(|_| -> u32 { todo!() }, 123)?; + Ok(Html::default()) } @@ -18,8 +22,12 @@ fn Comp2() -> HtmlResult { use_transitive_state_without_closure!(|_| { todo!() }, 123)?; + use_transitive_state_without_closure!(123, |_| { todo!() })?; + use_transitive_state_without_closure!(|_| -> u32 { todo!() })?; + use_transitive_state_without_closure!(|_| -> u32 { todo!() }, 123)?; + Ok(Html::default()) } diff --git a/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr b/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr index 947122613..b8293a392 100644 --- a/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr +++ b/packages/yew-macro/tests/hook_macro/use_transitive_state-fail.stderr @@ -1,39 +1,67 @@ -error: expected closure - --> tests/hook_macro/use_transitive_state-fail.rs:6:40 +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_transitive_state-fail.rs:6:5 | 6 | use_transitive_state_with_closure!(123)?; - | ^^^ + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `use_transitive_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) -error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle. - --> tests/hook_macro/use_transitive_state-fail.rs:8:40 +error: expected closure + --> tests/hook_macro/use_transitive_state-fail.rs:8:57 | 8 | use_transitive_state_with_closure!(|_| { todo!() }, 123)?; - | ^^^^^^^^^^^^^^^ + | ^^^ + +error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle. + --> tests/hook_macro/use_transitive_state-fail.rs:10:45 + | +10 | use_transitive_state_with_closure!(123, |_| { todo!() })?; + | ^^^^^^^^^^^^^^^ error: this hook takes 2 arguments but 1 argument was supplied - --> tests/hook_macro/use_transitive_state-fail.rs:10:5 + --> tests/hook_macro/use_transitive_state-fail.rs:12:5 | -10 | use_transitive_state_with_closure!(|_| -> u32 { todo!() })?; +12 | use_transitive_state_with_closure!(|_| -> u32 { todo!() })?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the macro `use_transitive_state_with_closure` (in Nightly builds, run with -Z macro-backtrace for more info) error: expected closure - --> tests/hook_macro/use_transitive_state-fail.rs:17:43 + --> tests/hook_macro/use_transitive_state-fail.rs:14:64 | -17 | use_transitive_state_without_closure!(123)?; - | ^^^ - -error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle. - --> tests/hook_macro/use_transitive_state-fail.rs:19:43 - | -19 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?; - | ^^^^^^^^^^^^^^^ +14 | use_transitive_state_with_closure!(|_| -> u32 { todo!() }, 123)?; + | ^^^ error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_transitive_state-fail.rs:21:5 | -21 | use_transitive_state_without_closure!(|_| -> u32 { todo!() })?; +21 | use_transitive_state_without_closure!(123)?; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: this error originates in the macro `use_transitive_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: expected closure + --> tests/hook_macro/use_transitive_state-fail.rs:23:60 + | +23 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?; + | ^^^ + +error: You must specify a return type for this closure. This is used when the closure is omitted from the client side rendering bundle. + --> tests/hook_macro/use_transitive_state-fail.rs:25:48 + | +25 | use_transitive_state_without_closure!(123, |_| { todo!() })?; + | ^^^^^^^^^^^^^^^ + +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_transitive_state-fail.rs:27:5 + | +27 | use_transitive_state_without_closure!(|_| -> u32 { todo!() })?; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: this error originates in the macro `use_transitive_state_without_closure` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: expected closure + --> tests/hook_macro/use_transitive_state-fail.rs:29:67 + | +29 | use_transitive_state_without_closure!(|_| -> u32 { todo!() }, 123)?; + | ^^^ 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 2ab10ac7e..3735398fa 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 @@ -13,8 +13,8 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] pub fn use_prepared_state( - f: F, deps: D, + f: F, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -41,7 +41,7 @@ where fn run(self, ctx: &mut HookContext) -> Self::Output { match ctx.creation_mode { - RenderMode::Ssr => feat_ssr::use_prepared_state(self.f, self.deps).run(ctx), + RenderMode::Ssr => feat_ssr::use_prepared_state(self.deps, self.f).run(ctx), _ => feat_hydration::use_prepared_state(self.deps).run(ctx), } } @@ -52,8 +52,8 @@ where #[doc(hidden)] pub fn use_prepared_state_with_suspension( - f: F, deps: D, + f: F, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -84,7 +84,7 @@ where fn run(self, ctx: &mut HookContext) -> Self::Output { match ctx.creation_mode { RenderMode::Ssr => { - feat_ssr::use_prepared_state_with_suspension(self.f, self.deps).run(ctx) + feat_ssr::use_prepared_state_with_suspension(self.deps, self.f).run(ctx) } _ => feat_hydration::use_prepared_state(self.deps).run(ctx), } 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 1720bbd1a..216debd42 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 @@ -14,8 +14,8 @@ use crate::suspense::{Suspension, SuspensionResult}; #[doc(hidden)] pub fn use_prepared_state( - f: F, deps: D, + f: F, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -69,8 +69,8 @@ where #[doc(hidden)] pub fn use_prepared_state_with_suspension( - f: F, deps: D, + f: F, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, 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 ad69308d8..c611be624 100644 --- a/packages/yew/src/functional/hooks/use_prepared_state/mod.rs +++ b/packages/yew/src/functional/hooks/use_prepared_state/mod.rs @@ -39,7 +39,7 @@ pub use feat_ssr::*; /// use yew::suspense::SuspensionResult; /// /// #[hook] -/// pub fn use_prepared_state(f: F, deps: D) -> SuspensionResult>> +/// pub fn use_prepared_state(deps: D, f: F) -> SuspensionResult>> /// where /// D: Serialize + DeserializeOwned + PartialEq + 'static, /// T: Serialize + DeserializeOwned + 'static, @@ -63,8 +63,8 @@ pub use feat_ssr::*; /// /// #[hook] /// pub fn use_prepared_state( -/// f: F, /// deps: D, +/// f: F, /// ) -> SuspensionResult>> /// where /// D: Serialize + DeserializeOwned + PartialEq + 'static, 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 56d8c0a30..a243ade92 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 @@ -12,8 +12,8 @@ use crate::suspense::SuspensionResult; #[doc(hidden)] pub fn use_transitive_state( - f: F, deps: D, + f: F, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, @@ -40,7 +40,7 @@ where fn run(self, ctx: &mut HookContext) -> Self::Output { match ctx.creation_mode { - RenderMode::Ssr => feat_ssr::use_transitive_state(self.f, self.deps).run(ctx), + RenderMode::Ssr => feat_ssr::use_transitive_state(self.deps, self.f).run(ctx), _ => feat_hydration::use_transitive_state(self.deps).run(ctx), } } 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 fc0304a20..1135ccb62 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 @@ -39,8 +39,8 @@ where #[doc(hidden)] pub fn use_transitive_state( - f: F, deps: D, + f: F, ) -> impl Hook>>> where D: Serialize + DeserializeOwned + PartialEq + 'static, 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 893a175bd..5709367b7 100644 --- a/packages/yew/src/functional/hooks/use_transitive_state/mod.rs +++ b/packages/yew/src/functional/hooks/use_transitive_state/mod.rs @@ -27,7 +27,7 @@ pub use feat_ssr::*; /// During hydration, it will only return `Ok(Some(Rc))` if the component is hydrated from a /// server-side rendering artifact and its dependency value matches. /// -/// `let state = use_transitive_state!(|deps| -> ReturnType { ... }, deps);` +/// `let state = use_transitive_state!(deps, |deps| -> ReturnType { ... });` /// /// It has the following function signature: /// @@ -39,7 +39,7 @@ pub use feat_ssr::*; /// use yew::suspense::SuspensionResult; /// /// #[hook] -/// pub fn use_transitive_state(f: F, deps: D) -> SuspensionResult>> +/// pub fn use_transitive_state(deps: D, f: F) -> SuspensionResult>> /// where /// D: Serialize + DeserializeOwned + PartialEq + 'static, /// T: Serialize + DeserializeOwned + 'static, diff --git a/packages/yew/tests/use_prepared_state.rs b/packages/yew/tests/use_prepared_state.rs index 8081c3014..28644d026 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 })?.unwrap_or_default(); Ok(html! {
@@ -68,7 +68,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 }, ())?.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 5cb663adb..71f7f31dd 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 })?.unwrap_or_default(); Ok(html! {
diff --git a/tools/benchmark-ssr/src/main.rs b/tools/benchmark-ssr/src/main.rs index 011194e22..f2a0dcaad 100644 --- a/tools/benchmark-ssr/src/main.rs +++ b/tools/benchmark-ssr/src/main.rs @@ -140,12 +140,9 @@ async fn bench_concurrent_task() -> Duration { #[function_component] fn Comp() -> HtmlResult { - let _state = use_prepared_state!( - async move |_| -> () { - sleep(Duration::from_secs(1)).await; - }, - () - )?; + let _state = use_prepared_state!((), async move |_| -> () { + sleep(Duration::from_secs(1)).await; + })?; Ok(Html::default()) } diff --git a/website/docs/migration-guides/yew/from-0_20_0-to-next.mdx b/website/docs/migration-guides/yew/from-0_20_0-to-next.mdx index c7f8c2756..9fbc98ce0 100644 --- a/website/docs/migration-guides/yew/from-0_20_0-to-next.mdx +++ b/website/docs/migration-guides/yew/from-0_20_0-to-next.mdx @@ -27,6 +27,12 @@ sg --pattern 'use_memo($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_memo($DEPENDE sg --pattern 'use_future_with_deps($CALLBACK,$$$DEPENDENCIES)' --rewrite 'use_effect_with($$$DEPENDENCIES, $CALLBACK)' -l rs -i sg --pattern 'use_future_with($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_effect_with($DEPENDENCIES,$$$CALLBACK)' -l rs -i + +sg --pattern 'use_transitive_state!($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_transitive_state!($DEPENDENCIES,$$$CALLBACK)' -l rs -i +sg --pattern 'use_transitive_state!($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_transitive_state!($DEPENDENCIES,$$$CALLBACK)' -l rs -i + +sg --pattern 'use_prepared_state!($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_prepared_state!($DEPENDENCIES,$$$CALLBACK)' -l rs -i +sg --pattern 'use_prepared_state!($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_prepared_state!($DEPENDENCIES,$$$CALLBACK)' -l rs -i ``` ### Reasoning