-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b4dc27d
commit e5e1372
Showing
14 changed files
with
1,077 additions
and
51 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
call-logging/common/src/dev/programadorthi/routing/callloging/MDC.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
3 changes: 1 addition & 2 deletions
3
call-logging/js/src/dev/programadorthi/routing/callloging/MDC.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
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
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
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
76 changes: 76 additions & 0 deletions
76
.../javascript/js/src/dev/programadorthi/routing/javascript/JavascriptRoutingStateManager.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,76 @@ | ||
package dev.programadorthi.routing.javascript | ||
|
||
import dev.programadorthi.routing.core.RouteMethod | ||
import dev.programadorthi.routing.core.Routing | ||
import dev.programadorthi.routing.core.application | ||
import dev.programadorthi.routing.core.application.ApplicationCall | ||
import io.ktor.http.parametersOf | ||
import kotlinx.browser.window | ||
import kotlinx.coroutines.withTimeout | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
internal object JavascriptRoutingStateManager { | ||
fun init(routing: Routing) { | ||
// First time or page refresh we try continue from last state | ||
routing.tryNotifyTheRoute(state = window.history.state) | ||
|
||
resetOnPopStateEvent(routing) | ||
} | ||
|
||
suspend fun replaceAll( | ||
call: ApplicationCall, | ||
routing: Routing, | ||
) { | ||
while (true) { | ||
window.history.replaceState( | ||
title = "", | ||
url = null, | ||
data = null, | ||
) | ||
window.history.go(-1) | ||
val forceBreak = | ||
runCatching { | ||
withTimeout(1_000) { | ||
suspendCoroutine { continuation -> | ||
window.onpopstate = { event -> | ||
val state = event.state.deserialize() | ||
continuation.resume(state == null) | ||
} | ||
} | ||
} | ||
}.getOrDefault(true) | ||
if (forceBreak) { | ||
break | ||
} | ||
} | ||
|
||
window.history.replaceState( | ||
title = "routing", | ||
url = call.uri, | ||
data = call.serialize(), | ||
) | ||
|
||
resetOnPopStateEvent(routing) | ||
} | ||
|
||
private fun resetOnPopStateEvent(routing: Routing) { | ||
window.onpopstate = { event -> | ||
routing.tryNotifyTheRoute(state = event.state) | ||
} | ||
} | ||
|
||
private fun Routing.tryNotifyTheRoute(state: Any?) { | ||
val javascriptState = state.deserialize() ?: return | ||
val call = | ||
ApplicationCall( | ||
application = application, | ||
name = javascriptState.name, | ||
uri = javascriptState.uri, | ||
routeMethod = RouteMethod.parse(javascriptState.routeMethod), | ||
parameters = parametersOf(javascriptState.parameters), | ||
) | ||
call.neglect = true | ||
execute(call) | ||
} | ||
} |
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
Oops, something went wrong.