Skip to content

Commit 9f38c52

Browse files
Merge pull request #128 from square/zachklipp/mutator-workflow-action
Pulled the deprecated Mutator and associated method into a sub-interface of WorkflowAction
2 parents 8e0823f + df767c2 commit 9f38c52

File tree

5 files changed

+83
-44
lines changed

5 files changed

+83
-44
lines changed

samples/containers/poetry/src/main/java/com/squareup/sample/poetry/PoemWorkflow.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ import com.squareup.sample.poetry.StanzaWorkflow.Output.ShowNextStanza
2828
import com.squareup.sample.poetry.StanzaWorkflow.Output.ShowPreviousStanza
2929
import com.squareup.sample.poetry.StanzaWorkflow.Props
3030
import com.squareup.sample.poetry.model.Poem
31-
import com.squareup.workflow1.RenderContext
31+
import com.squareup.workflow1.MutatorWorkflowAction
32+
import com.squareup.workflow1.MutatorWorkflowAction.Mutator
3233
import com.squareup.workflow1.Snapshot
3334
import com.squareup.workflow1.StatefulWorkflow
34-
import com.squareup.workflow1.WorkflowAction
3535
import com.squareup.workflow1.WorkflowAction.Companion.noAction
36-
import com.squareup.workflow1.WorkflowAction.Mutator
3736
import com.squareup.workflow1.parse
3837
import com.squareup.workflow1.ui.WorkflowUiExperimentalApi
3938
import com.squareup.workflow1.ui.backstack.BackStackScreen
@@ -107,7 +106,7 @@ object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScree
107106
sink.writeInt(state)
108107
}
109108

110-
private sealed class Action : WorkflowAction<Poem, Int, ClosePoem> {
109+
private sealed class Action : MutatorWorkflowAction<Poem, Int, ClosePoem> {
111110
object ClearSelection : Action()
112111
object SelectPrevious : Action()
113112
object SelectNext : Action()

workflow-core/api/workflow-core.api

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@ public abstract class com/squareup/workflow1/LifecycleWorker : com/squareup/work
4141
public final fun run ()Lkotlinx/coroutines/flow/Flow;
4242
}
4343

44+
public abstract interface class com/squareup/workflow1/MutatorWorkflowAction : com/squareup/workflow1/WorkflowAction {
45+
public abstract fun apply (Lcom/squareup/workflow1/MutatorWorkflowAction$Mutator;)Ljava/lang/Object;
46+
public abstract fun apply (Lcom/squareup/workflow1/WorkflowAction$Updater;)V
47+
}
48+
49+
public final class com/squareup/workflow1/MutatorWorkflowAction$DefaultImpls {
50+
public static fun apply (Lcom/squareup/workflow1/MutatorWorkflowAction;Lcom/squareup/workflow1/WorkflowAction$Updater;)V
51+
}
52+
53+
public final class com/squareup/workflow1/MutatorWorkflowAction$Mutator {
54+
public fun <init> (Ljava/lang/Object;)V
55+
public final fun getState ()Ljava/lang/Object;
56+
public final fun setState (Ljava/lang/Object;)V
57+
}
58+
4459
public abstract interface class com/squareup/workflow1/Sink {
4560
public abstract fun send (Ljava/lang/Object;)V
4661
}
@@ -153,7 +168,6 @@ public final class com/squareup/workflow1/Workflow$Companion {
153168

154169
public abstract interface class com/squareup/workflow1/WorkflowAction {
155170
public static final field Companion Lcom/squareup/workflow1/WorkflowAction$Companion;
156-
public abstract fun apply (Lcom/squareup/workflow1/WorkflowAction$Mutator;)Ljava/lang/Object;
157171
public abstract fun apply (Lcom/squareup/workflow1/WorkflowAction$Updater;)V
158172
}
159173

@@ -169,17 +183,6 @@ public final class com/squareup/workflow1/WorkflowAction$Companion {
169183
public final fun noAction ()Lcom/squareup/workflow1/WorkflowAction;
170184
}
171185

172-
public final class com/squareup/workflow1/WorkflowAction$DefaultImpls {
173-
public static fun apply (Lcom/squareup/workflow1/WorkflowAction;Lcom/squareup/workflow1/WorkflowAction$Mutator;)Ljava/lang/Object;
174-
public static fun apply (Lcom/squareup/workflow1/WorkflowAction;Lcom/squareup/workflow1/WorkflowAction$Updater;)V
175-
}
176-
177-
public final class com/squareup/workflow1/WorkflowAction$Mutator {
178-
public fun <init> (Ljava/lang/Object;)V
179-
public final fun getState ()Ljava/lang/Object;
180-
public final fun setState (Ljava/lang/Object;)V
181-
}
182-
183186
public final class com/squareup/workflow1/WorkflowAction$Updater {
184187
public fun <init> (Ljava/lang/Object;Ljava/lang/Object;)V
185188
public final fun getNextState ()Ljava/lang/Object;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
@file:JvmMultifileClass
17+
@file:JvmName("Workflows")
18+
19+
package com.squareup.workflow1
20+
21+
import com.squareup.workflow1.WorkflowAction.Updater
22+
23+
/**
24+
* An atomic operation that updates the state of a [Workflow], and also optionally emits an output.
25+
*/
26+
@Deprecated("Use WorkflowAction")
27+
@Suppress("DEPRECATION")
28+
interface MutatorWorkflowAction<in PropsT, StateT, out OutputT> :
29+
WorkflowAction<PropsT, StateT, OutputT> {
30+
31+
@Deprecated("Use WorkflowAction.Updater")
32+
class Mutator<S>(var state: S)
33+
34+
@Deprecated("Implement WorkflowAction.apply")
35+
fun Mutator<StateT>.apply(): OutputT?
36+
37+
override fun Updater<PropsT, StateT, OutputT>.apply() {
38+
val mutator = Mutator(state)
39+
mutator.apply()
40+
?.let { setOutput(it) }
41+
state = mutator.state
42+
}
43+
}

workflow-core/src/main/java/com/squareup/workflow1/StatefulWorkflow.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
package com.squareup.workflow1
2121

22+
import com.squareup.workflow1.MutatorWorkflowAction.Mutator
23+
import com.squareup.workflow1.StatefulWorkflow.RenderContext
2224
import com.squareup.workflow1.WorkflowAction.Companion.toString
23-
import com.squareup.workflow1.WorkflowAction.Mutator
2425
import com.squareup.workflow1.WorkflowAction.Updater
2526

2627
/**
@@ -287,11 +288,13 @@ fun <PropsT, StateT, OutputT, RenderingT>
287288
* in [toString].
288289
* @param update Function that defines the workflow update.
289290
*/
291+
/* ktlint-disable parameter-list-wrapping */
290292
fun <PropsT, StateT, OutputT, RenderingT>
291293
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.action(
292-
name: () -> String,
293-
update: Updater<PropsT, StateT, OutputT>.() -> Unit
294-
): WorkflowAction<PropsT, StateT, OutputT> = object : WorkflowAction<PropsT, StateT, OutputT> {
294+
name: () -> String,
295+
update: Updater<PropsT, StateT, OutputT>.() -> Unit
296+
): WorkflowAction<PropsT, StateT, OutputT> = object : WorkflowAction<PropsT, StateT, OutputT> {
297+
/* ktlint-enable parameter-list-wrapping */
295298
override fun Updater<PropsT, StateT, OutputT>.apply() = update.invoke(this)
296299
override fun toString(): String = "action(${name()})-${this@action}"
297300
}
@@ -303,11 +306,13 @@ fun <PropsT, StateT, OutputT, RenderingT>
303306
imports = arrayOf("com.squareup.workflow1.action")
304307
)
305308
)
309+
/* ktlint-disable parameter-list-wrapping */
306310
fun <PropsT, StateT, OutputT, RenderingT>
307311
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.workflowAction(
308-
name: String = "",
309-
block: Mutator<StateT>.() -> OutputT?
310-
) = workflowAction({ name }, block)
312+
name: String = "",
313+
block: Mutator<StateT>.() -> OutputT?
314+
) = workflowAction({ name }, block)
315+
/* ktlint-enable parameter-list-wrapping */
311316

312317
@Suppress("OverridingDeprecatedMember")
313318
@Deprecated(
@@ -317,11 +322,14 @@ fun <PropsT, StateT, OutputT, RenderingT>
317322
imports = arrayOf("com.squareup.workflow1.action")
318323
)
319324
)
325+
/* ktlint-disable parameter-list-wrapping */
320326
fun <PropsT, StateT, OutputT, RenderingT>
321327
StatefulWorkflow<PropsT, StateT, OutputT, RenderingT>.workflowAction(
322-
name: () -> String,
323-
block: Mutator<StateT>.() -> OutputT?
324-
): WorkflowAction<PropsT, StateT, OutputT> = object : WorkflowAction<PropsT, StateT, OutputT> {
325-
override fun Mutator<StateT>.apply() = block.invoke(this)
326-
override fun toString(): String = "workflowAction(${name()})-${this@workflowAction}"
327-
}
328+
name: () -> String,
329+
block: Mutator<StateT>.() -> OutputT?
330+
): WorkflowAction<PropsT, StateT, OutputT> =
331+
/* ktlint-enable parameter-list-wrapping */
332+
object : MutatorWorkflowAction<PropsT, StateT, OutputT> {
333+
override fun Mutator<StateT>.apply() = block.invoke(this)
334+
override fun toString(): String = "workflowAction(${name()})-${this@workflowAction}"
335+
}

workflow-core/src/main/java/com/squareup/workflow1/WorkflowAction.kt

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import com.squareup.workflow1.WorkflowAction.Updater
2525
* An atomic operation that updates the state of a [Workflow], and also optionally emits an output.
2626
*/
2727
interface WorkflowAction<in PropsT, StateT, out OutputT> {
28-
@Deprecated("Use Updater")
29-
class Mutator<S>(var state: S)
3028

3129
/**
3230
* The context for calls to [WorkflowAction.apply]. Allows the action to set the
@@ -61,19 +59,7 @@ interface WorkflowAction<in PropsT, StateT, out OutputT> {
6159
* Executes the logic for this action, including any side effects, updating [state][StateT], and
6260
* setting the [OutputT] to emit.
6361
*/
64-
@Suppress("DEPRECATION")
65-
fun Updater<PropsT, StateT, OutputT>.apply() {
66-
val mutator = Mutator(state)
67-
mutator.apply()
68-
?.let { setOutput(it) }
69-
state = mutator.state
70-
}
71-
72-
@Suppress("DEPRECATION")
73-
@Deprecated("Implement Updater.apply")
74-
fun Mutator<StateT>.apply(): OutputT? {
75-
throw UnsupportedOperationException()
76-
}
62+
fun Updater<PropsT, StateT, OutputT>.apply()
7763

7864
companion object {
7965
/**

0 commit comments

Comments
 (0)