Skip to content

Commit

Permalink
Make TreehouseAppContent#preload idempotent (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
veyndan authored Nov 30, 2023
1 parent bb8f233 commit b0e57a4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private class State<A : AppService>(
private sealed interface ViewState {
object None : ViewState

class Preloading(
data class Preloading(
val onBackPressedDispatcher: OnBackPressedDispatcher,
val uiConfiguration: UiConfiguration,
) : ViewState
Expand Down Expand Up @@ -87,6 +87,7 @@ internal class TreehouseAppContent<A : AppService>(
dispatchers.checkUi()
val previousState = stateFlow.value

if (previousState.viewState == ViewState.Preloading(onBackPressedDispatcher, uiConfiguration)) return // Idempotent.
check(previousState.viewState is ViewState.None)

val nextViewState = ViewState.Preloading(onBackPressedDispatcher, uiConfiguration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,23 @@ class TreehouseAppContentTest {
content.unbind()
}

@Test
fun preload_idempotent() = runTest {
val content = treehouseAppContent()

codeHost.startCodeSession("codeSessionA")
eventLog.takeEvent("codeSessionA.start()")

content.preload(onBackPressedDispatcher, uiConfiguration)
eventLog.takeEvent("codeSessionA.app.uis[0].start()")

content.preload(onBackPressedDispatcher, uiConfiguration)
eventLog.assertNoEvents()

content.unbind()
eventLog.takeEvent("codeSessionA.app.uis[0].close()")
}

@Test
fun bind_idempotent() = runTest {
val content = treehouseAppContent()
Expand Down

0 comments on commit b0e57a4

Please sign in to comment.