|
| 1 | +/* |
| 2 | + * Copyright 2019 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.debugging |
| 17 | + |
| 18 | +import com.squareup.workflow.debugging.WorkflowUpdateDebugInfo.Kind |
| 19 | +import com.squareup.workflow.debugging.WorkflowUpdateDebugInfo.Kind.Passthrough |
| 20 | +import com.squareup.workflow.debugging.WorkflowUpdateDebugInfo.Kind.Updated |
| 21 | +import com.squareup.workflow.debugging.WorkflowUpdateDebugInfo.Source |
| 22 | +import com.squareup.workflow.debugging.WorkflowUpdateDebugInfo.Source.Sink |
| 23 | +import com.squareup.workflow.debugging.WorkflowUpdateDebugInfo.Source.Subtree |
| 24 | +import com.squareup.workflow.debugging.WorkflowUpdateDebugInfo.Source.Worker |
| 25 | +import com.squareup.workflow.internal.WorkflowId |
| 26 | +import kotlin.LazyThreadSafetyMode.PUBLICATION |
| 27 | + |
| 28 | +/** |
| 29 | + * A description of a workflow update triggered by a [Source] (worker, event, etc). |
| 30 | + * |
| 31 | + * This is a simple linked list that represents a traversal down the workflow tree that starts at |
| 32 | + * the root and indicates, for each workflow, if its child output something that it handled |
| 33 | + * (see [Kind]), or if it was just a parent of a workflow that didn't output anything. |
| 34 | + * |
| 35 | + * When a workflow handles an update, the type of update is indicated by [Source]. |
| 36 | + */ |
| 37 | +data class WorkflowUpdateDebugInfo( |
| 38 | + val workflowType: String, |
| 39 | + val kind: Kind |
| 40 | +) { |
| 41 | + constructor( |
| 42 | + workflowId: WorkflowId<*, *, *>, |
| 43 | + kind: Kind |
| 44 | + ) : this(workflowId.typeDebugString, kind) |
| 45 | + |
| 46 | + /** |
| 47 | + * A sealed class that indicates whether a workflow actually executed a `WorkflowAction`, or |
| 48 | + * was just the ancestor of a workflow that did. |
| 49 | + * |
| 50 | + * Contains two subclasses, see their documentation for details: |
| 51 | + * - [Updated] |
| 52 | + * - [Passthrough] |
| 53 | + */ |
| 54 | + sealed class Kind { |
| 55 | + /** |
| 56 | + * Indicates that this workflow actually executed a `WorkflowAction`. |
| 57 | + * [Updated.source] is a [Source] that indicates if the action was triggered by: |
| 58 | + * - An event ([Source.Sink]) |
| 59 | + * - A worker ([Source.Worker]) |
| 60 | + * - A child workflow emitting an output ([Source.Subtree]) |
| 61 | + */ |
| 62 | + data class Updated(val source: Source) : Kind() |
| 63 | + |
| 64 | + /** |
| 65 | + * Indicates that one of this workflow's descendants executed a |
| 66 | + * `WorkflowAction`, but none of its immediate children emitted an output, so this workflow |
| 67 | + * didn't get directly notified about it. |
| 68 | + */ |
| 69 | + data class Passthrough( |
| 70 | + val key: String, |
| 71 | + val childInfo: WorkflowUpdateDebugInfo |
| 72 | + ) : Kind() |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * A sealed class that indicates what triggered the update. |
| 77 | + * |
| 78 | + * Contains three subclasses, see their documentation for details: |
| 79 | + * - [Sink] |
| 80 | + * - [Worker] |
| 81 | + * - [Subtree] |
| 82 | + */ |
| 83 | + sealed class Source { |
| 84 | + /** |
| 85 | + * Indicates that the update was triggered by an event being received by a |
| 86 | + * [Sink][com.squareup.workflow.Sink] (see |
| 87 | + * [makeActionSink][com.squareup.workflow.RenderContext.makeActionSink]). |
| 88 | + */ |
| 89 | + object Sink : Source() |
| 90 | + |
| 91 | + /** |
| 92 | + * Indicates that the update was triggered by an output emitted from a |
| 93 | + * [Worker][com.squareup.workflow.Worker] being run by this workflow. |
| 94 | + * |
| 95 | + * @param key The string key of the worker that emitted the output. |
| 96 | + * @param output The value emitted from the worker. |
| 97 | + */ |
| 98 | + data class Worker( |
| 99 | + val key: String, |
| 100 | + val output: Any |
| 101 | + ) : Source() |
| 102 | + |
| 103 | + /** |
| 104 | + * Indicates that the update was triggered by an output emitted from a child workflow that was |
| 105 | + * rendered by this workflow. |
| 106 | + * |
| 107 | + * @param key The string key of the child that emitted the output. |
| 108 | + * @param output The value emitted from the child. |
| 109 | + * @param childInfo The [WorkflowUpdateDebugInfo] that describes the child's update. |
| 110 | + */ |
| 111 | + data class Subtree( |
| 112 | + val key: String, |
| 113 | + val output: Any, |
| 114 | + val childInfo: WorkflowUpdateDebugInfo |
| 115 | + ) : Source() |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * This string is expensive to generate, so cache it. |
| 120 | + */ |
| 121 | + private val lazyDescription by lazy(PUBLICATION) { |
| 122 | + buildString { |
| 123 | + writeUpdate(this@WorkflowUpdateDebugInfo) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + override fun toString(): String = """ |
| 128 | + |WorkflowUpdateDebugInfo( |
| 129 | + |${toDescriptionString().trimEnd().prependIndent(" ")} |
| 130 | + |) |
| 131 | + """.trimMargin("|") |
| 132 | + |
| 133 | + /** |
| 134 | + * Generates a multi-line, recursive string describing the update. |
| 135 | + */ |
| 136 | + fun toDescriptionString(): String = lazyDescription |
| 137 | +} |
| 138 | + |
| 139 | +private fun StringBuilder.writeUpdate(update: WorkflowUpdateDebugInfo) { |
| 140 | + append(update.workflowType) |
| 141 | + append(' ') |
| 142 | + |
| 143 | + when (val kind = update.kind) { |
| 144 | + is Updated -> { |
| 145 | + append("updated from ") |
| 146 | + when (val source = kind.source) { |
| 147 | + Sink -> append("sink") |
| 148 | + is Worker -> { |
| 149 | + append("worker[key=") |
| 150 | + append(source.key) |
| 151 | + append("]: ") |
| 152 | + append(source.output) |
| 153 | + } |
| 154 | + is Subtree -> { |
| 155 | + append("workflow[key=") |
| 156 | + append(source.key) |
| 157 | + append("]: ") |
| 158 | + appendln(source.output) |
| 159 | + append("↳ ") |
| 160 | + append(source.childInfo.toDescriptionString()) |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + is Passthrough -> { |
| 165 | + append("passing through from workflow[key=") |
| 166 | + append(kind.key) |
| 167 | + appendln("]") |
| 168 | + append("↳ ") |
| 169 | + append(kind.childInfo.toDescriptionString()) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments