Skip to content

Gives WorkflowAction access to props. #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object PoemListWorkflow : StatelessWorkflow<List<Poem>, Int, PoemListRendering>(

override fun render(
props: List<Poem>,
context: RenderContext<Nothing, Int>
context: RenderContext<List<Poem>, Nothing, Int>
): PoemListRendering {
// A sink that emits the given index as the result of this workflow.
val sink = context.makeEventSink { index: Int -> setOutput(index) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object PoemsBrowserWorkflow :
override fun render(
props: List<Poem>,
state: SelectedPoem,
context: RenderContext<SelectedPoem, Nothing>
context: RenderContext<List<Poem>, SelectedPoem, Nothing>
): OverviewDetailScreen {
val poems: OverviewDetailScreen =
context.renderChild(PoemListWorkflow, props) { selected -> choosePoem(selected) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ object AreYouSureWorkflow : StatefulWorkflow<Unit, State, Finished, AlertContain
override fun render(
props: Unit,
state: State,
context: RenderContext<State, Finished>
context: RenderContext<Unit, State, Finished>
): AlertContainerScreen<*> {
val ableBakerCharlie = context.renderChild(HelloBackButtonWorkflow, Unit) { noAction() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object HelloBackButtonWorkflow : StatefulWorkflow<Unit, State, Nothing, Renderin
override fun render(
props: Unit,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<Unit, State, Nothing>
): Rendering {
return Rendering(
message = "$state",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScree
override fun render(
props: Poem,
state: Int,
context: RenderContext<Int, ClosePoem>
context: RenderContext<Poem, Int, ClosePoem>
): OverviewDetailScreen {
val previousStanzas: List<StanzaRendering> =
if (state == -1) emptyList()
Expand Down Expand Up @@ -105,7 +105,7 @@ object PoemWorkflow : StatefulWorkflow<Poem, Int, ClosePoem, OverviewDetailScree
sink.writeInt(state)
}

private sealed class Action : WorkflowAction<Int, ClosePoem> {
private sealed class Action : WorkflowAction<Poem, Int, ClosePoem> {
object ClearSelection : Action()
object SelectPrevious : Action()
object SelectNext : Action()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ object StanzaListWorkflow : StatelessWorkflow<Poem, Int, StanzaListRendering>()

override fun render(
props: Poem,
context: RenderContext<Nothing, Int>
context: RenderContext<Poem, Nothing, Int>
): StanzaListRendering {
// A sink that emits the given index as the result of this workflow.
val sink = context.makeEventSink { index: Int -> setOutput(index) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object StanzaWorkflow : StatelessWorkflow<Props, Output, StanzaRendering>() {

override fun render(
props: Props,
context: RenderContext<Nothing, Output>
context: RenderContext<Props, Nothing, Output>
): StanzaRendering {
with(props) {
val sink: Sink<Output> = context.makeEventSink { setOutput(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DungeonAppWorkflow(
override fun render(
props: Props,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<Props, State, Nothing>
): AlertContainerScreen<Any> = when (state) {

LoadingBoardList -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class GameSessionWorkflow(
override fun render(
props: Props,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<Props, State, Nothing>
): AlertContainerScreen<Any> = when (state) {

Loading -> {
Expand Down Expand Up @@ -98,8 +98,8 @@ class GameSessionWorkflow(

override fun snapshotState(state: State): Snapshot = Snapshot.EMPTY

private class StartRunning(val board: Board) : WorkflowAction<State, Nothing> {
override fun Updater<State, Nothing>.apply() {
private class StartRunning(val board: Board) : WorkflowAction<Props, State, Nothing> {
override fun Updater<Props, State, Nothing>.apply() {
state = Running(board)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TimeMachineAppWorkflow(

override fun render(
props: BoardPath,
context: RenderContext<Nothing, Nothing>
context: RenderContext<BoardPath, Nothing, Nothing>
): ShakeableTimeMachineRendering {
val propsFactory = PropsFactory { recording ->
Props(paused = !recording)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class AiWorkflow(
override fun render(
props: ActorProps,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<ActorProps, State, Nothing>
): ActorRendering {
context.runningWorker(state.directionTicker) { updateDirection }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class GameWorkflow(
override fun render(
props: Props,
state: State,
context: RenderContext<State, Output>
context: RenderContext<Props, State, Output>
): GameRendering {
val running = !props.paused && !state.game.isPlayerEaten
// Stop actors from ticking if the game is paused or finished.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ class PlayerWorkflow(
private val cellsPerSecond: Float = 15f
) : StatefulWorkflow<ActorProps, Movement, Nothing, Rendering>() {

sealed class Action : WorkflowAction<Movement, Nothing> {
sealed class Action : WorkflowAction<ActorProps, Movement, Nothing> {

class StartMoving(private val direction: Direction) : Action() {
override fun Updater<Movement, Nothing>.apply() {
override fun Updater<ActorProps, Movement, Nothing>.apply() {
state += direction
}
}

class StopMoving(private val direction: Direction) : Action() {
override fun Updater<Movement, Nothing>.apply() {
override fun Updater<ActorProps, Movement, Nothing>.apply() {
state -= direction
}
}
Expand All @@ -64,7 +64,7 @@ class PlayerWorkflow(
override fun render(
props: ActorProps,
state: Movement,
context: RenderContext<Movement, Nothing>
context: RenderContext<ActorProps, Movement, Nothing>
): Rendering = Rendering(
actorRendering = ActorRendering(avatar = avatar, movement = state),
onStartMoving = { context.actionSink.send(StartMoving(it)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ShakeableTimeMachineWorkflow<in P, O : Any, out R : Any>(
override fun render(
props: PropsFactory<P>,
state: State,
context: RenderContext<State, O>
context: RenderContext<PropsFactory<P>, State, O>
): ShakeableTimeMachineRendering {
// Only listen to shakes when recording.
if (state === Recording) context.runningWorker(shakeWorker) { onShake }
Expand Down Expand Up @@ -123,14 +123,14 @@ class ShakeableTimeMachineWorkflow<in P, O : Any, out R : Any>(

private inner class SeekAction(
private val newPosition: Duration
) : WorkflowAction<State, O> {
override fun Updater<State, O>.apply() {
) : WorkflowAction<PropsFactory<P>, State, O> {
override fun Updater<PropsFactory<P>, State, O>.apply() {
state = PlayingBack(newPosition)
}
}

private inner class ResumeRecordingAction : WorkflowAction<State, O> {
override fun Updater<State, O>.apply() {
private inner class ResumeRecordingAction : WorkflowAction<PropsFactory<P>, State, O> {
override fun Updater<PropsFactory<P>, State, O>.apply() {
state = Recording
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ internal class RecorderWorkflow<T>(
override fun render(
props: RecorderProps<T>,
state: Recording<T>,
context: RenderContext<Recording<T>, Nothing>
context: RenderContext<RecorderProps<T>, Recording<T>, Nothing>
): TimeMachineRendering<T> {
val value = when (props) {
is RecordValue -> props.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import kotlin.time.TimeSource
*
* @param delegateWorkflow The [Workflow] whose renderings to record. This workflow will be rendered
* continuously as long as the `TimeMachineWorkflow` is being rendered.
* @param clock The [Clock] to use to assign timestamps to recorded values.
* @param clock The [TimeSource] to use to assign timestamps to recorded values.
*/
@ExperimentalTime
class TimeMachineWorkflow<P, O : Any, out R>(
Expand Down Expand Up @@ -84,7 +84,7 @@ class TimeMachineWorkflow<P, O : Any, out R>(

override fun render(
props: TimeMachineProps<P>,
context: RenderContext<Nothing, O>
context: RenderContext<TimeMachineProps<P>, Nothing, O>
): TimeMachineRendering<R> {
// Always render the delegate, even if in playback mode, to keep it alive.
val delegateRendering =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BlinkingCursorWorkflow(
override fun render(
props: Unit,
state: Boolean,
context: RenderContext<Boolean, Nothing>
context: RenderContext<Unit, Boolean, Nothing>
): String {
context.runningWorker(intervalWorker) { setCursorShowing(it) }
return if (state) cursorString else ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import com.squareup.workflow.WorkflowAction
import com.squareup.workflow.action
import com.squareup.workflow.renderChild

private typealias HelloTerminalAction = WorkflowAction<State, ExitCode>
private typealias HelloTerminalAction = WorkflowAction<TerminalProps, State, ExitCode>

class HelloTerminalWorkflow : TerminalWorkflow,
StatefulWorkflow<TerminalProps, State, ExitCode, TerminalRendering>() {
Expand All @@ -52,7 +52,7 @@ class HelloTerminalWorkflow : TerminalWorkflow,
override fun render(
props: TerminalProps,
state: State,
context: RenderContext<State, ExitCode>
context: RenderContext<TerminalProps, State, ExitCode>
): TerminalRendering {
val (rows, columns) = props.size
val header = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class EditTextWorkflow : StatefulWorkflow<EditTextProps, EditTextState, String,
override fun render(
props: EditTextProps,
state: EditTextState,
context: RenderContext<EditTextState, String>
context: RenderContext<EditTextProps, EditTextState, String>
): String {
context.runningWorker(props.terminalProps.keyStrokes) { key -> onKeystroke(props, key) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import com.squareup.workflow.StatefulWorkflow
import com.squareup.workflow.WorkflowAction
import com.squareup.workflow.action

private typealias TodoAction = WorkflowAction<TodoList, Nothing>
private typealias TodoAction = WorkflowAction<TerminalProps, TodoList, Nothing>

class TodoWorkflow : TerminalWorkflow,
StatefulWorkflow<TerminalProps, TodoList, ExitCode, TerminalRendering>() {
Expand Down Expand Up @@ -75,14 +75,14 @@ class TodoWorkflow : TerminalWorkflow,
override fun render(
props: TerminalProps,
state: TodoList,
context: RenderContext<TodoList, ExitCode>
context: RenderContext<TerminalProps, TodoList, ExitCode>
): TerminalRendering {

context.runningWorker(props.keyStrokes) { onKeystroke(it) }

return TerminalRendering(buildString {
@Suppress("UNCHECKED_CAST")
appendln(state.renderTitle(props, context as RenderContext<TodoList, Nothing>))
appendln(state.renderTitle(props, context as RenderContext<TerminalProps, TodoList, Nothing>))
appendln(renderSelection(state.titleSeparator, false))
appendln(state.renderItems(props, context))
})
Expand Down Expand Up @@ -117,7 +117,7 @@ private fun setLabel(

private fun TodoList.renderTitle(
props: TerminalProps,
context: RenderContext<TodoList, Nothing>
context: RenderContext<TerminalProps, TodoList, Nothing>
): String {
val isSelected = focusedField == TITLE_FIELD_INDEX
val titleString = if (isSelected) {
Expand All @@ -136,7 +136,7 @@ private val TodoList.titleSeparator get() = "–".repeat(title.length + 1)

private fun TodoList.renderItems(
props: TerminalProps,
context: RenderContext<TodoList, Nothing>
context: RenderContext<TerminalProps, TodoList, Nothing>
): String =
items
.mapIndexed { index, item ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
override fun render(
props: Unit,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<Unit, State, Nothing>
): Rendering {
return Rendering(
message = state.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object HelloWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
override fun render(
props: Unit,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<Unit, State, Nothing>
): Rendering {
return Rendering(
message = state.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ object AppWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
get() = listOfNotNull(popup)
}

private sealed class Action : WorkflowAction<State, Nothing> {
private sealed class Action : WorkflowAction<Unit, State, Nothing> {
object ShowAddRowAction : Action()
data class CommitNewRowAction(val index: Int) : Action()

override fun Updater<State, Nothing>.apply() {
override fun Updater<Unit, State, Nothing>.apply() {
state = when (this@Action) {
ShowAddRowAction -> ChooseNewRow(state.rows)
is CommitNewRowAction -> ShowList(state.rows + allRowTypes[index])
Expand All @@ -94,7 +94,7 @@ object AppWorkflow : StatefulWorkflow<Unit, State, Nothing, Rendering>() {
override fun render(
props: Unit,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<Unit, State, Nothing>
): Rendering {
val listRendering = context.renderChild(EditableListWorkflow, Props(state.rows))
val baseScreen = BaseScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object EditableListWorkflow : StatefulWorkflow<Props, State, Nothing, Rendering>
override fun render(
props: Props,
state: State,
context: RenderContext<State, Nothing>
context: RenderContext<Props, State, Nothing>
): Rendering {
return Rendering(
rowValues = state.rowValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ sealed class AuthResult {
object Canceled : AuthResult()
}

internal sealed class Action : WorkflowAction<AuthState, AuthResult> {
internal sealed class Action : WorkflowAction<Unit, AuthState, AuthResult> {
class SubmitLogin(
val email: String,
val password: String
Expand All @@ -91,7 +91,7 @@ internal sealed class Action : WorkflowAction<AuthState, AuthResult> {
val response: AuthResponse
) : Action()

final override fun Updater<AuthState, AuthResult>.apply() {
final override fun Updater<Unit, AuthState, AuthResult>.apply() {
when (this@Action) {
is SubmitLogin -> {
state = when {
Expand Down Expand Up @@ -146,7 +146,7 @@ class RealAuthWorkflow(private val authService: AuthService) : AuthWorkflow,
override fun render(
props: Unit,
state: AuthState,
context: RenderContext<AuthState, AuthResult>
context: RenderContext<Unit, AuthState, AuthResult>
): BackStackScreen<Any> = when (state) {
is LoginPrompt -> {
BackStackScreen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ typealias RunGameScreen = AlertContainerScreen<PanelContainerScreen<Any, Any>>
*/
typealias RunGameWorkflow = Workflow<Unit, RunGameResult, RunGameScreen>

sealed class Action : WorkflowAction<RunGameState, RunGameResult> {
sealed class Action : WorkflowAction<Unit, RunGameState, RunGameResult> {
object CancelNewGame : Action()

class StartGame(
Expand Down Expand Up @@ -99,7 +99,7 @@ sealed class Action : WorkflowAction<RunGameState, RunGameResult> {
// signal that this workflow is too big, and should be refactored into something
// like one workflow per screen.

override fun Updater<RunGameState, RunGameResult>.apply() {
override fun Updater<Unit, RunGameState, RunGameResult>.apply() {

when (this@Action) {
CancelNewGame -> setOutput(CanceledStart)
Expand Down Expand Up @@ -178,7 +178,7 @@ class RealRunGameWorkflow(
override fun render(
props: Unit,
state: RunGameState,
context: RenderContext<RunGameState, RunGameResult>
context: RenderContext<Unit, RunGameState, RunGameResult>
): RunGameScreen = when (state) {
is NewGame -> {
val emptyGameScreen = GamePlayScreen()
Expand Down
Loading