Skip to content

Compose Runtime Integration Take 2 #823

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id("com.android.application")
`kotlin-android`
id("kotlin-parcelize")
id("app.cash.molecule")
}
android {
compileSdk = 32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.squareup.benchmarks.performance.complex.poetry

import androidx.compose.runtime.Composable
import com.squareup.benchmarks.performance.complex.poetry.instrumentation.ActionHandlingTracingInterceptor
import com.squareup.benchmarks.performance.complex.poetry.instrumentation.asTraceableWorker
import com.squareup.benchmarks.performance.complex.poetry.views.LoaderSpinner
Expand Down Expand Up @@ -48,5 +49,36 @@ class MaybeLoadingGatekeeperWorkflow<T : Any>(
)
}

@Composable
override fun Rendering(
renderProps: Unit,
renderState: IsLoading,
context: RenderContext,
hoistRendering: @Composable (rendering: MayBeLoadingScreen) -> Unit
) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm stumped here. These overrides are recognized by the compiler as it runs in the IDE but when tracing through the call always goes through to the base StatefulWorkflow::Rendering implementation?

@zach-klippenstein any idea why this override wouldn't be recognized at runtime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Reproduced in small project. I believe bug in Molecule - cashapp/molecule#91

context.runningWorker(isLoading.asTraceableWorker("GatekeeperLoading")) {
action {
state = it
}
}
context.ChildRendering(
childWithLoading, childProps, "",
hoistRendering = {
hoistRendering(
MayBeLoadingScreen(
baseScreen = it,
loaders = if (renderState) listOf(LoaderSpinner) else emptyList()
)
)
}
) {
action(ActionHandlingTracingInterceptor.keyForTrace("GatekeeperChildFinished")) {
setOutput(
Unit
)
}
}
}

override fun snapshotState(state: IsLoading): Snapshot? = null
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.squareup.benchmarks.performance.complex.poetry

import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.squareup.benchmarks.performance.complex.poetry.PerformancePoemWorkflow.Action.ClearSelection
import com.squareup.benchmarks.performance.complex.poetry.PerformancePoemWorkflow.Action.HandleStanzaListOutput
import com.squareup.benchmarks.performance.complex.poetry.PerformancePoemWorkflow.Action.SelectNext
Expand All @@ -16,6 +20,7 @@ import com.squareup.benchmarks.performance.complex.poetry.views.BlankScreen
import com.squareup.sample.container.overviewdetail.OverviewDetailScreen
import com.squareup.sample.poetry.PoemWorkflow
import com.squareup.sample.poetry.PoemWorkflow.ClosePoem
import com.squareup.sample.poetry.StanzaListScreen
import com.squareup.sample.poetry.StanzaListWorkflow
import com.squareup.sample.poetry.StanzaListWorkflow.NO_SELECTED_STANZA
import com.squareup.sample.poetry.StanzaScreen
Expand Down Expand Up @@ -57,6 +62,7 @@ import kotlinx.coroutines.flow.flow
* break ties/conflicts with a token in the start/stop requests. We leave that complexity out
* here. **
*/
@OptIn(WorkflowUiExperimentalApi::class)
class PerformancePoemWorkflow(
private val simulatedPerfConfig: SimulatedPerfConfig = SimulatedPerfConfig.NO_SIMULATED_PERF,
private val isLoading: MutableStateFlow<Boolean>,
Expand Down Expand Up @@ -234,6 +240,164 @@ class PerformancePoemWorkflow(
}
}

@Composable
override fun Rendering(
renderProps: Poem,
renderState: State,
context: RenderContext,
hoistRendering: @Composable (rendering: OverviewDetailScreen) -> Unit
) {
when (renderState) {
Initializing -> {
// Again, the entire `Initializing` state is a smell, which is most obvious from the
// use of `Worker.from { Unit }`. A Worker doing no work and only shuttling the state
// along is usually the sign you have an extraneous state that can be collapsed!
// Don't try this at home.
context.runningWorker(
Worker.from {
isLoading.value = true
},
"initializing"
) {
action {
isLoading.value = false
state = Selected(NO_SELECTED_STANZA)
}
}
hoistRendering(OverviewDetailScreen(overviewRendering = BackStackScreen(BlankScreen)))
}
else -> {
val (stanzaIndex, currentStateIsLoading, repeat) = when (renderState) {
is ComplexCall -> Triple(renderState.payload, true, renderState.repeater)
is Selected -> Triple(renderState.stanzaIndex, false, 0)
Initializing -> throw IllegalStateException("No longer initializing.")
}

if (currentStateIsLoading) {
if (repeat > 0) {
// Running a flow that emits 'repeat' number of times
context.runningWorker(
flow {
while (true) {
// As long as this Worker is running we want to be emitting values.
delay(2)
emit(repeat)
}
}.asTraceableWorker("EventRepetition")
) {
action {
(state as? ComplexCall)?.let { currentState ->
// Still repeating the complex call
state = ComplexCall(
payload = currentState.payload,
repeater = (currentState.repeater - 1).coerceAtLeast(0)
)
}
}
}
} else {
context.runningWorker(
worker = TraceableWorker.from("PoemLoading") {
isLoading.value = true
delay(simulatedPerfConfig.complexityDelay)
// No Output for Worker is necessary because the selected index
// is already in the state.
}
) {
action {
isLoading.value = false
(state as? ComplexCall)?.let { currentState ->
state = Selected(currentState.payload)
}
}
}
}
}

val previousStanzas: MutableState<List<StanzaScreen>> = remember {
mutableStateOf(emptyList())
}
val visibleStanza: MutableState<StanzaScreen?> = remember {
mutableStateOf(null)
}

if (stanzaIndex != NO_SELECTED_STANZA) {
renderProps.stanzas.subList(0, stanzaIndex)
.forEachIndexed { index, _ ->
context.ChildRendering(
StanzaWorkflow,
Props(
poem = renderProps,
index = index,
eventHandlerTag = ActionHandlingTracingInterceptor::keyForTrace
),
key = "$index",
hoistRendering = @Composable {
previousStanzas.value = previousStanzas.value + it
}
) {
noAction()
}
}
context.ChildRendering(
StanzaWorkflow,
Props(
poem = renderProps,
index = stanzaIndex,
eventHandlerTag = ActionHandlingTracingInterceptor::keyForTrace
),
key = "$stanzaIndex",
hoistRendering = @Composable {
visibleStanza.value = it
}
) {
when (it) {
CloseStanzas -> ClearSelection(simulatedPerfConfig)
ShowPreviousStanza -> SelectPrevious(simulatedPerfConfig)
ShowNextStanza -> SelectNext(simulatedPerfConfig)
}
}
}

val stackedStanzas = visibleStanza.value?.let {
(previousStanzas.value + it).toBackStackScreen<Screen>()
}

val stanzaListOverview: MutableState<StanzaListScreen?> = remember {
mutableStateOf(null)
}
context.ChildRendering(
StanzaListWorkflow,
StanzaListWorkflow.Props(
poem = renderProps,
eventHandlerTag = ActionHandlingTracingInterceptor::keyForTrace
),
key = "",
hoistRendering = @Composable {
stanzaListOverview.value = it.copy(selection = stanzaIndex)
}
) { selected ->
HandleStanzaListOutput(simulatedPerfConfig, selected)
}

hoistRendering(
stackedStanzas
?.let {
OverviewDetailScreen(
overviewRendering = BackStackScreen(stanzaListOverview.value!!),
detailRendering = it
)
} ?: OverviewDetailScreen(
overviewRendering = BackStackScreen(stanzaListOverview.value!!),
selectDefault = {
context.actionSink.send(HandleStanzaListOutput(simulatedPerfConfig, 0))
}
)
)
}
}
}

override fun snapshotState(state: State): Snapshot? = null

internal sealed class Action : WorkflowAction<Poem, State, ClosePoem>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.squareup.benchmarks.performance.complex.poetry

import androidx.compose.runtime.Composable
import com.squareup.benchmarks.performance.complex.poetry.PerformancePoemsBrowserWorkflow.State
import com.squareup.benchmarks.performance.complex.poetry.PerformancePoemsBrowserWorkflow.State.ComplexCall
import com.squareup.benchmarks.performance.complex.poetry.PerformancePoemsBrowserWorkflow.State.Initializing
Expand Down Expand Up @@ -42,6 +43,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
* break ties/conflicts with a token in the start/stop requests. We leave that complexity out
* here. **
*/
@OptIn(WorkflowUiExperimentalApi::class)
class PerformancePoemsBrowserWorkflow(
private val simulatedPerfConfig: SimulatedPerfConfig,
private val poemWorkflow: PoemWorkflow,
Expand Down Expand Up @@ -70,7 +72,6 @@ class PerformancePoemsBrowserWorkflow(
return if (simulatedPerfConfig.useInitializingState) Initializing else NoSelection
}

@OptIn(WorkflowUiExperimentalApi::class)
override fun render(
renderProps: List<Poem>,
renderState: State,
Expand Down Expand Up @@ -154,6 +155,106 @@ class PerformancePoemsBrowserWorkflow(
}
}

@Composable
override fun Rendering(
renderProps: List<Poem>,
renderState: State,
context: RenderContext,
hoistRendering: @Composable (rendering: OverviewDetailScreen) -> Unit
) {
val poemListProps = Props(
poems = renderProps,
eventHandlerTag = ActionHandlingTracingInterceptor::keyForTrace
)
context.ChildRendering(
child = PoemListWorkflow,
props = poemListProps,
key = "",
hoistRendering = { poemListRendering ->
when (renderState) {
// Again, then entire `Initializing` state is a smell, which is most obvious from the
// use of `Worker.from { Unit }`. A Worker doing no work and only shuttling the state
// along is usually the sign you have an extraneous state that can be collapsed!
// Don't try this at home.
is Initializing -> {
context.runningWorker(TraceableWorker.from("BrowserInitializing") { Unit }, "init") {
isLoading.value = true
action {
isLoading.value = false
state = NoSelection
}
}
hoistRendering(OverviewDetailScreen(overviewRendering = BackStackScreen(BlankScreen)))
}
is NoSelection -> {
hoistRendering(
OverviewDetailScreen(
overviewRendering = BackStackScreen(
poemListRendering.copy(selection = NO_POEM_SELECTED)
)
)
)
}
is ComplexCall -> {
context.runningWorker(
TraceableWorker.from("ComplexCallBrowser(${renderState.payload})") {
isLoading.value = true
delay(simulatedPerfConfig.complexityDelay)
// No Output for Worker is necessary because the selected index
// is already in the state.
}
) {
action {
isLoading.value = false
(state as? ComplexCall)?.let { currentState ->
state = if (currentState.payload != NO_POEM_SELECTED) {
Selected(currentState.payload)
} else {
NoSelection
}
}
}
}
val poemOverview = OverviewDetailScreen(
overviewRendering = BackStackScreen(
poemListRendering.copy(selection = renderState.payload)
)
)
if (renderState.payload != NO_POEM_SELECTED) {
context.ChildRendering(
poemWorkflow,
renderProps[renderState.payload],
key = "",
hoistRendering = { poem: OverviewDetailScreen ->
hoistRendering(poemOverview + poem)
}
) { clearSelection }
} else {
hoistRendering(poemOverview)
}
}
is Selected -> {
val poemOverview = OverviewDetailScreen(
overviewRendering = BackStackScreen(
poemListRendering.copy(selection = renderState.poemIndex)
)
)
context.ChildRendering(
poemWorkflow,
renderProps[renderState.poemIndex],
key = "",
hoistRendering = { poem: OverviewDetailScreen ->
hoistRendering(poemOverview + poem)
}
) { clearSelection }
}
}
}
) { selected ->
choosePoem(selected)
}
}

override fun snapshotState(state: State): Snapshot? = null

private fun choosePoem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,12 @@ class PerformancePoetryActivity : AppCompatActivity() {
installedInterceptor = ActionHandlingTracingInterceptor()
}

val isFrameTimeout = intent.getBooleanExtra(EXTRA_RUNTIME_FRAME_TIMEOUT, false)
val runtimeConfig = if (isFrameTimeout) FrameTimeout() else RenderPerAction
val isFrameTimeout = true; //intent.getBooleanExtra(EXTRA_RUNTIME_FRAME_TIMEOUT, false)
val runtimeConfig = if (isFrameTimeout) {
FrameTimeout(useComposeInRuntime = true)
} else {
RenderPerAction
}

val component =
PerformancePoetryComponent(installedInterceptor, simulatedPerfConfig, runtimeConfig)
Expand Down
Loading