Skip to content

Commit a78efa5

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

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ 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
1418
import com.squareup.workflow1.ActionApplied
1519
import com.squareup.workflow1.ActionProcessingResult
1620
import com.squareup.workflow1.NoopWorkflowInterceptor
@@ -29,8 +33,8 @@ import com.squareup.workflow1.compose.WorkflowComposableRenderer
2933
import com.squareup.workflow1.identifier
3034
import com.squareup.workflow1.internal.IdCounter
3135
import com.squareup.workflow1.internal.WorkflowNodeId
32-
import com.squareup.workflow1.internal.requireSend
3336
import com.squareup.workflow1.internal.createId
37+
import com.squareup.workflow1.internal.requireSend
3438
import com.squareup.workflow1.workflowSessionToString
3539
import kotlinx.coroutines.CoroutineName
3640
import kotlinx.coroutines.CoroutineScope
@@ -96,6 +100,8 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
96100
}
97101
}
98102

103+
private var lastProps by mutableStateOf(initialProps)
104+
99105
/**
100106
* Function invoked when [onNextAction] receives an output from [outputsChannel].
101107
*/
@@ -149,6 +155,9 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
149155
// inside a renderChild call and renderChild does the keying.
150156
log("rendering workflow: props=$props")
151157
workflow as ComposeWorkflow
158+
159+
notifyInterceptorWhenPropsChanged(props)
160+
152161
return withCompositionLocals(
153162
LocalSaveableStateRegistry provides saveableStateRegistry,
154163
LocalWorkflowComposableRenderer provides this
@@ -167,6 +176,22 @@ internal class ComposeWorkflowChildNode<PropsT, OutputT, RenderingT>(
167176
}
168177
}
169178

179+
@ReadOnlyComposable
180+
@NonRestartableComposable
181+
@Composable
182+
private fun notifyInterceptorWhenPropsChanged(newProps: PropsT) {
183+
if (lastProps != newProps) {
184+
interceptor.onPropsChanged(
185+
old = lastProps,
186+
new = newProps,
187+
state = ComposeWorkflowState,
188+
session = this,
189+
proceed = { _, _, _ -> ComposeWorkflowState },
190+
)
191+
lastProps = newProps
192+
}
193+
}
194+
170195
@Composable
171196
override fun <ChildPropsT, ChildOutputT, ChildRenderingT> renderChild(
172197
childWorkflow: Workflow<ChildPropsT, ChildOutputT, ChildRenderingT>,

0 commit comments

Comments
 (0)