Skip to content

Commit 57aeebf

Browse files
Cache identifier
1 parent a39c288 commit 57aeebf

File tree

22 files changed

+73
-25
lines changed

22 files changed

+73
-25
lines changed

samples/compose-samples/src/main/java/com/squareup/sample/compose/hellocomposeworkflow/ComposeWorkflow.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import com.squareup.workflow1.RenderContext
55
import com.squareup.workflow1.Sink
66
import com.squareup.workflow1.StatefulWorkflow
77
import com.squareup.workflow1.Workflow
8+
import com.squareup.workflow1.WorkflowIdentifier
9+
import com.squareup.workflow1.computeIdentifier
810
import com.squareup.workflow1.ui.ViewEnvironment
911
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
1012
import com.squareup.workflow1.ui.compose.ComposeScreen
@@ -48,6 +50,8 @@ abstract class ComposeWorkflow<in PropsT, out OutputT : Any> :
4850

4951
override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, ComposeScreen> =
5052
ComposeWorkflowImpl(this)
53+
54+
override val identifier: WorkflowIdentifier = computeIdentifier()
5155
}
5256

5357
/**

workflow-core/api/workflow-core.api

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public final class com/squareup/workflow1/Snapshots {
118118
public abstract class com/squareup/workflow1/StatefulWorkflow : com/squareup/workflow1/Workflow {
119119
public fun <init> ()V
120120
public final fun asStatefulWorkflow ()Lcom/squareup/workflow1/StatefulWorkflow;
121+
public fun getIdentifier ()Lcom/squareup/workflow1/WorkflowIdentifier;
121122
public abstract fun initialState (Ljava/lang/Object;Lcom/squareup/workflow1/Snapshot;)Ljava/lang/Object;
122123
public fun onPropsChanged (Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;
123124
public abstract fun render (Ljava/lang/Object;Ljava/lang/Object;Lcom/squareup/workflow1/StatefulWorkflow$RenderContext;)Ljava/lang/Object;
@@ -144,6 +145,7 @@ public final class com/squareup/workflow1/StatefulWorkflow$RenderContext : com/s
144145
public abstract class com/squareup/workflow1/StatelessWorkflow : com/squareup/workflow1/Workflow {
145146
public fun <init> ()V
146147
public final fun asStatefulWorkflow ()Lcom/squareup/workflow1/StatefulWorkflow;
148+
public fun getIdentifier ()Lcom/squareup/workflow1/WorkflowIdentifier;
147149
public abstract fun render (Ljava/lang/Object;Lcom/squareup/workflow1/StatelessWorkflow$RenderContext;)Ljava/lang/Object;
148150
}
149151

@@ -191,6 +193,7 @@ public final class com/squareup/workflow1/Worker$DefaultImpls {
191193
public abstract interface class com/squareup/workflow1/Workflow {
192194
public static final field Companion Lcom/squareup/workflow1/Workflow$Companion;
193195
public abstract fun asStatefulWorkflow ()Lcom/squareup/workflow1/StatefulWorkflow;
196+
public abstract fun getIdentifier ()Lcom/squareup/workflow1/WorkflowIdentifier;
194197
}
195198

196199
public final class com/squareup/workflow1/Workflow$Companion {
@@ -283,8 +286,8 @@ public final class com/squareup/workflow1/Workflows {
283286
public static synthetic fun action$default (Lcom/squareup/workflow1/StatelessWorkflow;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lcom/squareup/workflow1/WorkflowAction;
284287
public static synthetic fun action$default (Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lcom/squareup/workflow1/WorkflowAction;
285288
public static final fun applyTo (Lcom/squareup/workflow1/WorkflowAction;Ljava/lang/Object;Ljava/lang/Object;)Lkotlin/Pair;
289+
public static final fun computeIdentifier (Lcom/squareup/workflow1/Workflow;)Lcom/squareup/workflow1/WorkflowIdentifier;
286290
public static final fun contraMap (Lcom/squareup/workflow1/Sink;Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/Sink;
287-
public static final fun getIdentifier (Lcom/squareup/workflow1/Workflow;)Lcom/squareup/workflow1/WorkflowIdentifier;
288291
public static final fun mapRendering (Lcom/squareup/workflow1/Workflow;Lkotlin/jvm/functions/Function1;)Lcom/squareup/workflow1/Workflow;
289292
public static final fun renderChild (Lcom/squareup/workflow1/BaseRenderContext;Lcom/squareup/workflow1/Workflow;Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;
290293
public static final fun renderChild (Lcom/squareup/workflow1/BaseRenderContext;Lcom/squareup/workflow1/Workflow;Ljava/lang/String;)Ljava/lang/Object;

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/ImpostorWorkflow.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import kotlin.jvm.JvmName
1818
public interface ImpostorWorkflow {
1919
/**
2020
* The [WorkflowIdentifier] of another workflow to be combined with the identifier of this
21-
* workflow, as obtained by [Workflow.identifier].
21+
* workflow, as obtained by [Workflow.computeIdentifier].
2222
*
2323
* For workflows that implement operators, this should be the identifier of the upstream
2424
* [Workflow] that this workflow wraps.

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/StatefulWorkflow.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ public abstract class StatefulWorkflow<
129129
context: RenderContext
130130
): RenderingT
131131

132+
override val identifier: WorkflowIdentifier by lazy {
133+
computeIdentifier()
134+
}
135+
132136
/**
133137
* Called whenever the state changes to generate a new [Snapshot] of the state.
134138
*

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/StatelessWorkflow.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ public abstract class StatelessWorkflow<in PropsT, out OutputT, out RenderingT>
5757
context: RenderContext
5858
): RenderingT
5959

60+
override val identifier: WorkflowIdentifier by lazy {
61+
computeIdentifier()
62+
}
63+
6064
/**
6165
* Satisfies the [Workflow] interface by wrapping `this` in a [StatefulWorkflow] with `Unit`
6266
* state.

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/Workflow.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ public interface Workflow<in PropsT, out OutputT, out RenderingT> {
104104
*/
105105
public fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, RenderingT>
106106

107+
public val identifier: WorkflowIdentifier
108+
107109
/**
108110
* Empty companion serves as a hook point to allow us to create `Workflow.foo`
109111
* extension methods elsewhere.
@@ -120,7 +122,7 @@ Workflow<PropsT, OutputT, FromRenderingT>.mapRendering(
120122
transform: (FromRenderingT) -> ToRenderingT
121123
): Workflow<PropsT, OutputT, ToRenderingT> =
122124
object : StatelessWorkflow<PropsT, OutputT, ToRenderingT>(), ImpostorWorkflow {
123-
override val realIdentifier: WorkflowIdentifier get() = this@mapRendering.identifier
125+
override val realIdentifier: WorkflowIdentifier = this@mapRendering.identifier
124126

125127
override fun render(
126128
renderProps: PropsT,

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/WorkflowIdentifier.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,14 @@ public class WorkflowIdentifier internal constructor(
146146
/**
147147
* The [WorkflowIdentifier] that identifies this [Workflow].
148148
*/
149-
public val Workflow<*, *, *>.identifier: WorkflowIdentifier
150-
get() {
151-
val maybeImpostor = this as? ImpostorWorkflow
152-
return WorkflowIdentifier(
153-
type = Snapshottable(this::class),
154-
proxiedIdentifier = maybeImpostor?.realIdentifier,
155-
description = maybeImpostor?.let { it::describeRealIdentifier }
156-
)
157-
}
149+
public fun Workflow<*, *, *>.computeIdentifier(): WorkflowIdentifier {
150+
val maybeImpostor = this as? ImpostorWorkflow
151+
return WorkflowIdentifier(
152+
type = Snapshottable(this::class),
153+
proxiedIdentifier = maybeImpostor?.realIdentifier,
154+
description = maybeImpostor?.let { it::describeRealIdentifier }
155+
)
156+
}
158157

159158
/**
160159
* Creates a [WorkflowIdentifier] that is not capable of being snapshotted and will cause any

workflow-core/src/commonTest/kotlin/com/squareup/workflow1/WorkflowIdentifierTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,17 +172,22 @@ internal class WorkflowIdentifierTest {
172172
public object TestWorkflow1 : Workflow<Nothing, Nothing, Nothing> {
173173
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =
174174
throw NotImplementedError()
175+
176+
override val identifier: WorkflowIdentifier = computeIdentifier()
175177
}
176178

177179
public object TestWorkflow2 : Workflow<Nothing, Nothing, Nothing> {
178180
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =
179181
throw NotImplementedError()
182+
183+
override val identifier: WorkflowIdentifier = computeIdentifier()
180184
}
181185

182186
public class TestImpostor1(
183187
private val proxied: Workflow<*, *, *>
184188
) : Workflow<Nothing, Nothing, Nothing>, ImpostorWorkflow {
185189
override val realIdentifier: WorkflowIdentifier = proxied.identifier
190+
override val identifier: WorkflowIdentifier = computeIdentifier()
186191
override fun describeRealIdentifier(): String =
187192
"TestImpostor1(${WorkflowIdentifierTypeNamer.uniqueName(proxied::class)})"
188193
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =
@@ -193,6 +198,7 @@ internal class WorkflowIdentifierTest {
193198
proxied: Workflow<*, *, *>
194199
) : Workflow<Nothing, Nothing, Nothing>, ImpostorWorkflow {
195200
override val realIdentifier: WorkflowIdentifier = proxied.identifier
201+
override val identifier: WorkflowIdentifier = computeIdentifier()
196202
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =
197203
throw NotImplementedError()
198204
}
@@ -201,6 +207,7 @@ internal class WorkflowIdentifierTest {
201207
type: KType
202208
) : Workflow<Nothing, Nothing, Nothing>, ImpostorWorkflow {
203209
override val realIdentifier: WorkflowIdentifier = unsnapshottableIdentifier(type)
210+
override val identifier: WorkflowIdentifier = computeIdentifier()
204211
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =
205212
throw NotImplementedError()
206213
}

workflow-core/src/iosTest/kotlin/com/squareup/workflow1/NativeWorkflowIdentifierTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class NativeWorkflowIdentifierTest {
2121
fun `impostor identifier toString uses full chain when describeRealIdentifier returns null`() {
2222
class TestImpostor : Workflow<Nothing, Nothing, Nothing>, ImpostorWorkflow {
2323
override val realIdentifier: WorkflowIdentifier = TestWorkflow1.identifier
24+
override val identifier: WorkflowIdentifier = computeIdentifier()
2425
override fun describeRealIdentifier(): String? = null
2526

2627
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =

workflow-core/src/jsTest/kotlin/com.squareup.workflow1/JsWorkflowIdentifierTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ internal class JsWorkflowIdentifierTest {
2626
fun impostor_identifier_toString_uses_full_chain_when_describeRealIdentifier_returns_null() {
2727
class TestImpostor : Workflow<Nothing, Nothing, Nothing>, ImpostorWorkflow {
2828
override val realIdentifier: WorkflowIdentifier = TestWorkflow1.identifier
29+
override val identifier: WorkflowIdentifier = computeIdentifier()
2930
override fun describeRealIdentifier(): String? = null
3031

3132
override fun asStatefulWorkflow(): StatefulWorkflow<Nothing, *, Nothing, Nothing> =

0 commit comments

Comments
 (0)