@@ -2,17 +2,15 @@ package com.squareup.workflow1.internal.compose
2
2
3
3
import androidx.compose.runtime.Composable
4
4
import androidx.compose.runtime.DisposableEffect
5
- import androidx.compose.runtime.collection.mutableVectorOf
5
+ import androidx.compose.runtime.collection.MutableVector
6
6
import androidx.compose.runtime.currentRecomposeScope
7
7
import androidx.compose.runtime.getValue
8
8
import androidx.compose.runtime.key
9
- import androidx.compose.runtime.mutableStateOf
10
9
import androidx.compose.runtime.remember
11
10
import androidx.compose.runtime.rememberCoroutineScope
12
11
import androidx.compose.runtime.rememberUpdatedState
13
12
import androidx.compose.runtime.saveable.LocalSaveableStateRegistry
14
13
import androidx.compose.runtime.saveable.SaveableStateRegistry
15
- import androidx.compose.runtime.setValue
16
14
import com.squareup.workflow1.ActionApplied
17
15
import com.squareup.workflow1.ActionProcessingResult
18
16
import com.squareup.workflow1.NoopWorkflowInterceptor
@@ -77,12 +75,12 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
77
75
override val renderKey: String get() = id.name
78
76
override val sessionId: Long = idCounter.createId()
79
77
80
- private var lastProps by mutableStateOf(initialProps)
81
-
82
78
/* * This does not need to be a snapshot state object, it's only set again by [snapshot]. */
83
79
private var snapshotCache = snapshot?.childTreeSnapshots
84
80
private val saveableStateRegistry: SaveableStateRegistry
85
- private val childNodes = mutableVectorOf<ComposeChildNode <* , * , * >>()
81
+
82
+ // Don't allocate childNodes list until a child is rendered, leaf node optimization.
83
+ private var childNodes: MutableVector <ComposeChildNode <* , * , * >>? = null
86
84
87
85
private val outputsChannel = Channel <OutputT >(capacity = OUTPUT_QUEUE_LIMIT )
88
86
@@ -130,8 +128,7 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
130
128
snapshot = workflowSnapshot,
131
129
workflowScope = this ,
132
130
session = this ,
133
- proceed = { innerProps, innerSnapshot, _ ->
134
- lastProps = innerProps
131
+ proceed = { _, innerSnapshot, _ ->
135
132
restoredRegistry = restoreSaveableStateRegistryFromSnapshot(innerSnapshot)
136
133
ComposeWorkflowState
137
134
}
@@ -157,7 +154,7 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
157
154
LocalWorkflowComposableRenderer provides this
158
155
) {
159
156
interceptor.onRenderComposeWorkflow(
160
- renderProps = lastProps ,
157
+ renderProps = props ,
161
158
emitOutput = onEmitOutput,
162
159
session = this ,
163
160
proceed = { innerProps, innerEmitOutput ->
@@ -228,11 +225,11 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
228
225
229
226
@OptIn(ExperimentalCoroutinesApi ::class , DelicateCoroutinesApi ::class )
230
227
override fun onNextAction (selector : SelectBuilder <ActionProcessingResult >): Boolean {
231
- var empty = childNodes.fold(true ) { empty, child ->
228
+ var empty = childNodes? .fold(true ) { empty, child ->
232
229
// Do this separately so the compiler doesn't avoid it if empty is already false.
233
230
val childEmpty = child.onNextAction(selector)
234
231
empty && childEmpty
235
- }
232
+ } ? : true
236
233
237
234
empty = empty && (outputsChannel.isEmpty || outputsChannel.isClosedForReceive)
238
235
with (selector) {
@@ -307,15 +304,17 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
307
304
}
308
305
309
306
private fun addChildNode (childNode : ComposeChildNode <* , * , * >) {
310
- childNodes + = childNode
307
+ ( childNodes ? : MutableVector < ComposeChildNode < * , * , * >>(). also { childNodes = it }) + = childNode
311
308
}
312
309
313
310
private fun removeChildNode (childNode : ComposeChildNode <* , * , * >) {
314
- childNodes - = childNode
311
+ val childNodes = childNodes
312
+ ? : throw AssertionError (" removeChildNode called before addChildNode" )
313
+ childNodes.remove(childNode)
315
314
}
316
315
317
316
private fun createChildSnapshots (): Map <WorkflowNodeId , TreeSnapshot > = buildMap {
318
- childNodes.forEach { child ->
317
+ childNodes? .forEach { child ->
319
318
put(child.id, child.snapshot())
320
319
}
321
320
}
0 commit comments