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

Add field-level attribute to set/get state. #1109

Closed
hgzimmerman opened this issue Oct 22, 2019 · 4 comments · May be fixed by yewstack/yew_router#218
Closed

Add field-level attribute to set/get state. #1109

hgzimmerman opened this issue Oct 22, 2019 · 4 comments · May be fixed by yewstack/yew_router#218
Labels
A-yew-router Area: The yew-router crate feature-request A feature request

Comments

@hgzimmerman
Copy link
Member

Description

While the Switch trait allows for it, the derive Switch macro support for getting/setting the state is nonexistent, relying on manual implementations to actually allow setting the state.

This is fine for the moment, where just supporting the () state type is acceptable, but in the future, the macro needs to support setting and getting the state.

This might look like:

#[derive(Switch)]
enum AppRoute {
    #[to="/home/{name}"]
    Variant{
        name: String,
        #[state]
        state: JsValue
    }
}
@hgzimmerman
Copy link
Member Author

This is an important feature, that I do want to support eventually, but because its use case isn't needed except for in larger, professional applications, I think my time is better spent elsewhere in the meantime.

If the lack of this feature bothers you or prevents you from using this library, or Yew itself, please leave a comment here so I can gauge interest.

@hgzimmerman
Copy link
Member Author

Enough changes to how the state system works recently now makes this feature plausible to implement.

enum State {
    Id(i32)
    FormData(String, String),
    SelectedResource(Url)
}

#[derive(Switch, Clone)]
enum AppRoute {
    #[to="/home/{name}"]
    Variant{
        name: String,
        #[state]
        state: Option<State> // By default, holds nothing.
    }
}

@hgzimmerman
Copy link
Member Author

hgzimmerman commented Dec 21, 2019

Upon exploring this space, I have found some design deficiencies with respect to Routes with type parameters.

Because of the STATE type parameter on Route, there isn't a way for the macro to set/get the state field, because the __T has no relation to the annotated state: _ field in the variant/struct.

The way to overcome this problem is to simplify Route.

  • Remove the type parameter STATE.
  • Have the state field just be a String.
  • Allow setting/getting the state field for any type by ser/de ing it to/from JSON.
  • Remove <T> parameter from Switch, remove STATE from every other construct in the library.

This would have the add-on benefit of removing the Routes type parameter - which has "infected" nearly every other major construct in this library and is a point of developer error if you ever mis-match the type parameters.

@jstarry jstarry transferred this issue from yewstack/yew_router Apr 20, 2020
@jstarry jstarry added feature-request A feature request A-yew-router Area: The yew-router crate labels Apr 20, 2020
@ranile
Copy link
Member

ranile commented Mar 20, 2022

With the router rewrite, Switch trait has been removed. The new router provides different APIs, which allows ergonomic usage of router state. I'm closing this as this is no longer needed.

@ranile ranile closed this as completed Mar 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-yew-router Area: The yew-router crate feature-request A feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants