-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add browser hash/history navigation to wasmJs
- Loading branch information
1 parent
39a328b
commit 5c2084b
Showing
14 changed files
with
487 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
...Main/kotlin/com/copperleaf/ballast/navigation/browser/BaseBrowserNavigationInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package com.copperleaf.ballast.navigation.browser | ||
|
||
import com.copperleaf.ballast.Queued | ||
import com.copperleaf.ballast.awaitViewModelStart | ||
import com.copperleaf.ballast.events | ||
import com.copperleaf.ballast.navigation.internal.Uri | ||
import com.copperleaf.ballast.navigation.internal.UriBuilder | ||
import com.copperleaf.ballast.navigation.routing.Route | ||
import com.copperleaf.ballast.navigation.routing.RouterContract | ||
import com.copperleaf.ballast.navigation.routing.build | ||
import com.copperleaf.ballast.navigation.routing.directions | ||
import com.copperleaf.ballast.navigation.routing.mapCurrentDestination | ||
import com.copperleaf.ballast.navigation.vm.RouterInterceptor | ||
import com.copperleaf.ballast.navigation.vm.RouterInterceptorScope | ||
import com.copperleaf.ballast.navigation.vm.RouterNotification | ||
import kotlinx.coroutines.CompletableDeferred | ||
import kotlinx.coroutines.CoroutineStart | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.mapNotNull | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.launch | ||
|
||
@Suppress("UNUSED_PARAMETER") | ||
public abstract class BaseBrowserNavigationInterceptor<T : Route> internal constructor( | ||
private val initialRoute: T | ||
) : RouterInterceptor<T> { | ||
|
||
internal abstract fun getInitialUrl(): Uri? | ||
internal abstract fun watchForUrlChanges(): Flow<Uri> | ||
internal abstract fun setDestinationUrl(url: Uri) | ||
|
||
final override fun RouterInterceptorScope<T>.start( | ||
notifications: Flow<RouterNotification<T>> | ||
) { | ||
launch(start = CoroutineStart.UNDISPATCHED) { | ||
// start by setting the initial route from the browser hash, if provided when the webpage first loads | ||
onViewModelInitSetStateFromBrowser(notifications) | ||
|
||
// then sync the hash state to the router state (in both directions) | ||
joinAll( | ||
updateBrowserOnStateChange(notifications), | ||
updateStateOnBrowserChange(notifications) | ||
) | ||
} | ||
} | ||
|
||
private suspend fun RouterInterceptorScope<T>.onViewModelInitSetStateFromBrowser( | ||
notifications: Flow<RouterNotification<T>> | ||
) { | ||
// wait for the BallastNotification.ViewModelStarted to be sent | ||
notifications.awaitViewModelStart() | ||
|
||
val initialDestinationUrl = getInitialUrl()?.encodedPathAndQuery | ||
?: initialRoute.directions().build() | ||
|
||
val deferred = CompletableDeferred<Unit>() | ||
|
||
sendToQueue( | ||
Queued.HandleInput( | ||
deferred, | ||
RouterContract.Inputs.GoToDestination( | ||
destination = initialDestinationUrl | ||
) | ||
) | ||
) | ||
|
||
// wait for the initial URL to be set in the state, before allowing the rest of the address bar syncing to begin | ||
deferred.await() | ||
} | ||
|
||
private fun RouterInterceptorScope<T>.updateBrowserOnStateChange( | ||
notifications: Flow<RouterNotification<T>> | ||
): Job = launch(start = CoroutineStart.UNDISPATCHED) { | ||
notifications | ||
.events { it } | ||
.filterIsInstance<RouterContract.Events.BackstackChanged<T>>() | ||
.mapNotNull { ev -> | ||
ev.backstack.mapCurrentDestination( | ||
route = { | ||
if (annotations.any { it is WebEventRouteAnnotation }) { | ||
// ignore this request | ||
null | ||
} else { | ||
UriBuilder.parse(originalDestinationUrl) | ||
} | ||
}, | ||
notFound = { UriBuilder.parse(it) }, | ||
) | ||
} | ||
.distinctUntilChanged() | ||
.onEach { destination -> setDestinationUrl(destination) } | ||
.launchIn(this) | ||
} | ||
|
||
private fun RouterInterceptorScope<T>.updateStateOnBrowserChange( | ||
notifications: Flow<RouterNotification<T>> | ||
): Job = launch(start = CoroutineStart.UNDISPATCHED) { | ||
watchForUrlChanges() | ||
.onEach { destination -> | ||
sendToQueue( | ||
Queued.HandleInput( | ||
null, | ||
RouterContract.Inputs.GoToDestination( | ||
destination = destination.encodedPathAndQuery, | ||
extraAnnotations = setOf(WebEventRouteAnnotation), | ||
) | ||
) | ||
) | ||
} | ||
.launchIn(this) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...Main/kotlin/com/copperleaf/ballast/navigation/browser/BrowserHashNavigationInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.copperleaf.ballast.navigation.browser | ||
|
||
import com.copperleaf.ballast.navigation.internal.Uri | ||
import com.copperleaf.ballast.navigation.internal.UriBuilder | ||
import com.copperleaf.ballast.navigation.routing.Route | ||
import kotlinx.browser.window | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
import org.w3c.dom.HashChangeEvent | ||
|
||
public class BrowserHashNavigationInterceptor<T : Route>( | ||
initialRoute: T, | ||
) : BaseBrowserNavigationInterceptor<T>(initialRoute) { | ||
|
||
override fun getInitialUrl(): Uri? { | ||
val hashValue = window.location.hash.trim().trimStart('#').trimStart('/') | ||
val hashPieces = hashValue.split('?') | ||
|
||
val (initialPath: String?, initialQueryString: String?) = if (hashPieces.size == 2) { | ||
// we have path and query in the hash value | ||
val path = hashPieces[0].takeIf { it.isNotBlank() } | ||
val query = hashPieces[1].takeIf { it.isNotBlank() } | ||
path to query | ||
} else { | ||
// only have the path in the hash value | ||
val path = hashPieces[0].takeIf { it.isNotBlank() } | ||
val query = window.location.search.trimStart('?').takeIf { it.isNotBlank() } | ||
path to query | ||
} | ||
|
||
return if (!initialPath.isNullOrBlank() || !initialQueryString.isNullOrBlank()) { | ||
UriBuilder.build( | ||
encodedPath = "/$initialPath".also { println("initialPath: $it") }, | ||
encodedQueryString = initialQueryString, | ||
) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
override fun watchForUrlChanges(): Flow<Uri> { | ||
return callbackFlow<Uri> { | ||
window.onhashchange = { event: HashChangeEvent -> | ||
val partAfterHash = event.newURL?.split("#")?.last() | ||
if (!partAfterHash.isNullOrBlank()) { | ||
this@callbackFlow.trySend(UriBuilder.parse(partAfterHash)) | ||
} | ||
Unit | ||
} | ||
|
||
awaitClose { | ||
window.onhashchange = null | ||
} | ||
} | ||
} | ||
|
||
override fun setDestinationUrl(url: Uri) { | ||
window.location.hash = url.encodedPathAndQuery | ||
} | ||
} |
Oops, something went wrong.