|
| 1 | +/* |
| 2 | + * Copyright 2020 Square Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.squareup.workflow |
| 17 | + |
| 18 | +import com.squareup.workflow.WorkflowInterceptor.WorkflowSession |
| 19 | +import kotlinx.coroutines.CoroutineScope |
| 20 | + |
| 21 | +/** |
| 22 | + * Provides hooks into the workflow runtime that can be used to instrument or modify the behavior |
| 23 | + * of workflows. |
| 24 | + * |
| 25 | + * This interface's methods mirror the methods of [StatefulWorkflow]. It also has one additional |
| 26 | + * method, [onSessionStarted], that is notified when a workflow is started. Each method returns the |
| 27 | + * same thing as the corresponding method on [StatefulWorkflow], and receives the same parameters |
| 28 | + * as well as two extra parameters: |
| 29 | + * |
| 30 | + * - **`proceed`** – A function that _exactly_ mirrors the corresponding function on |
| 31 | + * [StatefulWorkflow], accepting the same parameters and returning the same thing. An interceptor |
| 32 | + * can call this function to run the actual workflow, but it may also decide to not call it at |
| 33 | + * all, or call it multiple times. |
| 34 | + * - **`session`** – A [WorkflowSession] object that can be queried for information about the |
| 35 | + * workflow being intercepted. |
| 36 | + * |
| 37 | + * All methods have default no-op implementations. |
| 38 | + * |
| 39 | + * ## Workflow sessions |
| 40 | + * |
| 41 | + * A single workflow may be rendered by different parents at the same time, or the same parent at |
| 42 | + * different, disjoint times. Each continuous sequence of renderings of a particular workflow type, |
| 43 | + * with the same key passed to [RenderContext.renderChild], is called an "session" of that |
| 44 | + * workflow. The workflow's [StatefulWorkflow.initialState] method will be called at the start of |
| 45 | + * the session, and its state will be maintained by the runtime until the session is finished. |
| 46 | + * Each session is identified by the [WorkflowSession] object passed into the corresponding method |
| 47 | + * in a [WorkflowInterceptor]. |
| 48 | + * |
| 49 | + * In addition to the [WorkflowIdentifier] of the type of the workflow being rendered, this object |
| 50 | + * also knows the [key][WorkflowSession.renderKey] used to render the workflow and the |
| 51 | + * [WorkflowSession] of the [parent][WorkflowSession.parent] workflow that is rendering it. |
| 52 | + * |
| 53 | + * Each session is also assigned a numerical ID that uniquely identifies the session over the |
| 54 | + * life of the entire runtime. This value will remain constant as long as the workflow's parent is |
| 55 | + * rendering it, and then it will never be used again. If this workflow stops being rendered, and |
| 56 | + * then starts again, the value will be different. |
| 57 | + */ |
| 58 | +@ExperimentalWorkflowApi |
| 59 | +interface WorkflowInterceptor { |
| 60 | + |
| 61 | + /** |
| 62 | + * Called when the session is starting, before [onInitialState]. |
| 63 | + * |
| 64 | + * @param workflowScope The [CoroutineScope] that will be used for any side effects the workflow |
| 65 | + * runs, as well as the parent for any workflows it renders. |
| 66 | + */ |
| 67 | + fun onSessionStarted( |
| 68 | + workflowScope: CoroutineScope, |
| 69 | + session: WorkflowSession |
| 70 | + ) = Unit |
| 71 | + |
| 72 | + /** |
| 73 | + * Intercepts calls to [StatefulWorkflow.initialState]. |
| 74 | + */ |
| 75 | + fun <P, S> onInitialState( |
| 76 | + props: P, |
| 77 | + snapshot: Snapshot?, |
| 78 | + proceed: (P, Snapshot?) -> S, |
| 79 | + session: WorkflowSession |
| 80 | + ): S = proceed(props, snapshot) |
| 81 | + |
| 82 | + /** |
| 83 | + * Intercepts calls to [StatefulWorkflow.onPropsChanged]. |
| 84 | + */ |
| 85 | + fun <P, S> onPropsChanged( |
| 86 | + old: P, |
| 87 | + new: P, |
| 88 | + state: S, |
| 89 | + proceed: (P, P, S) -> S, |
| 90 | + session: WorkflowSession |
| 91 | + ): S = proceed(old, new, state) |
| 92 | + |
| 93 | + /** |
| 94 | + * Intercepts calls to [StatefulWorkflow.render]. |
| 95 | + */ |
| 96 | + fun <P, S, O : Any, R> onRender( |
| 97 | + props: P, |
| 98 | + state: S, |
| 99 | + context: RenderContext<S, O>, |
| 100 | + proceed: (P, S, RenderContext<S, O>) -> R, |
| 101 | + session: WorkflowSession |
| 102 | + ): R = proceed(props, state, context) |
| 103 | + |
| 104 | + /** |
| 105 | + * Intercepts calls to [StatefulWorkflow.snapshotState]. |
| 106 | + */ |
| 107 | + fun <S> onSnapshotState( |
| 108 | + state: S, |
| 109 | + proceed: (S) -> Snapshot, |
| 110 | + session: WorkflowSession |
| 111 | + ): Snapshot = proceed(state) |
| 112 | + |
| 113 | + /** |
| 114 | + * Information about the session of a workflow in the runtime that a [WorkflowInterceptor] method |
| 115 | + * is intercepting. |
| 116 | + */ |
| 117 | + @ExperimentalWorkflowApi |
| 118 | + interface WorkflowSession { |
| 119 | + /** The [WorkflowIdentifier] that represents the type of this workflow. */ |
| 120 | + val identifier: WorkflowIdentifier |
| 121 | + |
| 122 | + /** |
| 123 | + * The string key argument that was passed to [RenderContext.renderChild] to render this |
| 124 | + * workflow. |
| 125 | + */ |
| 126 | + val renderKey: String |
| 127 | + |
| 128 | + /** |
| 129 | + * A unique value that identifies the currently-running session of this workflow in the |
| 130 | + * runtime. See the documentation on [WorkflowInterceptor] for more information about what this |
| 131 | + * value represents. |
| 132 | + */ |
| 133 | + val sessionId: Long |
| 134 | + |
| 135 | + /** The parent [WorkflowSession] of this workflow, or null if this is the root workflow. */ |
| 136 | + val parent: WorkflowSession? |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/** A [WorkflowInterceptor] that does not intercept anything. */ |
| 141 | +@ExperimentalWorkflowApi |
| 142 | +object NoopWorkflowInterceptor : WorkflowInterceptor |
| 143 | + |
| 144 | +/** |
| 145 | + * Returns a [StatefulWorkflow] that will intercept all calls to [workflow] via this |
| 146 | + * [WorkflowInterceptor]. |
| 147 | + */ |
| 148 | +@OptIn(ExperimentalWorkflowApi::class) |
| 149 | +internal fun <P, S, O : Any, R> WorkflowInterceptor.intercept( |
| 150 | + workflow: StatefulWorkflow<P, S, O, R>, |
| 151 | + workflowSession: WorkflowSession |
| 152 | +): StatefulWorkflow<P, S, O, R> = if (this === NoopWorkflowInterceptor) { |
| 153 | + workflow |
| 154 | +} else { |
| 155 | + object : StatefulWorkflow<P, S, O, R>() { |
| 156 | + override fun initialState( |
| 157 | + props: P, |
| 158 | + snapshot: Snapshot? |
| 159 | + ): S = onInitialState(props, snapshot, workflow::initialState, workflowSession) |
| 160 | + |
| 161 | + override fun onPropsChanged( |
| 162 | + old: P, |
| 163 | + new: P, |
| 164 | + state: S |
| 165 | + ): S = onPropsChanged(old, new, state, workflow::onPropsChanged, workflowSession) |
| 166 | + |
| 167 | + override fun render( |
| 168 | + props: P, |
| 169 | + state: S, |
| 170 | + context: RenderContext<S, O> |
| 171 | + ): R = onRender(props, state, context, workflow::render, workflowSession) |
| 172 | + |
| 173 | + override fun snapshotState(state: S): Snapshot = |
| 174 | + onSnapshotState(state, workflow::snapshotState, workflowSession) |
| 175 | + |
| 176 | + override fun toString(): String = "InterceptedWorkflow($workflow, $this@intercept)" |
| 177 | + } |
| 178 | +} |
0 commit comments