Skip to content

Commit 6a2c0e8

Browse files
Change RenderContext from inner class to nested class
1 parent cdf8890 commit 6a2c0e8

File tree

2 files changed

+33
-49
lines changed

2 files changed

+33
-49
lines changed

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

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
package com.squareup.workflow1
66

77
import com.squareup.workflow1.RuntimeConfigOptions.STABLE_EVENT_HANDLERS
8-
import com.squareup.workflow1.StatefulWorkflow.RenderContext
9-
import com.squareup.workflow1.WorkflowAction.Companion.toString
108
import kotlinx.coroutines.CoroutineScope
119
import kotlin.jvm.JvmMultifileClass
1210
import kotlin.jvm.JvmName
@@ -75,7 +73,7 @@ public abstract class StatefulWorkflow<
7573
out RenderingT
7674
> : Workflow<PropsT, OutputT, RenderingT>, IdCacheable {
7775

78-
public inner class RenderContext internal constructor(
76+
public class RenderContext<PropsT, StateT, OutputT> internal constructor(
7977
baseContext: BaseRenderContext<PropsT, StateT, OutputT>
8078
) : BaseRenderContext<PropsT, StateT, OutputT> by baseContext {
8179
@PublishedApi
@@ -748,15 +746,6 @@ public abstract class StatefulWorkflow<
748746
?: onFailedCast(name, CurrentStateT::class, state)
749747
}
750748

751-
@PublishedApi
752-
internal fun defaultOnFailedCast(
753-
name: String,
754-
expectedType: KClass<*>,
755-
state: StateT
756-
) {
757-
println("$name expected state of type ${expectedType.simpleName}, got $state")
758-
}
759-
760749
/**
761750
* Called from [RenderContext.renderChild] when the state machine is first started, to get the
762751
* initial state.
@@ -829,7 +818,7 @@ public abstract class StatefulWorkflow<
829818
public abstract fun render(
830819
renderProps: PropsT,
831820
renderState: StateT,
832-
context: RenderContext
821+
context: RenderContext<PropsT, StateT, OutputT>
833822
): RenderingT
834823

835824
/**
@@ -861,6 +850,18 @@ public abstract class StatefulWorkflow<
861850
*/
862851
final override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, StateT, OutputT, RenderingT> =
863852
this
853+
854+
companion object {
855+
856+
@PublishedApi
857+
internal fun <StateT> defaultOnFailedCast(
858+
name: String,
859+
expectedType: KClass<*>,
860+
state: StateT
861+
) {
862+
println("$name expected state of type ${expectedType.simpleName}, got $state")
863+
}
864+
}
864865
}
865866

866867
/**
@@ -870,21 +871,16 @@ public abstract class StatefulWorkflow<
870871
public fun <PropsT, StateT, OutputT, RenderingT> RenderContext(
871872
baseContext: BaseRenderContext<PropsT, StateT, OutputT>,
872873
workflow: StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>
873-
): StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.RenderContext =
874-
(baseContext as? StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.RenderContext)
875-
?: workflow.RenderContext(baseContext)
874+
): StatefulWorkflow.RenderContext<PropsT, StateT, OutputT> =
875+
(baseContext as? StatefulWorkflow.RenderContext<PropsT, StateT, OutputT>)
876+
?: StatefulWorkflow.RenderContext<PropsT, StateT, OutputT>(baseContext)
876877

877878
/**
878879
* Returns a stateful [Workflow] implemented via the given functions.
879880
*/
880881
public inline fun <PropsT, StateT, OutputT, RenderingT> Workflow.Companion.stateful(
881882
crossinline initialState: (PropsT, Snapshot?) -> StateT,
882-
crossinline render: StatefulWorkflow<
883-
PropsT,
884-
StateT,
885-
OutputT,
886-
*
887-
>.RenderContext.(
883+
crossinline render: StatefulWorkflow.RenderContext<PropsT, StateT, OutputT>.(
888884
props: PropsT,
889885
state: StateT
890886
) -> RenderingT,
@@ -910,7 +906,7 @@ public inline fun <PropsT, StateT, OutputT, RenderingT> Workflow.Companion.state
910906
override fun render(
911907
renderProps: PropsT,
912908
renderState: StateT,
913-
context: RenderContext
909+
context: RenderContext<PropsT, StateT, OutputT>
914910
): RenderingT = render(context, renderProps, renderState)
915911

916912
override fun snapshotState(state: StateT) = snapshot(state)
@@ -921,12 +917,7 @@ public inline fun <PropsT, StateT, OutputT, RenderingT> Workflow.Companion.state
921917
*/
922918
public inline fun <StateT, OutputT, RenderingT> Workflow.Companion.stateful(
923919
crossinline initialState: (Snapshot?) -> StateT,
924-
crossinline render: StatefulWorkflow<
925-
Unit,
926-
StateT,
927-
OutputT,
928-
*
929-
>.RenderContext.(state: StateT) -> RenderingT,
920+
crossinline render: StatefulWorkflow.RenderContext<Unit, StateT, OutputT>.(state: StateT) -> RenderingT,
930921
crossinline snapshot: (StateT) -> Snapshot?
931922
): StatefulWorkflow<Unit, StateT, OutputT, RenderingT> = stateful(
932923
{ _, initialSnapshot -> initialState(initialSnapshot) },
@@ -941,7 +932,7 @@ public inline fun <StateT, OutputT, RenderingT> Workflow.Companion.stateful(
941932
*/
942933
public inline fun <PropsT, StateT, OutputT, RenderingT> Workflow.Companion.stateful(
943934
crossinline initialState: (PropsT) -> StateT,
944-
crossinline render: StatefulWorkflow<PropsT, StateT, OutputT, *>.RenderContext.(
935+
crossinline render: StatefulWorkflow.RenderContext<PropsT, StateT, OutputT>.(
945936
props: PropsT,
946937
state: StateT
947938
) -> RenderingT,
@@ -964,12 +955,9 @@ public inline fun <PropsT, StateT, OutputT, RenderingT> Workflow.Companion.state
964955
*/
965956
public inline fun <StateT, OutputT, RenderingT> Workflow.Companion.stateful(
966957
initialState: StateT,
967-
crossinline render: StatefulWorkflow<
968-
Unit,
958+
crossinline render: StatefulWorkflow.RenderContext<Unit,
969959
StateT,
970-
OutputT,
971-
*
972-
>.RenderContext.(state: StateT) -> RenderingT
960+
OutputT>.(state: StateT) -> RenderingT
973961
): StatefulWorkflow<Unit, StateT, OutputT, RenderingT> = stateful(
974962
{ initialState },
975963
{ _, state -> render(state) }

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public abstract class StatelessWorkflow<PropsT, OutputT, out RenderingT> :
2929
Workflow<PropsT, OutputT, RenderingT>, IdCacheable {
3030

3131
@Suppress("UNCHECKED_CAST")
32-
public inner class RenderContext internal constructor(
32+
public class RenderContext<PropsT, OutputT> internal constructor(
3333
baseContext: BaseRenderContext<PropsT, *, OutputT>
3434
) : BaseRenderContext<PropsT, Nothing, OutputT> by
3535
baseContext as BaseRenderContext<PropsT, Nothing, OutputT> {
@@ -235,14 +235,14 @@ public abstract class StatelessWorkflow<PropsT, OutputT, out RenderingT> :
235235
* render() is called.
236236
*/
237237
private var cachedStatelessRenderContext:
238-
StatelessWorkflow<PropsT, OutputT, RenderingT>.RenderContext? = null
238+
StatelessWorkflow.RenderContext<PropsT, OutputT>? = null
239239

240240
/**
241241
* We must know if the RenderContext we are passed (which is a StatefulWorkflow.RenderContext)
242242
* has changed, so keep track of it.
243243
*/
244244
private var canonicalStatefulRenderContext:
245-
StatefulWorkflow<PropsT, Unit, OutputT, RenderingT>.RenderContext? = null
245+
StatefulWorkflow.RenderContext<PropsT, Unit, OutputT>? = null
246246

247247
override fun initialState(
248248
props: PropsT,
@@ -252,7 +252,7 @@ public abstract class StatelessWorkflow<PropsT, OutputT, out RenderingT> :
252252
override fun render(
253253
renderProps: PropsT,
254254
renderState: Unit,
255-
context: RenderContext
255+
context: RenderContext<PropsT, Unit, OutputT>
256256
): RenderingT {
257257
// The `RenderContext` used *might* change - primarily in the case of our tests. E.g., The
258258
// `RenderTester` uses a special NoOp context to render twice to test for idempotency.
@@ -306,7 +306,7 @@ public abstract class StatelessWorkflow<PropsT, OutputT, out RenderingT> :
306306
*/
307307
public abstract fun render(
308308
renderProps: PropsT,
309-
context: RenderContext
309+
context: RenderContext<PropsT, OutputT>
310310
): RenderingT
311311

312312
/**
@@ -332,9 +332,9 @@ public abstract class StatelessWorkflow<PropsT, OutputT, out RenderingT> :
332332
public fun <PropsT, OutputT, RenderingT> RenderContext(
333333
baseContext: BaseRenderContext<PropsT, *, OutputT>,
334334
workflow: StatelessWorkflow<PropsT, OutputT, RenderingT>
335-
): StatelessWorkflow<PropsT, OutputT, RenderingT>.RenderContext =
336-
(baseContext as? StatelessWorkflow<PropsT, OutputT, RenderingT>.RenderContext)
337-
?: workflow.RenderContext(baseContext)
335+
): StatelessWorkflow.RenderContext<PropsT, OutputT> =
336+
(baseContext as? StatelessWorkflow.RenderContext<PropsT, OutputT>)
337+
?: StatelessWorkflow.RenderContext<PropsT, OutputT>(baseContext)
338338

339339
/**
340340
* Returns a stateless [Workflow] via the given [render] function.
@@ -344,16 +344,12 @@ public fun <PropsT, OutputT, RenderingT> RenderContext(
344344
* their own internal state.
345345
*/
346346
public inline fun <PropsT, OutputT, RenderingT> Workflow.Companion.stateless(
347-
crossinline render: StatelessWorkflow<
348-
PropsT,
349-
OutputT,
350-
RenderingT
351-
>.RenderContext.(props: PropsT) -> RenderingT
347+
crossinline render: StatelessWorkflow.RenderContext<PropsT, OutputT>.(props: PropsT) -> RenderingT
352348
): Workflow<PropsT, OutputT, RenderingT> =
353349
object : StatelessWorkflow<PropsT, OutputT, RenderingT>() {
354350
override fun render(
355351
renderProps: PropsT,
356-
context: RenderContext
352+
context: RenderContext<PropsT, OutputT>
357353
): RenderingT = render(context, renderProps)
358354
}
359355

0 commit comments

Comments
 (0)