|
3 | 3 |
|
4 | 4 | package com.squareup.workflow1 |
5 | 5 |
|
6 | | -import kotlin.LazyThreadSafetyMode.NONE |
7 | 6 | import kotlin.jvm.JvmMultifileClass |
8 | 7 | import kotlin.jvm.JvmName |
9 | 8 |
|
@@ -33,12 +32,6 @@ public abstract class StatelessWorkflow<in PropsT, out OutputT, out RenderingT> |
33 | 32 | ) : BaseRenderContext<@UnsafeVariance PropsT, Nothing, @UnsafeVariance OutputT> by |
34 | 33 | baseContext as BaseRenderContext<PropsT, Nothing, OutputT> |
35 | 34 |
|
36 | | - @Suppress("UNCHECKED_CAST") |
37 | | - private val statefulWorkflow = Workflow.stateful<PropsT, Unit, OutputT, RenderingT>( |
38 | | - initialState = { Unit }, |
39 | | - render = { props, _ -> render(props, RenderContext(this, this@StatelessWorkflow)) } |
40 | | - ) |
41 | | - |
42 | 35 | /** |
43 | 36 | * Called at least once any time one of the following things happens: |
44 | 37 | * - This workflow's [renderProps] change (via the parent passing a different one in). |
@@ -70,11 +63,39 @@ public abstract class StatelessWorkflow<in PropsT, out OutputT, out RenderingT> |
70 | 63 | * Satisfies the [Workflow] interface by wrapping `this` in a [StatefulWorkflow] with `Unit` |
71 | 64 | * state. |
72 | 65 | * |
73 | | - * This method is called a few times per instance, but we don't need to allocate a new |
74 | | - * [StatefulWorkflow] every time, so we store it in a private property. |
| 66 | + * This is only called when the instance of the Workflow is created, so we can recreate this each |
| 67 | + * time. |
75 | 68 | */ |
76 | | - final override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, RenderingT> = |
77 | | - statefulWorkflow |
| 69 | + final override fun asStatefulWorkflow(): StatefulWorkflow<PropsT, *, OutputT, RenderingT> { |
| 70 | + return object : StatefulWorkflow<PropsT, Unit, OutputT, RenderingT>() { |
| 71 | + // We want to cache the render context so that we don't have to recreate it each time |
| 72 | + // render() is called. |
| 73 | + private var cachedStatelessRenderContext: |
| 74 | + StatelessWorkflow<PropsT, OutputT, RenderingT>.RenderContext? = null |
| 75 | + private var cachedStatefulRenderContext: |
| 76 | + StatefulWorkflow<PropsT, Unit, OutputT, RenderingT>.RenderContext? = null |
| 77 | + |
| 78 | + override fun initialState( |
| 79 | + props: PropsT, |
| 80 | + snapshot: Snapshot? |
| 81 | + ) = Unit |
| 82 | + |
| 83 | + override fun render( |
| 84 | + renderProps: PropsT, |
| 85 | + renderState: Unit, |
| 86 | + context: RenderContext |
| 87 | + ): RenderingT { |
| 88 | + if (cachedStatelessRenderContext == null || context != cachedStatefulRenderContext) { |
| 89 | + // Recreate it if the underlying context changed. |
| 90 | + cachedStatelessRenderContext = RenderContext(context, this@StatelessWorkflow) |
| 91 | + } |
| 92 | + cachedStatefulRenderContext = context |
| 93 | + return render(renderProps, cachedStatelessRenderContext!!) |
| 94 | + } |
| 95 | + |
| 96 | + override fun snapshotState(state: Unit): Snapshot? = null |
| 97 | + } |
| 98 | + } |
78 | 99 | } |
79 | 100 |
|
80 | 101 | /** |
|
0 commit comments