Skip to content

Commit 79337da

Browse files
authored
Merge pull request #324 from forrestbice/bice/this-state-and-props
Cleanup `this.state`
2 parents d8a6465 + 201c632 commit 79337da

File tree

10 files changed

+49
-50
lines changed

10 files changed

+49
-50
lines changed

samples/containers/hello-back-button/src/main/java/com/squareup/sample/hellobackbutton/HelloBackButtonWorkflow.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ object HelloBackButtonWorkflow : StatefulWorkflow<
3838
return HelloBackButtonRendering(
3939
message = "$renderState",
4040
onClick = context.eventHandler {
41-
this.state = when (this.state) {
41+
state = when (state) {
4242
Able -> Baker
4343
Baker -> Charlie
4444
Charlie -> Able
4545
}
4646
},
4747
onBackPressed = if (renderState == Able) null else context.eventHandler {
48-
this.state = when (this.state) {
48+
state = when (state) {
4949
Able -> throw IllegalStateException()
5050
Baker -> Able
5151
Charlie -> Baker

samples/dungeon/common/src/main/java/com/squareup/sample/dungeon/GameWorkflow.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class GameWorkflow(
114114
if (running) {
115115
context.runningWorker(ticker) { tick ->
116116
return@runningWorker updateGame(
117-
renderProps.ticksPerSecond, tick, game, playerRendering, board, aiRenderings
117+
renderProps.ticksPerSecond, tick, playerRendering, aiRenderings
118118
)
119119
}
120120
}
@@ -139,9 +139,7 @@ class GameWorkflow(
139139
private fun updateGame(
140140
ticksPerSecond: Int,
141141
tick: Long,
142-
game: Game,
143142
playerRendering: Rendering,
144-
board: Board,
145143
aiRenderings: List<Pair<Location, ActorRendering>>
146144
) = action("updateGame") {
147145
// Calculate if this tick should result in movement based on the movement's speed.
@@ -152,25 +150,27 @@ class GameWorkflow(
152150

153151
// Execute player movement.
154152
var output: Output? = null
155-
var newPlayerLocation: Location = game.playerLocation
153+
var newPlayerLocation: Location = state.game.playerLocation
156154
if (playerRendering.actorRendering.movement.isTimeToMove()) {
157-
val moveResult = game.playerLocation.move(playerRendering.actorRendering.movement, board)
155+
val moveResult = state.game.playerLocation.move(
156+
playerRendering.actorRendering.movement, props.board
157+
)
158158
newPlayerLocation = moveResult.newLocation
159159
if (moveResult.collisionDetected) output = Vibrate
160160
}
161161

162162
// Execute AI movement.
163163
val newAiLocations = aiRenderings.map { (location, rendering) ->
164164
return@map if (rendering.movement.isTimeToMove()) {
165-
location.move(rendering.movement, board)
165+
location.move(rendering.movement, props.board)
166166
// Don't care about collisions.
167167
.newLocation
168168
} else {
169169
location
170170
}
171171
}
172172

173-
val newGame = game.copy(
173+
val newGame = state.game.copy(
174174
playerLocation = newPlayerLocation,
175175
aiLocations = newAiLocations
176176
)

samples/dungeon/timemachine/src/test/java/com/squareup/sample/timemachine/TimeMachineWorkflowTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class TimeMachineWorkflowTest {
2424

2525
val delegateWorkflow = Workflow.stateful<String, Nothing, DelegateRendering>(
2626
initialState = "initial",
27-
render = { state ->
28-
DelegateRendering(state, setState = eventHandler { s -> this.state = s })
27+
render = { renderState ->
28+
DelegateRendering(renderState, setState = eventHandler { s -> state = s })
2929
}
3030
)
3131
val clock = TestTimeSource()

samples/hello-terminal/todo-terminal-app/src/main/java/com/squareup/sample/hellotodo/EditTextWorkflow.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class EditTextWorkflow : StatefulWorkflow<EditTextProps, EditTextState, String,
5151
): String {
5252
context.runningWorker(
5353
renderProps.terminalProps.keyStrokes
54-
) { key -> onKeystroke(renderProps, key) }
54+
) { key -> onKeystroke(key) }
5555

5656
return buildString {
5757
renderProps.text.forEachIndexed { index, c ->
@@ -64,7 +64,6 @@ class EditTextWorkflow : StatefulWorkflow<EditTextProps, EditTextState, String,
6464
override fun snapshotState(state: EditTextState): Snapshot? = null
6565

6666
private fun onKeystroke(
67-
props: EditTextProps,
6867
key: KeyStroke
6968
) = action {
7069
when (key.keyType) {

samples/tictactoe/common/src/main/java/com/squareup/sample/authworkflow/AuthWorkflow.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class RealAuthWorkflow(private val authService: AuthService) : AuthWorkflow,
7878
LoginScreen(
7979
renderState.errorMessage,
8080
onLogin = context.eventHandler { email, password ->
81-
this.state = when {
81+
state = when {
8282
email.isValidEmail -> Authorizing(email, password)
8383
else -> LoginPrompt(email.emailValidationErrorMessage)
8484
}
@@ -106,11 +106,11 @@ class RealAuthWorkflow(private val authService: AuthService) : AuthWorkflow,
106106
SecondFactorScreen(
107107
renderState.errorMessage,
108108
onSubmit = context.eventHandler { secondFactor ->
109-
(this.state as? SecondFactorPrompt)?.let { oldState ->
110-
this.state = AuthorizingSecondFactor(oldState.tempToken, secondFactor)
109+
(state as? SecondFactorPrompt)?.let { oldState ->
110+
state = AuthorizingSecondFactor(oldState.tempToken, secondFactor)
111111
}
112112
},
113-
onCancel = context.eventHandler { this.state = LoginPrompt() }
113+
onCancel = context.eventHandler { state = LoginPrompt() }
114114
)
115115
)
116116
}

samples/tictactoe/common/src/main/java/com/squareup/sample/gameworkflow/RunGameWorkflow.kt

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class RealRunGameWorkflow(
7979
renderState.defaultXName,
8080
renderState.defaultOName,
8181
onCancel = context.eventHandler { setOutput(CanceledStart) },
82-
onStartGame = context.eventHandler { x, o -> this.state = Playing(PlayerInfo(x, o)) }
82+
onStartGame = context.eventHandler { x, o -> state = Playing(PlayerInfo(x, o)) }
8383
)
8484
)
8585
}
@@ -103,13 +103,13 @@ class RealRunGameWorkflow(
103103
base = GamePlayScreen(renderState.playerInfo, renderState.completedGame.lastTurn),
104104
alert = maybeQuitScreen(
105105
confirmQuit = context.eventHandler {
106-
(this.state as? MaybeQuitting)?.let { oldState ->
107-
this.state = MaybeQuittingForSure(oldState.playerInfo, oldState.completedGame)
106+
(state as? MaybeQuitting)?.let { oldState ->
107+
state = MaybeQuittingForSure(oldState.playerInfo, oldState.completedGame)
108108
}
109109
},
110110
continuePlaying = context.eventHandler {
111-
(this.state as? MaybeQuitting)?.let { oldState ->
112-
this.state = Playing(oldState.playerInfo, oldState.completedGame.lastTurn)
111+
(state as? MaybeQuitting)?.let { oldState ->
112+
state = Playing(oldState.playerInfo, oldState.completedGame.lastTurn)
113113
}
114114
}
115115
)
@@ -125,13 +125,13 @@ class RealRunGameWorkflow(
125125
positive = "Yes!!",
126126
negative = "Sigh, no",
127127
confirmQuit = context.eventHandler {
128-
(this.state as? MaybeQuittingForSure)?.let { oldState ->
129-
this.state = GameOver(oldState.playerInfo, oldState.completedGame)
128+
(state as? MaybeQuittingForSure)?.let { oldState ->
129+
state = GameOver(oldState.playerInfo, oldState.completedGame)
130130
}
131131
},
132132
continuePlaying = context.eventHandler {
133-
(this.state as? MaybeQuittingForSure)?.let { oldState ->
134-
this.state = Playing(oldState.playerInfo, oldState.completedGame.lastTurn)
133+
(state as? MaybeQuittingForSure)?.let { oldState ->
134+
state = Playing(oldState.playerInfo, oldState.completedGame.lastTurn)
135135
}
136136
}
137137
)
@@ -171,14 +171,14 @@ class RealRunGameWorkflow(
171171
}
172172

173173
private fun RenderContext.playAgain() = eventHandler {
174-
(this.state as? GameOver)?.let { oldState ->
174+
(state as? GameOver)?.let { oldState ->
175175
val (x, o) = oldState.playerInfo
176176
state = NewGame(x, o)
177177
}
178178
}
179179

180180
private fun RenderContext.trySaveAgain() = eventHandler {
181-
(this.state as? GameOver)?.let { oldState ->
181+
(state as? GameOver)?.let { oldState ->
182182
check(oldState.syncState == SAVE_FAILED) {
183183
"Should only fire trySaveAgain in syncState $SAVE_FAILED, " +
184184
"was ${oldState.syncState}"

samples/tutorial/Tutorial1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ The `action` function is a shorthand for implementing the `WorkflowAction` class
186186
private fun onUsernameChanged(username: String) =
187187
object : WorkflowAction<Unit, State, Nothing>() {
188188
override fun Updater.apply() {
189-
this.state = this.state.copy(username = username)
189+
state = state.copy(username = username)
190190
}
191191
}
192192
```

workflow-runtime/src/test/java/com/squareup/workflow1/RenderWorkflowInTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ class RenderWorkflowInTest {
128128
snapshot = { state ->
129129
Snapshot.write { it.writeUtf8WithLength(state) }
130130
},
131-
render = { _, state ->
131+
render = { _, renderState ->
132132
Pair(
133-
state,
134-
{ newState -> actionSink.send(action { this.state = newState }) }
133+
renderState,
134+
{ newState -> actionSink.send(action { state = newState }) }
135135
)
136136
}
137137
)
@@ -171,9 +171,9 @@ class RenderWorkflowInTest {
171171
ByteString.of(1)
172172
}
173173
},
174-
render = { _, state ->
175-
sink = actionSink.contraMap { action { this.state = it } }
176-
state
174+
render = { _, renderState ->
175+
sink = actionSink.contraMap { action { state = it } }
176+
renderState
177177
}
178178
)
179179
val props = MutableStateFlow(Unit)
@@ -464,14 +464,14 @@ class RenderWorkflowInTest {
464464
// A workflow whose state and rendering is the last output that it emitted.
465465
val workflow = Workflow.stateful<Unit, String, String, String>(
466466
initialState = { "{no output}" },
467-
render = { _, state ->
467+
render = { _, renderState ->
468468
runningWorker(Worker.from { outputTrigger.await() }) { output ->
469469
action {
470470
setOutput(output)
471-
this.state = output
471+
state = output
472472
}
473473
}
474-
return@stateful state
474+
return@stateful renderState
475475
}
476476
)
477477
val events = mutableListOf<String>()

workflow-runtime/src/test/java/com/squareup/workflow1/internal/WorkflowNodeTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,9 +1083,9 @@ class WorkflowNodeTest {
10831083
@Test fun `actionSink action changes state`() {
10841084
val workflow = Workflow.stateful<Unit, String, Nothing, Pair<String, Sink<String>>>(
10851085
initialState = { "initial" },
1086-
render = { _, state ->
1087-
state to actionSink.contraMap {
1088-
action { this.state = "${this.state}->$it" }
1086+
render = { _, renderState ->
1087+
renderState to actionSink.contraMap {
1088+
action { state = "$state->$it" }
10891089
}
10901090
}
10911091
)
@@ -1163,11 +1163,11 @@ class WorkflowNodeTest {
11631163
@Test fun `child action changes state`() {
11641164
val workflow = Workflow.stateful<Unit, String, Nothing, String>(
11651165
initialState = { "initial" },
1166-
render = { _, state ->
1166+
render = { _, renderState ->
11671167
runningSideEffect("test") {
1168-
actionSink.send(action { this.state = "${this.state}->hello" })
1168+
actionSink.send(action { state = "$state->hello" })
11691169
}
1170-
return@stateful state
1170+
return@stateful renderState
11711171
}
11721172
)
11731173
val node = WorkflowNode(

workflow-runtime/src/test/java/com/squareup/workflow1/internal/WorkflowRunnerTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ class WorkflowRunnerTest {
9292
@Test fun `nextOutput() handles workflow update`() {
9393
val workflow = Workflow.stateful<Unit, String, String, String>(
9494
initialState = { "initial" },
95-
render = { _, state ->
95+
render = { _, renderState ->
9696
runningWorker(Worker.from { "work" }) {
9797
action {
98-
this.state = "state: $it"
98+
state = "state: $it"
9999
setOutput("output: $it")
100100
}
101101
}
102-
return@stateful state
102+
return@stateful renderState
103103
}
104104
)
105105
val runner = WorkflowRunner(workflow, MutableStateFlow(Unit))
@@ -119,14 +119,14 @@ class WorkflowRunnerTest {
119119
@Test fun `nextOutput() handles concurrent props change and workflow update`() {
120120
val workflow = Workflow.stateful<String, String, String, String>(
121121
initialState = { "initial state($it)" },
122-
render = { props, state ->
122+
render = { renderProps, renderState ->
123123
runningWorker(Worker.from { "work" }) {
124124
action {
125-
this.state = "state: $it"
125+
state = "state: $it"
126126
setOutput("output: $it")
127127
}
128128
}
129-
return@stateful "$props|$state"
129+
return@stateful "$renderProps|$renderState"
130130
}
131131
)
132132
val props = MutableStateFlow("initial props")

0 commit comments

Comments
 (0)