Skip to content

Interoperability and wasmJs bugfixes#412

Open
rjaros wants to merge 6 commits intohfhbd:mainfrom
rjaros:main
Open

Interoperability and wasmJs bugfixes#412
rjaros wants to merge 6 commits intohfhbd:mainfrom
rjaros:main

Conversation

@rjaros
Copy link
Copy Markdown
Contributor

@rjaros rjaros commented Oct 26, 2024

This PR fixes wasmJs navigation as well as some memory leaks. It also makes the library more open and easier to extend (I'm using it in my Kilua framework).

@hfhbd hfhbd self-assigned this Oct 26, 2024
val path = Path.from(rawPath)
val node = remember(path) { RouteBuilder(path.path, path) }
val node = remember(path) {
RouteBuilder.routeBuilderCache.getOrPut("${this.hashCode()} ${path.path} $path") {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Why can't we put all parameters to remember? Remember uses its own cache.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Remember only works with a recomposition. The routing changes the composition (route calls are leaving and entering the composition when navigating) and remember doesn't work here.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Do you have any samples causing the memory leaks (or a reproducer)? I do understand the cause, we create a new RouteBuilder during each composition, but ideally the GC/Compose should be able to remove the object.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't have a simple reproducer because this effect is only visible after thousands of recompositions and I've measured this only when running my app as a SSR service. The problem is not about RouteBuilder itself but about Compose MutableState it contains. I think there are subscriptions created for each instance by the compose compiler and I think they are not disposed correctly.

routeBuilder: @Composable RouteBuilder.() -> Unit
) {
BrowserRouter().route(initPath, routeBuilder)
Router.internalGlobalRouter = BrowserRouter().apply { route(initPath, routeBuilder) }
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What's the use-case/code for setting this variable? Can't the function just return the Router instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It could. I've implemented it this way just to simplify the client code and to be consistent with Router.current. In my applications I just use Router.global when Router.current can't be called.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What do you mean with can't be called? Because Router.current is @Composable or because there is no value set?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It can't be called from any non-composable functions like effects, event handlers, view model methods or some global utility functions.

@hfhbd
Copy link
Copy Markdown
Owner

hfhbd commented Oct 27, 2024

BTW I will cherry pick your wasm fixes in another PR.

@exaucae
Copy link
Copy Markdown

exaucae commented Jan 30, 2025

Hey @rjaros @hfhbd, is there any chance we get a minor release incorporating the cherry picked fixes ( namely 973728b ). pushState yields a stack overflow in version 0.4.0 as shown in #421

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.

3 participants