Skip to content

Commit 32cb7bb

Browse files
Merge pull request #1239 from square/py/nit_cleanups
Add smol caching & a utility method
2 parents cd7485e + f6ec466 commit 32cb7bb

File tree

7 files changed

+44
-17
lines changed

7 files changed

+44
-17
lines changed

workflow-core/api/workflow-core.api

+1
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ public final class com/squareup/workflow1/WorkflowIdentifier {
254254
public static final field Companion Lcom/squareup/workflow1/WorkflowIdentifier$Companion;
255255
public fun equals (Ljava/lang/Object;)Z
256256
public final fun getRealIdentifierType ()Lcom/squareup/workflow1/WorkflowIdentifierType;
257+
public final fun getRealType ()Lcom/squareup/workflow1/WorkflowIdentifierType;
257258
public fun hashCode ()I
258259
public final fun toByteStringOrNull ()Lokio/ByteString;
259260
public fun toString ()Ljava/lang/String;

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

+25-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import okio.Buffer
99
import okio.ByteString
1010
import okio.EOFException
1111
import kotlin.LazyThreadSafetyMode.PUBLICATION
12+
import kotlin.concurrent.Volatile
1213
import kotlin.jvm.JvmMultifileClass
1314
import kotlin.jvm.JvmName
1415
import kotlin.reflect.KClass
@@ -58,6 +59,17 @@ public class WorkflowIdentifier internal constructor(
5859

5960
private val proxiedIdentifiers = generateSequence(this) { it.proxiedIdentifier }
6061

62+
@Volatile
63+
private var cachedToString: String? = null
64+
65+
/**
66+
* Either a [KClass] or [KType] representing the "real" type that this identifier
67+
* identifies – i.e. which is not an [ImpostorWorkflow].
68+
*/
69+
public val realType: WorkflowIdentifierType by lazy(PUBLICATION) {
70+
proxiedIdentifiers.last().type
71+
}
72+
6173
/**
6274
* If this identifier is snapshottable, returns the serialized form of the identifier.
6375
* If it is not snapshottable, returns null.
@@ -83,23 +95,26 @@ public class WorkflowIdentifier internal constructor(
8395
}
8496
}
8597

86-
/**
87-
* Returns either a [KClass] or [KType] representing the "real" type that this identifier
88-
* identifies – i.e. which is not an [ImpostorWorkflow].
89-
*/
90-
public fun getRealIdentifierType(): WorkflowIdentifierType = proxiedIdentifiers.last().type
98+
@Deprecated("This is now a lazily computed val", ReplaceWith("realType"))
99+
public fun getRealIdentifierType(): WorkflowIdentifierType = realType
91100

92101
/**
93102
* If this identifier identifies an [ImpostorWorkflow], returns the result of that workflow's
94103
* [ImpostorWorkflow.describeRealIdentifier] method, otherwise returns a description of this
95104
* identifier including the name of its workflow type and any [ImpostorWorkflow.realIdentifier]s.
96105
*
97106
*/
98-
override fun toString(): String =
99-
description?.invoke()
100-
?: proxiedIdentifiers
101-
.joinToString { it.typeName }
102-
.let { "WorkflowIdentifier($it)" }
107+
override fun toString(): String {
108+
return cachedToString ?: (
109+
description?.invoke()
110+
?: proxiedIdentifiers
111+
.joinToString { it.typeName }
112+
.let { "WorkflowIdentifier($it)" }
113+
)
114+
.also {
115+
cachedToString = it
116+
}
117+
}
103118

104119
override fun equals(other: Any?): Boolean = when {
105120
this === other -> true

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,22 @@ internal class WorkflowIdentifierTest {
153153

154154
@Test fun getRealIdentifierType_returns_self_for_non_impostor_workflow() {
155155
val id = TestWorkflow1.identifier
156-
assertEquals(Snapshottable(TestWorkflow1::class), id.getRealIdentifierType())
156+
assertEquals(Snapshottable(TestWorkflow1::class), id.realType)
157157
}
158158

159159
@Test fun getRealIdentifierType_returns_real_identifier_for_impostor_workflow() {
160160
val id = TestImpostor1(TestWorkflow1).identifier
161-
assertEquals(Snapshottable(TestWorkflow1::class), id.getRealIdentifierType())
161+
assertEquals(Snapshottable(TestWorkflow1::class), id.realType)
162162
}
163163

164164
@Test fun getRealIdentifierType_returns_leaf_real_identifier_for_impostor_workflow_chain() {
165165
val id = TestImpostor2(TestImpostor1(TestWorkflow1)).identifier
166-
assertEquals(Snapshottable(TestWorkflow1::class), id.getRealIdentifierType())
166+
assertEquals(Snapshottable(TestWorkflow1::class), id.realType)
167167
}
168168

169169
@Test fun getRealIdentifierType_returns_KType_of_unsnapshottable_identifier() {
170170
val id = TestUnsnapshottableImpostor(typeOf<List<String>>()).identifier
171-
assertEquals(Unsnapshottable(typeOf<List<String>>()), id.getRealIdentifierType())
171+
assertEquals(Unsnapshottable(typeOf<List<String>>()), id.realType)
172172
}
173173

174174
public object TestWorkflow1 : Workflow<Nothing, Nothing, Nothing> {

workflow-runtime/api/workflow-runtime.api

+5
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,10 @@ public abstract interface class com/squareup/workflow1/WorkflowInterceptor$Workf
104104
public abstract fun getRenderKey ()Ljava/lang/String;
105105
public abstract fun getRuntimeConfig ()Ljava/util/Set;
106106
public abstract fun getSessionId ()J
107+
public abstract fun isRootWorkflow ()Z
108+
}
109+
110+
public final class com/squareup/workflow1/WorkflowInterceptor$WorkflowSession$DefaultImpls {
111+
public static fun isRootWorkflow (Lcom/squareup/workflow1/WorkflowInterceptor$WorkflowSession;)Z
107112
}
108113

workflow-runtime/src/commonMain/kotlin/com/squareup/workflow1/WorkflowInterceptor.kt

+6
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ public interface WorkflowInterceptor {
152152
/** The parent [WorkflowSession] of this workflow, or null if this is the root workflow. */
153153
public val parent: WorkflowSession?
154154

155+
/**
156+
* true if this is the root workflow, in which case [parent] is null.
157+
*/
158+
public val isRootWorkflow: Boolean
159+
get() = parent == null
160+
155161
/** The [RuntimeConfig] of the runtime this session is executing in. */
156162
public val runtimeConfig: RuntimeConfig
157163
}

workflow-testing/src/main/java/com/squareup/workflow1/testing/RealRenderTester.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ internal fun createRenderChildInvocation(
360360
internal fun WorkflowIdentifier.realTypeMatchesExpectation(
361361
expected: WorkflowIdentifier
362362
): Boolean {
363-
val expectedType = expected.getRealIdentifierType()
364-
val actualType = getRealIdentifierType()
363+
val expectedType = expected.realType
364+
val actualType = realType
365365
return actualType.matchesExpectation(expectedType)
366366
}
367367

workflow-tracing/src/main/java/com/squareup/workflow1/diagnostic/tracing/TracingWorkflowInterceptor.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ public class TracingWorkflowInterceptor internal constructor(
494494
}
495495

496496
private fun WorkflowIdentifier.toLoggingName(): String {
497-
val type = getRealIdentifierType()
497+
val type = realType
498498
return when {
499499
type is Snapshottable && type.kClass != null -> type.kClass!!.toLoggingName()
500500
type is Unsnapshottable -> type.kType.toLoggingName()

0 commit comments

Comments
 (0)