From 84b7c4cace121ad436f29dfaf9c3ab76a39a0919 Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 14 Aug 2023 17:03:14 +0500 Subject: [PATCH 1/8] Update signature of use_prepared_state --- packages/yew-macro/src/use_prepared_state.rs | 23 +++++++++++-------- .../use_prepared_state/feat_hydration_ssr.rs | 6 ++--- .../hooks/use_prepared_state/feat_ssr.rs | 2 +- .../hooks/use_prepared_state/mod.rs | 4 ++-- packages/yew/tests/use_prepared_state.rs | 8 +++---- 5 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/yew-macro/src/use_prepared_state.rs b/packages/yew-macro/src/use_prepared_state.rs index 737c2368c19..7aa5cefcd76 100644 --- a/packages/yew-macro/src/use_prepared_state.rs +++ b/packages/yew-macro/src/use_prepared_state.rs @@ -12,6 +12,17 @@ pub struct PreparedState { impl Parse for PreparedState { fn parse(input: ParseStream) -> syn::Result { + // Reads the deps. + let deps = input.parse()?; + + input.parse::().map_err(|e| { + syn::Error::new( + e.span(), + "this hook takes 2 arguments but 1 argument was supplied", + ) + })?; + + // Reads a closure. let expr: Expr = input.parse()?; @@ -20,12 +31,6 @@ impl Parse for PreparedState { other => return Err(syn::Error::new_spanned(other, "expected closure")), }; - input.parse::().map_err(|e| { - syn::Error::new( - e.span(), - "this hook takes 2 arguments but 1 argument was supplied", - ) - })?; let return_type = match &closure.output { ReturnType::Default => { @@ -38,8 +43,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 +110,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/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..5f27e2052d4 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, 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 1720bbd1a44..a51b67b8c6c 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, 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 ad69308d890..c611be62449 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/tests/use_prepared_state.rs b/packages/yew/tests/use_prepared_state.rs index 8081c3014a4..7545c675bb5 100644 --- a/packages/yew/tests/use_prepared_state.rs +++ b/packages/yew/tests/use_prepared_state.rs @@ -1,5 +1,5 @@ -#![cfg(target_arch = "wasm32")] -#![cfg(feature = "hydration")] +// #![cfg(target_arch = "wasm32")] +// #![cfg(feature = "hydration")] #![cfg_attr(nightly_yew, feature(async_closure))] use std::time::Duration; @@ -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! {
From d8e5e997ab1aa2e039ae39b25edbc54ab54f38c8 Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 14 Aug 2023 17:10:23 +0500 Subject: [PATCH 2/8] Update signature of use_transitive_state --- .../yew-macro/src/use_transitive_state.rs | 20 ++++++++++--------- .../feat_hydration_ssr.rs | 4 ++-- .../hooks/use_transitive_state/feat_ssr.rs | 2 +- .../hooks/use_transitive_state/mod.rs | 4 ++-- packages/yew/tests/use_transitive_state.rs | 2 +- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/yew-macro/src/use_transitive_state.rs b/packages/yew-macro/src/use_transitive_state.rs index 08bf9fc8265..3daf3da992b 100644 --- a/packages/yew-macro/src/use_transitive_state.rs +++ b/packages/yew-macro/src/use_transitive_state.rs @@ -12,6 +12,16 @@ pub struct TransitiveState { impl Parse for TransitiveState { fn parse(input: ParseStream) -> syn::Result { + // Reads the deps. + let deps = input.parse()?; + + input.parse::().map_err(|e| { + syn::Error::new( + e.span(), + "this hook takes 2 arguments but 1 argument was supplied", + ) + })?; + // Reads a closure. let expr: Expr = input.parse()?; @@ -20,12 +30,6 @@ impl Parse for TransitiveState { other => return Err(syn::Error::new_spanned(other, "expected closure")), }; - input.parse::().map_err(|e| { - syn::Error::new( - e.span(), - "this hook takes 2 arguments but 1 argument was supplied", - ) - })?; let return_type = match &closure.output { ReturnType::Default => { @@ -38,8 +42,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 +66,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/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..a243ade9230 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 fc0304a2090..1135ccb62b1 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 893a175bd4f..5709367b70c 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_transitive_state.rs b/packages/yew/tests/use_transitive_state.rs index 5cb663adbde..71f7f31dda1 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! {
From 6acbf26c89695f502b605081d788b1c32bde3a03 Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 14 Aug 2023 17:10:33 +0500 Subject: [PATCH 3/8] Migration guide + example --- examples/simple_ssr/src/lib.rs | 2 +- website/docs/migration-guides/yew/from-0_20_0-to-next.mdx | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/simple_ssr/src/lib.rs b/examples/simple_ssr/src/lib.rs index 1204af904ec..8611b7c0cf9 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/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 c288cfc9285..e1f78cce413 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 @@ -24,6 +24,12 @@ sg --pattern 'use_callback($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_callback( sg --pattern 'use_memo($CALLBACK,$$$DEPENDENCIES)' --rewrite 'use_memo($$$DEPENDENCIES, $CALLBACK)' -l rs -i sg --pattern 'use_memo($DEPENDENCIES,,$$$CALLBACK)' --rewrite 'use_memo($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 From 8103e4f62ae8294fb7d21a1ae1051c068b3654b1 Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 14 Aug 2023 17:13:50 +0500 Subject: [PATCH 4/8] bless trybuild tests --- .../hook_macro/use_prepared_state-fail.stderr | 28 +++++++++++-------- .../use_transitive_state-fail.stderr | 28 +++++++++++-------- 2 files changed, 32 insertions(+), 24 deletions(-) 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 62ba0033a56..9adf7f294a3 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 +error: expected closure + --> tests/hook_macro/use_prepared_state-fail.rs:8:55 | 8 | 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:10:5 @@ -26,17 +28,19 @@ 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: expected closure - --> tests/hook_macro/use_prepared_state-fail.rs:19:41 +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_prepared_state-fail.rs:19:5 | 19 | 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:21:41 +error: expected closure + --> tests/hook_macro/use_prepared_state-fail.rs:21:58 | 21 | 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:23:5 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 94712261373..8aa99b72603 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,14 +1,16 @@ -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: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_transitive_state-fail.rs:10:5 @@ -18,17 +20,19 @@ error: this hook takes 2 arguments but 1 argument was supplied | = 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 +error: this hook takes 2 arguments but 1 argument was supplied + --> tests/hook_macro/use_transitive_state-fail.rs:17:5 | 17 | 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: 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 +error: expected closure + --> tests/hook_macro/use_transitive_state-fail.rs:19:60 | 19 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?; - | ^^^^^^^^^^^^^^^ + | ^^^ error: this hook takes 2 arguments but 1 argument was supplied --> tests/hook_macro/use_transitive_state-fail.rs:21:5 From 1f5fa82c4b51581e4def5798e66fa65e56fe7cbd Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 14 Aug 2023 17:18:26 +0500 Subject: [PATCH 5/8] please fmt --- packages/yew-macro/src/use_prepared_state.rs | 3 --- packages/yew-macro/src/use_transitive_state.rs | 2 -- 2 files changed, 5 deletions(-) diff --git a/packages/yew-macro/src/use_prepared_state.rs b/packages/yew-macro/src/use_prepared_state.rs index 7aa5cefcd76..ec1a87712eb 100644 --- a/packages/yew-macro/src/use_prepared_state.rs +++ b/packages/yew-macro/src/use_prepared_state.rs @@ -22,7 +22,6 @@ impl Parse for PreparedState { ) })?; - // Reads a closure. let expr: Expr = input.parse()?; @@ -31,7 +30,6 @@ impl Parse for PreparedState { other => return Err(syn::Error::new_spanned(other, "expected closure")), }; - let return_type = match &closure.output { ReturnType::Default => { return Err(syn::Error::new_spanned( @@ -43,7 +41,6 @@ impl Parse for PreparedState { ReturnType::Type(_rarrow, ty) => *ty.to_owned(), }; - if !input.is_empty() { let maybe_trailing_comma = input.lookahead1(); diff --git a/packages/yew-macro/src/use_transitive_state.rs b/packages/yew-macro/src/use_transitive_state.rs index 3daf3da992b..57af33f3a91 100644 --- a/packages/yew-macro/src/use_transitive_state.rs +++ b/packages/yew-macro/src/use_transitive_state.rs @@ -30,7 +30,6 @@ impl Parse for TransitiveState { other => return Err(syn::Error::new_spanned(other, "expected closure")), }; - let return_type = match &closure.output { ReturnType::Default => { return Err(syn::Error::new_spanned( @@ -42,7 +41,6 @@ impl Parse for TransitiveState { ReturnType::Type(_rarrow, ty) => *ty.to_owned(), }; - if !input.is_empty() { let maybe_trailing_comma = input.lookahead1(); From 0229ac95af0b7b75f75cb68fa7585568d64bbcd8 Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 14 Aug 2023 17:34:07 +0500 Subject: [PATCH 6/8] there's one usage here as well --- .../yew/src/functional/hooks/use_prepared_state/feat_ssr.rs | 2 +- tools/benchmark-ssr/src/main.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 a51b67b8c6c..216debd424b 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 @@ -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/tools/benchmark-ssr/src/main.rs b/tools/benchmark-ssr/src/main.rs index 011194e2287..77a60296bf4 100644 --- a/tools/benchmark-ssr/src/main.rs +++ b/tools/benchmark-ssr/src/main.rs @@ -141,10 +141,10 @@ async fn bench_concurrent_task() -> Duration { #[function_component] fn Comp() -> HtmlResult { let _state = use_prepared_state!( + (), async move |_| -> () { sleep(Duration::from_secs(1)).await; - }, - () + } )?; Ok(Html::default()) From 55683cea81d3215677edd119231bbe684a8f63b8 Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Mon, 14 Aug 2023 19:51:15 +0500 Subject: [PATCH 7/8] use_prepared_state_with_suspension needs updates --- .../hooks/use_prepared_state/feat_hydration_ssr.rs | 2 +- tools/benchmark-ssr/src/main.rs | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) 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 5f27e2052d4..3735398fa80 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 @@ -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/tools/benchmark-ssr/src/main.rs b/tools/benchmark-ssr/src/main.rs index 77a60296bf4..f2a0dcaad96 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()) } From 07804c9e010a736d74c70702e242344d5d969a8f Mon Sep 17 00:00:00 2001 From: Muhammad Hamza Date: Sat, 19 Aug 2023 00:56:00 +0500 Subject: [PATCH 8/8] updated tests --- .../hook_macro/use_prepared_state-fail.rs | 10 +++- .../hook_macro/use_prepared_state-fail.stderr | 52 ++++++++++++++----- .../hook_macro/use_transitive_state-fail.rs | 8 +++ .../use_transitive_state-fail.stderr | 40 +++++++++++--- packages/yew/tests/use_prepared_state.rs | 4 +- 5 files changed, 89 insertions(+), 25 deletions(-) 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 e12ee98fe64..b7181f04d23 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 9adf7f294a3..775071c00d3 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 @@ -6,11 +6,11 @@ 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: expected closure - --> tests/hook_macro/use_prepared_state-fail.rs:8:55 +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: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 @@ -20,40 +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: expected closure + --> tests/hook_macro/use_prepared_state-fail.rs:12:62 + | +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:12:5 + --> tests/hook_macro/use_prepared_state-fail.rs:14:5 | -12 | use_prepared_state_with_closure!(async |_| -> u32 { todo!() })?; +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:16:55 + | +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:19:5 + --> tests/hook_macro/use_prepared_state-fail.rs:23:5 | -19 | use_prepared_state_without_closure!(123)?; +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:21:58 + --> tests/hook_macro/use_prepared_state-fail.rs:27:58 | -21 | use_prepared_state_without_closure!(|_| { todo!() }, 123)?; +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:23:5 + --> tests/hook_macro/use_prepared_state-fail.rs:29:5 | -23 | use_prepared_state_without_closure!(|_| -> u32 { todo!() })?; +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: expected closure + --> tests/hook_macro/use_prepared_state-fail.rs:31:65 + | +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:25:5 + --> tests/hook_macro/use_prepared_state-fail.rs:33:5 | -25 | use_prepared_state_without_closure!(async |_| -> u32 { todo!() })?; +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 113b0404ed3..42d6e42a85f 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 8aa99b72603..b8293a39298 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 @@ -12,32 +12,56 @@ error: expected closure 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:14:64 + | +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:17:5 + --> tests/hook_macro/use_transitive_state-fail.rs:21:5 | -17 | use_transitive_state_without_closure!(123)?; +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:19:60 + --> tests/hook_macro/use_transitive_state-fail.rs:23:60 | -19 | use_transitive_state_without_closure!(|_| { todo!() }, 123)?; +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:21:5 + --> tests/hook_macro/use_transitive_state-fail.rs:27:5 | -21 | use_transitive_state_without_closure!(|_| -> u32 { todo!() })?; +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/tests/use_prepared_state.rs b/packages/yew/tests/use_prepared_state.rs index 7545c675bb5..28644d026be 100644 --- a/packages/yew/tests/use_prepared_state.rs +++ b/packages/yew/tests/use_prepared_state.rs @@ -1,5 +1,5 @@ -// #![cfg(target_arch = "wasm32")] -// #![cfg(feature = "hydration")] +#![cfg(target_arch = "wasm32")] +#![cfg(feature = "hydration")] #![cfg_attr(nightly_yew, feature(async_closure))] use std::time::Duration;