Skip to content

Commit e565108

Browse files
Dispatch WorkflowInterceptor.onPropsChanged from Compose workflow nodes.
1 parent 912b1c7 commit e565108

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

workflow-runtime/src/commonMain/kotlin/com/squareup/workflow1/internal/compose/ComposeWorkflowChildNode.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@ package com.squareup.workflow1.internal.compose
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.DisposableEffect
5+
import androidx.compose.runtime.NonRestartableComposable
6+
import androidx.compose.runtime.ReadOnlyComposable
57
import androidx.compose.runtime.collection.MutableVector
68
import androidx.compose.runtime.currentRecomposeScope
79
import androidx.compose.runtime.getValue
810
import androidx.compose.runtime.key
11+
import androidx.compose.runtime.mutableStateOf
912
import androidx.compose.runtime.remember
1013
import androidx.compose.runtime.rememberCoroutineScope
1114
import androidx.compose.runtime.rememberUpdatedState
1215
import androidx.compose.runtime.saveable.LocalSaveableStateRegistry
1316
import androidx.compose.runtime.saveable.SaveableStateRegistry
17+
import androidx.compose.runtime.setValue
18+
import androidx.compose.runtime.snapshots.Snapshot
1419
import com.squareup.workflow1.ActionApplied
1520
import com.squareup.workflow1.ActionProcessingResult
1621
import com.squareup.workflow1.NoopWorkflowInterceptor
@@ -29,8 +34,8 @@ import com.squareup.workflow1.compose.WorkflowComposableRenderer
2934
import com.squareup.workflow1.identifier
3035
import com.squareup.workflow1.internal.IdCounter
3136
import com.squareup.workflow1.internal.WorkflowNodeId
32-
import com.squareup.workflow1.internal.requireSend
3337
import com.squareup.workflow1.internal.createId
38+
import com.squareup.workflow1.internal.requireSend
3439
import com.squareup.workflow1.workflowSessionToString
3540
import kotlinx.coroutines.CoroutineName
3641
import kotlinx.coroutines.CoroutineScope
@@ -96,6 +101,8 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
96101
}
97102
}
98103

104+
private var lastProps by mutableStateOf(initialProps)
105+
99106
/**
100107
* Function invoked when [onNextAction] receives an output from [outputsChannel].
101108
*/
@@ -149,6 +156,9 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
149156
// inside a renderChild call and renderChild does the keying.
150157
log("rendering workflow: props=$props")
151158
workflow as ComposeWorkflow
159+
160+
notifyInterceptorWhenPropsChanged(props)
161+
152162
return withCompositionLocals(
153163
LocalSaveableStateRegistry provides saveableStateRegistry,
154164
LocalWorkflowComposableRenderer provides this
@@ -167,6 +177,26 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
167177
}
168178
}
169179

180+
@ReadOnlyComposable
181+
@NonRestartableComposable
182+
@Composable
183+
private fun notifyInterceptorWhenPropsChanged(newProps: PropsT) {
184+
// Don't both asking the composition to track reads of lastProps since this is the only function
185+
// that will every write to it.
186+
Snapshot.withoutReadObservation {
187+
if (lastProps != newProps) {
188+
interceptor.onPropsChanged(
189+
old = lastProps,
190+
new = newProps,
191+
state = ComposeWorkflowState,
192+
session = this,
193+
proceed = { _, _, _ -> ComposeWorkflowState },
194+
)
195+
lastProps = newProps
196+
}
197+
}
198+
}
199+
170200
@Composable
171201
override fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild(
172202
childWorkflow: Workflow<ChildPropsT, ChildOutputT, ChildRenderingT>,

0 commit comments

Comments
 (0)