Skip to content

Draft#417

Draft
hfhbd wants to merge 2 commits intomainfrom
draft
Draft

Draft#417
hfhbd wants to merge 2 commits intomainfrom
draft

Conversation

@hfhbd
Copy link
Copy Markdown
Owner

@hfhbd hfhbd commented Oct 27, 2024

@rjaros what do you think of this draft:

  • adding hashCode to router instances (for a stable cache)
  • publishing HashRouter/BrowserRouter to use without @composable, I guess you only support BrowserRouter at all?

@rjaros
Copy link
Copy Markdown
Contributor

rjaros commented Oct 27, 2024

Yes, I work with BrowserRouter only. Making it a singleton will be enough to drop a need for any other global variable.

But I'm not sure how does this change relate to cache?

@hfhbd
Copy link
Copy Markdown
Owner Author

hfhbd commented Oct 27, 2024

Your cache uses hashcode but there is no custom hashcode implementation for any class yet, so it will still create and add a new instance over and over because it currently uses the object identity.
Or what do I miss?

@rjaros
Copy link
Copy Markdown
Contributor

rjaros commented Oct 27, 2024

The default hashCode always returns the same value for the same instance.

@rjaros
Copy link
Copy Markdown
Contributor

rjaros commented Oct 27, 2024

In typical scenario there is only one Router instance in an application, but I had to add this hashCode to my cache key because there are multiple Router instances created in tests.

@hfhbd
Copy link
Copy Markdown
Owner Author

hfhbd commented Oct 27, 2024

The default hashCode always returns the same value for the same instance.

That's true, but in line 91,

val newState = routeBuilderCache.getOrPut("${currentRouter.hashCode()} $basePath $newPath") {
, the current router is not the global one, but any Router, including DelegatedRouter with identity hashcode.

@rjaros
Copy link
Copy Markdown
Contributor

rjaros commented Oct 27, 2024

I think you are right. In my copy of your lib I currently don't use this hashCode. I just use the paths as the key and it works perfectly fine:
https://github.com/rjaros/kilua/blob/main/modules/kilua-routing/src/commonMain/kotlin/app/softwork/routingcompose/RouteBuilder.kt#L113

I've added the hashCode to fix failing tests, when I decided to share my changes directly in a PR, and it may be wrong.

@rjaros
Copy link
Copy Markdown
Contributor

rjaros commented Mar 5, 2025

Just to let you know, I've implemented the changes you proposed in this draft in my code and everything seems to work well (of course I still have my global routeBuilderCache to avoid memory leaks). The only problem is operator fun Router.invoke, which is not suggested by IntelliJ auto-complete so it has to be manually imported. I would consider keeping some top level functions to allow simple calls like HashRouter("/") without this import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants