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

Interoperability and wasmJs bugfixes #412

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

rjaros
Copy link
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
@@ -41,20 +59,34 @@ public fun Router.route(
CompositionLocalProvider(RouterCompositionLocal provides this) {
val rawPath by getPath(initRoute)
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
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
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
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.

@@ -24,7 +24,7 @@ public fun BrowserRouter(
initPath: String,
routeBuilder: @Composable RouteBuilder.() -> Unit
) {
BrowserRouter().route(initPath, routeBuilder)
Router.internalGlobalRouter = BrowserRouter().apply { route(initPath, routeBuilder) }
Copy link
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
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
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
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
Owner

hfhbd commented Oct 27, 2024

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

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