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

Clean-up and optimize router a little bit #1869

Merged
merged 11 commits into from
May 31, 2021
Merged

Conversation

ranile
Copy link
Member

@ranile ranile commented May 24, 2021

Description

This PR fixes the hack I introduced in #1859 and also adds a couple of optimizations optimizations.

Checklist

  • I have run cargo make pr-flow
  • I have reviewed my own code
  • I have added tests

…te is obtained

This has the drawback of requiring the `Routable` enum to be `Clone`. Generally, this shouldn't be an issue as cloning the value *should* be cheaper than making a DOM API call and then running the route recognition process
@ranile ranile mentioned this pull request May 24, 2021
3 tasks
@ranile
Copy link
Member Author

ranile commented May 25, 2021

@Diggsey, can you take a look? I implemented some of the optimizations from your PR in the current router.

Also, sorry I didn't mention you earlier. I was going to but forgot

@Diggsey
Copy link
Contributor

Diggsey commented May 25, 2021

Seems reasonable - it's maybe surprising that recognise has side-effects on current-route? And it would be nice if there was a way to reset the route when the router has been destroyed, so that it doesn't continue returning an out-of-date value.

@ranile
Copy link
Member Author

ranile commented May 26, 2021

it's maybe surprising that recognise has side-effects on current-route?

Those methods aren't really supposed to be used by the user. We provide abstractions over those which should be used instead. I feel like it is a documentation issue.

it would be nice if there was a way to reset the route when the router has been destroyed, so that it doesn't continue returning an out-of-date value.

That's a good point. I will add a destroy method which is called from component's destroy and will clean up the state.

@siku2
Copy link
Member

siku2 commented May 31, 2021

Should probably update the documentation to state that Routable needs to implement Clone now.
You might even want to add it as a bound to the Routable trait itself.

packages/yew-router-macro/src/routable_derive.rs Outdated Show resolved Hide resolved
///
/// # Usage
///
/// The functions exposed by this trait are **not** supposed to be consumed directly. Instead use
Copy link
Member

Choose a reason for hiding this comment

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

I personally don't think it's great API design to have a derivable trait that isn't supposed to be consumed. The Routable trait could function as a useful abstraction outside of the Router component if it were a bit more flexible.
This is just a side note though, there's no need to do anything about it in this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is generally the case that Routable won't be consumed directly by the user. The doc comment exists because macro introduces state and therefore care must be taken that a manual usage cleans up properly. See the above comment about side-effects.

packages/yew-router/src/utils.rs Outdated Show resolved Hide resolved
Comment on lines +14 to +19
BASE_URL_LOADED.call_once(|| {
BASE_URL.with(|val| {
*val.borrow_mut() = fetch_base_url();
})
});
BASE_URL.with(|it| it.borrow().as_ref().map(|it| it.to_string()))
Copy link
Member

Choose a reason for hiding this comment

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

Why not use an option instead of using both an additional once?

Copy link
Member Author

Choose a reason for hiding this comment

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

fetch_base_url needs to called only once from this function at the very start to populate state. Option can't tell the difference between uninitialized and no route found. In fact, Option is used currently for the latter case.

packages/yew-router-macro/src/routable_derive.rs Outdated Show resolved Hide resolved
Comment on lines -81 to +83
let route_listener = attach_route_listener(link.callback(Msg::UpdateRoute));
let route_listener = EventListener::new(&yew::utils::window(), "popstate", move |_| {
link.send_message(Msg::ReRender)
});
Copy link
Member

Choose a reason for hiding this comment

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

Why did you move away from the modular API again?

Copy link
Member Author

Choose a reason for hiding this comment

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

Optimization. The API changed, meaning that in the previous implementation, it'd now read the current route twice. This change ensures the current route is checked and obtained once by the component.

Copy link
Member

Choose a reason for hiding this comment

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

That's very unfortunate, are you sure there's no way to avoid that?

Copy link
Member Author

@ranile ranile May 31, 2021

Choose a reason for hiding this comment

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

There will be one when the listener is attached and called and one for the initial render. I'm not aware of any ways that provide a way to handle this. Iirc, the tests failed with the previous implementation (don't exactly remember though)

Copy link
Member

Choose a reason for hiding this comment

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

Alright then.

@github-actions
Copy link

github-actions bot commented May 31, 2021

Visit the preview URL for this PR (updated for commit f86da84):

https://yew-rs--pr1869-router-cleanup-jdmv9nlf.web.app

(expires Mon, 07 Jun 2021 17:40:42 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

@ranile
Copy link
Member Author

ranile commented May 31, 2021

@siku2 I made the changes. Take a look.

@siku2 siku2 merged commit 5fec49c into yewstack:master May 31, 2021
@voidpumpkin voidpumpkin added the A-yew-router Area: The yew-router crate label Nov 26, 2021
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants