Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update signature of use_prepared_state/use_transitive_state #3376

Merged
merged 9 commits into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/simple_ssr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
<div>{"Random UUID: "}{uuid}</div>
Expand Down
24 changes: 12 additions & 12 deletions packages/yew-macro/src/use_prepared_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ pub struct PreparedState {

impl Parse for PreparedState {
fn parse(input: ParseStream) -> syn::Result<Self> {
// 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::<Token![,]>().map_err(|e| {
syn::Error::new(
Expand All @@ -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(
Expand All @@ -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();

Expand Down Expand Up @@ -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)
},
}
}
Expand Down
22 changes: 11 additions & 11 deletions packages/yew-macro/src/use_transitive_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ pub struct TransitiveState {

impl Parse for TransitiveState {
fn parse(input: ParseStream) -> syn::Result<Self> {
// 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::<Token![,]>().map_err(|e| {
syn::Error::new(
Expand All @@ -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(
Expand All @@ -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();

Expand All @@ -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)
}
}

Expand Down
28 changes: 16 additions & 12 deletions packages/yew-macro/tests/hook_macro/use_prepared_state-fail.stderr
Original file line number Diff line number Diff line change
@@ -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
ranile marked this conversation as resolved.
Show resolved Hide resolved
--> tests/hook_macro/use_prepared_state-fail.rs:10:5
Expand All @@ -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)?;
| ^^^^^^^^^^^^^^^
| ^^^

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to above, this testing is asserting that the closure has specified a return type.

error: this hook takes 2 arguments but 1 argument was supplied
--> tests/hook_macro/use_prepared_state-fail.rs:23:5
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::suspense::SuspensionResult;

#[doc(hidden)]
pub fn use_prepared_state<T, D, F>(
f: F,
deps: D,
f: F,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -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),
}
}
Expand All @@ -52,8 +52,8 @@ where

#[doc(hidden)]
pub fn use_prepared_state_with_suspension<T, D, F, U>(
f: F,
deps: D,
f: F,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::suspense::{Suspension, SuspensionResult};

#[doc(hidden)]
pub fn use_prepared_state<T, D, F>(
f: F,
deps: D,
f: F,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/functional/hooks/use_prepared_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub use feat_ssr::*;
/// use yew::suspense::SuspensionResult;
///
/// #[hook]
/// pub fn use_prepared_state<T, D, F>(f: F, deps: D) -> SuspensionResult<Option<Rc<T>>>
/// pub fn use_prepared_state<T, D, F>(deps: D, f: F) -> SuspensionResult<Option<Rc<T>>>
/// where
/// D: Serialize + DeserializeOwned + PartialEq + 'static,
/// T: Serialize + DeserializeOwned + 'static,
Expand All @@ -63,8 +63,8 @@ pub use feat_ssr::*;
///
/// #[hook]
/// pub fn use_prepared_state<T, D, F, U>(
/// f: F,
/// deps: D,
/// f: F,
/// ) -> SuspensionResult<Option<Rc<T>>>
/// where
/// D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use crate::suspense::SuspensionResult;

#[doc(hidden)]
pub fn use_transitive_state<T, D, F>(
f: F,
deps: D,
f: F,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand All @@ -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),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ where

#[doc(hidden)]
pub fn use_transitive_state<T, D, F>(
f: F,
deps: D,
f: F,
) -> impl Hook<Output = SuspensionResult<Option<Rc<T>>>>
where
D: Serialize + DeserializeOwned + PartialEq + 'static,
Expand Down
4 changes: 2 additions & 2 deletions packages/yew/src/functional/hooks/use_transitive_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use feat_ssr::*;
/// During hydration, it will only return `Ok(Some(Rc<T>))` 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:
///
Expand All @@ -39,7 +39,7 @@ pub use feat_ssr::*;
/// use yew::suspense::SuspensionResult;
///
/// #[hook]
/// pub fn use_transitive_state<T, D, F>(f: F, deps: D) -> SuspensionResult<Option<Rc<T>>>
/// pub fn use_transitive_state<T, D, F>(deps: D, f: F) -> SuspensionResult<Option<Rc<T>>>
/// where
/// D: Serialize + DeserializeOwned + PartialEq + 'static,
/// T: Serialize + DeserializeOwned + 'static,
Expand Down
8 changes: 4 additions & 4 deletions packages/yew/tests/use_prepared_state.rs
Original file line number Diff line number Diff line change
@@ -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))]
ranile marked this conversation as resolved.
Show resolved Hide resolved

use std::time::Duration;
Expand All @@ -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! {
<div>
Expand Down Expand Up @@ -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! {
<div>
Expand Down
2 changes: 1 addition & 1 deletion packages/yew/tests/use_transitive_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
<div>
Expand Down
6 changes: 6 additions & 0 deletions website/docs/migration-guides/yew/from-0_20_0-to-next.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading