|
| 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.diagnostic |
| 17 | + |
| 18 | +import com.squareup.workflow.VeryExperimentalWorkflow |
| 19 | +import com.squareup.workflow.WorkflowAction |
| 20 | +import kotlinx.coroutines.CoroutineScope |
| 21 | + |
| 22 | +/** |
| 23 | + * Returns a [WorkflowDiagnosticListener] that will delegate all method calls first to this |
| 24 | + * instance, and then to [next]. |
| 25 | + */ |
| 26 | +fun WorkflowDiagnosticListener.andThen( |
| 27 | + next: WorkflowDiagnosticListener |
| 28 | +): WorkflowDiagnosticListener { |
| 29 | + return (this as? ChainedDiagnosticListener |
| 30 | + ?: ChainedDiagnosticListener(this)) |
| 31 | + .apply { addVisitor(next) } |
| 32 | +} |
| 33 | + |
| 34 | +@UseExperimental(VeryExperimentalWorkflow::class) |
| 35 | +@Suppress("TooManyFunctions") |
| 36 | +internal class ChainedDiagnosticListener( |
| 37 | + listener: WorkflowDiagnosticListener |
| 38 | +) : WorkflowDiagnosticListener { |
| 39 | + |
| 40 | + private val visitors = mutableListOf(listener) |
| 41 | + |
| 42 | + fun addVisitor(listener: WorkflowDiagnosticListener) { |
| 43 | + if (listener is ChainedDiagnosticListener) { |
| 44 | + visitors.addAll(listener.visitors) |
| 45 | + } else { |
| 46 | + visitors += listener |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + override fun onBeforeRenderPass(props: Any?) { |
| 51 | + visitors.forEach { it.onBeforeRenderPass(props) } |
| 52 | + } |
| 53 | + |
| 54 | + override fun onPropsChanged( |
| 55 | + workflowId: Long?, |
| 56 | + oldProps: Any?, |
| 57 | + newProps: Any?, |
| 58 | + oldState: Any?, |
| 59 | + newState: Any? |
| 60 | + ) { |
| 61 | + visitors.forEach { it.onPropsChanged(workflowId, oldProps, newProps, oldState, newState) } |
| 62 | + } |
| 63 | + |
| 64 | + override fun onBeforeWorkflowRendered( |
| 65 | + workflowId: Long, |
| 66 | + props: Any?, |
| 67 | + state: Any? |
| 68 | + ) { |
| 69 | + visitors.forEach { it.onBeforeWorkflowRendered(workflowId, props, state) } |
| 70 | + } |
| 71 | + |
| 72 | + override fun onAfterWorkflowRendered( |
| 73 | + workflowId: Long, |
| 74 | + rendering: Any? |
| 75 | + ) { |
| 76 | + visitors.forEach { it.onAfterWorkflowRendered(workflowId, rendering) } |
| 77 | + } |
| 78 | + |
| 79 | + override fun onAfterRenderPass(rendering: Any?) { |
| 80 | + visitors.forEach { it.onAfterRenderPass(rendering) } |
| 81 | + } |
| 82 | + |
| 83 | + override fun onBeforeSnapshotPass() { |
| 84 | + visitors.forEach { it.onBeforeSnapshotPass() } |
| 85 | + } |
| 86 | + |
| 87 | + override fun onAfterSnapshotPass() { |
| 88 | + visitors.forEach { it.onAfterSnapshotPass() } |
| 89 | + } |
| 90 | + |
| 91 | + override fun onRuntimeStarted(workflowScope: CoroutineScope) { |
| 92 | + visitors.forEach { it.onRuntimeStarted(workflowScope) } |
| 93 | + } |
| 94 | + |
| 95 | + override fun onRuntimeStopped() { |
| 96 | + visitors.forEach { it.onRuntimeStopped() } |
| 97 | + } |
| 98 | + |
| 99 | + override fun onWorkflowStarted( |
| 100 | + workflowId: Long, |
| 101 | + parentId: Long?, |
| 102 | + workflowType: String, |
| 103 | + key: String, |
| 104 | + initialProps: Any?, |
| 105 | + initialState: Any?, |
| 106 | + restoredFromSnapshot: Boolean |
| 107 | + ) { |
| 108 | + visitors.forEach { |
| 109 | + it.onWorkflowStarted( |
| 110 | + workflowId, parentId, workflowType, key, initialProps, initialState, restoredFromSnapshot |
| 111 | + ) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + override fun onWorkflowStopped(workflowId: Long) { |
| 116 | + visitors.forEach { it.onWorkflowStopped(workflowId) } |
| 117 | + } |
| 118 | + |
| 119 | + override fun onWorkerStarted( |
| 120 | + workerId: Long, |
| 121 | + parentWorkflowId: Long, |
| 122 | + key: String, |
| 123 | + description: String |
| 124 | + ) { |
| 125 | + visitors.forEach { it.onWorkerStarted(workerId, parentWorkflowId, key, description) } |
| 126 | + } |
| 127 | + |
| 128 | + override fun onWorkerStopped( |
| 129 | + workerId: Long, |
| 130 | + parentWorkflowId: Long |
| 131 | + ) { |
| 132 | + visitors.forEach { it.onWorkerStopped(workerId, parentWorkflowId) } |
| 133 | + } |
| 134 | + |
| 135 | + override fun onWorkerOutput( |
| 136 | + workerId: Long, |
| 137 | + parentWorkflowId: Long, |
| 138 | + output: Any |
| 139 | + ) { |
| 140 | + visitors.forEach { it.onWorkerOutput(workerId, parentWorkflowId, output) } |
| 141 | + } |
| 142 | + |
| 143 | + override fun onSinkReceived( |
| 144 | + workflowId: Long, |
| 145 | + action: WorkflowAction<*, *> |
| 146 | + ) { |
| 147 | + visitors.forEach { it.onSinkReceived(workflowId, action) } |
| 148 | + } |
| 149 | + |
| 150 | + override fun onWorkflowAction( |
| 151 | + workflowId: Long, |
| 152 | + action: WorkflowAction<*, *>, |
| 153 | + oldState: Any?, |
| 154 | + newState: Any?, |
| 155 | + output: Any? |
| 156 | + ) { |
| 157 | + visitors.forEach { it.onWorkflowAction(workflowId, action, oldState, newState, output) } |
| 158 | + } |
| 159 | +} |
0 commit comments