Skip to content

Rename WorkflowContext.compose to composeChild. #274

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
Apr 13, 2019
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 @@ -106,10 +106,10 @@ class RealRunGameWorkflow(
}

is Playing -> {
// context.compose starts takeTurnsWorkflow, or keeps it running if it was
// context.composeChild starts takeTurnsWorkflow, or keeps it running if it was
// already going. TakeTurnsWorkflow.compose is immediately called,
// and the GamePlayScreen it renders is immediately returned.
val takeTurnsScreen = context.compose(takeTurnsWorkflow, state.playerInfo) { output ->
val takeTurnsScreen = context.composeChild(takeTurnsWorkflow, state.playerInfo) { output ->
when (output.ending) {
Quitted -> enterState(MaybeQuitting(output, state.playerInfo))
else -> enterState(GameOver(state.playerInfo, output))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import com.squareup.workflow.StatefulWorkflow
import com.squareup.workflow.Workflow
import com.squareup.workflow.WorkflowAction.Companion.enterState
import com.squareup.workflow.WorkflowContext
import com.squareup.workflow.compose
import com.squareup.workflow.composeChild

/**
* Application specific root [Workflow], and demonstration of workflow composition.
Expand Down Expand Up @@ -59,13 +59,13 @@ class MainWorkflow(
context: WorkflowContext<MainState, Unit>
): RunGameScreen = when (state) {
is Authenticating -> {
val authScreen = context.compose(authWorkflow) { enterState(RunningGame) }
val authScreen = context.composeChild(authWorkflow) { enterState(RunningGame) }
val emptyGameScreen = GamePlayScreen()

AlertContainerScreen(authScreen.asPanelOver(emptyGameScreen))
}

is RunningGame -> context.compose(runGameWorkflow) { enterState(Authenticating) }
is RunningGame -> context.composeChild(runGameWorkflow) { enterState(Authenticating) }
}

override fun snapshotState(state: MainState): Snapshot = state.toSnapshot()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.squareup.workflow

/**
* A composable, stateful object that can [handle events][WorkflowContext.onEvent],
* [delegate to children][WorkflowContext.compose], [subscribe][onReceive] to arbitrary streams from
* [delegate to children][WorkflowContext.composeChild], [subscribe][onReceive] to arbitrary streams from
* the outside world, and be [saved][snapshotState] to a serialized form to be restored later.
*
* The basic purpose of a `Workflow` is to take some [input][InputT] and return a
Expand Down Expand Up @@ -63,7 +63,7 @@ abstract class StatefulWorkflow<
> : Workflow<InputT, OutputT, RenderingT> {

/**
* Called from [WorkflowContext.compose] when the state machine is first started, to get the
* Called from [WorkflowContext.composeChild] when the state machine is first started, to get the
* initial state.
*
* @param snapshot
Expand All @@ -78,7 +78,7 @@ abstract class StatefulWorkflow<
): StateT

/**
* Called from [WorkflowContext.compose] instead of [initialState] when the workflow is already
* Called from [WorkflowContext.composeChild] instead of [initialState] when the workflow is already
* running. This allows the workflow to detect changes in input, and possibly change its state in
* response. This method is called eagerly: `old` and `new` might be the same value, so it is up
* to implementing code to perform any diffing if desired.
Expand All @@ -100,7 +100,7 @@ abstract class StatefulWorkflow<
* - Emits an output.
*
* **Never call this method directly.** To nest the rendering of a child workflow in your own,
* pass the child and any required input to [WorkflowContext.compose].
* pass the child and any required input to [WorkflowContext.composeChild].
*
* This method *should not* have any side effects, and in particular should not do anything that
* blocks the current thread. It may be called multiple times for the same state. It must do all its
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ abstract class StatelessWorkflow<InputT : Any, OutputT : Any, RenderingT : Any>
* - Emits an output.
*
* **Never call this method directly.** To get the rendering from a child workflow, pass the child
* and any required input to [WorkflowContext.compose].
* and any required input to [WorkflowContext.composeChild].
*
* This method *should not* have any side effects, and in particular should not do anything that
* blocks the current thread. It may be called multiple times for the same state. It must do all its
Expand Down Expand Up @@ -137,6 +137,6 @@ fun <InputT : Any, OutputT : Any, FromRenderingT : Any, ToRenderingT : Any>
transform: (FromRenderingT) -> ToRenderingT
): Workflow<InputT, OutputT, ToRenderingT> = Workflow.stateless { input, context ->
// @formatter:on
context.compose(this@mapRendering, input) { emitOutput(it) }
context.composeChild(this@mapRendering, input) { emitOutput(it) }
.let(transform)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package com.squareup.workflow

/**
* A composable, optionally-stateful object that can [handle events][WorkflowContext.onEvent],
* [delegate to children][WorkflowContext.compose], [subscribe][onReceive] to arbitrary streams from
* [delegate to children][WorkflowContext.composeChild], [subscribe][onReceive] to arbitrary streams from
* the outside world.
*
* The basic purpose of a `Workflow` is to take some [input][InputT] and return a
Expand Down Expand Up @@ -67,7 +67,7 @@ package com.squareup.workflow
* to its parent.
* May be [Nothing] if the workflow doesn't need to emit anything.
*
* @param RenderingT The value returned to this workflow's parent during [composition][compose].
* @param RenderingT The value returned to this workflow's parent during [composition][composeChild].
* Typically represents a "view" of this workflow's input, current state, and children's renderings.
* A workflow that represents a UI component may use a view model as its rendering type.
*
Expand All @@ -78,7 +78,7 @@ interface Workflow<in InputT : Any, out OutputT : Any, out RenderingT : Any> {

/**
* Provides a [StatefulWorkflow] view of this workflow. Necessary because [StatefulWorkflow] is
* the common API required for [WorkflowContext.compose] to do its work.
* the common API required for [WorkflowContext.composeChild] to do its work.
*/
fun asStatefulWorkflow(): StatefulWorkflow<InputT, *, OutputT, RenderingT>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import kotlin.reflect.KType
*
* ## Composing Children
*
* See [compose].
* See [composeChild].
*
* ## Handling Workflow Teardown
*
Expand Down Expand Up @@ -126,7 +126,7 @@ interface WorkflowContext<StateT : Any, in OutputT : Any> {
* @param key An optional string key that is used to distinguish between workflows of the same
* type.
*/
fun <ChildInputT : Any, ChildOutputT : Any, ChildRenderingT : Any> compose(
fun <ChildInputT : Any, ChildOutputT : Any, ChildRenderingT : Any> composeChild(
child: Workflow<ChildInputT, ChildOutputT, ChildRenderingT>,
input: ChildInputT,
key: String = "",
Expand All @@ -137,7 +137,7 @@ interface WorkflowContext<StateT : Any, in OutputT : Any> {
* Adds an action to be invoked if the workflow is discarded by its parent before the next
* compose pass. Multiple hooks can be registered in the same compose pass, they will be invoked
* in the same order they're set. Like any other work performed through the context (e.g. calls
* to [compose] or [onReceive]), hooks are cleared at the start of each compose pass, so you must
* to [composeChild] or [onReceive]), hooks are cleared at the start of each composeChild pass, so you must
* set any hooks you need in each compose pass.
*
* Teardown handlers should be non-blocking and execute quickly, since they are invoked
Expand Down Expand Up @@ -176,46 +176,46 @@ inline fun <StateT : Any, OutputT : Any, reified T> WorkflowContext<StateT, Outp
)

/**
* Convenience alias of [WorkflowContext.compose] for workflows that don't take input.
* Convenience alias of [WorkflowContext.composeChild] for workflows that don't take input.
*/
fun <StateT : Any, OutputT : Any, ChildOutputT : Any, ChildRenderingT : Any>
WorkflowContext<StateT, OutputT>.compose(
WorkflowContext<StateT, OutputT>.composeChild(
// Intellij refuses to format this parameter list correctly because of the weird line break,
// and detekt will complain about it.
// @formatter:off
child: Workflow<Unit, ChildOutputT, ChildRenderingT>,
key: String = "",
handler: (ChildOutputT) -> WorkflowAction<StateT, OutputT>
): ChildRenderingT = compose(child, Unit, key, handler)
): ChildRenderingT = composeChild(child, Unit, key, handler)
// @formatter:on

/**
* Convenience alias of [WorkflowContext.compose] for workflows that don't take input or emit
* Convenience alias of [WorkflowContext.composeChild] for workflows that don't take input or emit
* output.
*/
fun <InputT : Any, StateT : Any, OutputT : Any, ChildRenderingT : Any>
WorkflowContext<StateT, OutputT>.compose(
WorkflowContext<StateT, OutputT>.composeChild(
// Intellij refuses to format this parameter list correctly because of the weird line break,
// and detekt will complain about it.
// @formatter:off
child: Workflow<InputT, Nothing, ChildRenderingT>,
input: InputT,
key: String = ""
): ChildRenderingT = compose(child, input, key) { WorkflowAction.noop() }
): ChildRenderingT = composeChild(child, input, key) { WorkflowAction.noop() }
// @formatter:on

/**
* Convenience alias of [WorkflowContext.compose] for workflows that don't take input or emit
* Convenience alias of [WorkflowContext.composeChild] for workflows that don't take input or emit
* output.
*/
fun <StateT : Any, OutputT : Any, ChildRenderingT : Any>
WorkflowContext<StateT, OutputT>.compose(
WorkflowContext<StateT, OutputT>.composeChild(
// Intellij refuses to format this parameter list correctly because of the weird line break,
// and detekt will complain about it.
// @formatter:off
child: Workflow<Unit, Nothing, ChildRenderingT>,
key: String = ""
): ChildRenderingT = compose(child, Unit, key) { WorkflowAction.noop() }
): ChildRenderingT = composeChild(child, Unit, key) { WorkflowAction.noop() }
// @formatter:on

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class RealWorkflowContext<StateT : Any, OutputT : Any>(

// @formatter:off
override fun <ChildInputT : Any, ChildOutputT : Any, ChildRenderingT : Any>
compose(
composeChild(
child: Workflow<ChildInputT, ChildOutputT, ChildRenderingT>,
input: ChildInputT,
key: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ class RealWorkflowContextTest {
assertEquals("foo", output)
}

@Test fun `compose works`() {
@Test fun `composeChild works`() {
val context = RealWorkflowContext(TestComposer())
val workflow = TestWorkflow()

val (case, child, id, input) = context.compose(workflow, "input", "key") { output ->
val (case, child, id, input) = context.composeChild(workflow, "input", "key") { output ->
emitOutput("output:$output")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import com.squareup.workflow.Workflow
import com.squareup.workflow.WorkflowAction.Companion.emitOutput
import com.squareup.workflow.WorkflowAction.Companion.enterState
import com.squareup.workflow.WorkflowContext
import com.squareup.workflow.compose
import com.squareup.workflow.composeChild
import com.squareup.workflow.invoke
import com.squareup.workflow.onReceive
import com.squareup.workflow.parse
Expand Down Expand Up @@ -477,7 +477,7 @@ class WorkflowNodeTest {
state: String,
context: WorkflowContext<String, Nothing>
): String {
val childRendering = context.compose(childWorkflow, "child input")
val childRendering = context.composeChild(childWorkflow, "child input")
return "$state|$childRendering"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class CompositionIntegrationTest {
context: WorkflowContext<Boolean, Nothing>
): () -> Unit {
if (state) {
context.compose(child1, key = "child1")
context.compose(child2, key = "child2")
context.composeChild(child1, key = "child1")
context.composeChild(child2, key = "child2")
}
return context.onEvent<Unit> { enterState(false) }::invoke
}
Expand All @@ -142,7 +142,7 @@ class CompositionIntegrationTest {
context.onTeardown { teardowns += "grandchild" }
}
val child = Workflow.stateless<Nothing, Unit> { context ->
context.compose(grandchild)
context.composeChild(grandchild)
context.onTeardown { teardowns += "child" }
}
// A workflow that will render child1 and child2 until its rendering is invoked, at which point
Expand All @@ -159,7 +159,7 @@ class CompositionIntegrationTest {
context: WorkflowContext<Boolean, Nothing>
): () -> Unit {
if (state) {
context.compose(child)
context.composeChild(child)
}
return context.onEvent<Unit> { enterState(false) }::invoke
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal class TreeWorkflow(
): Rendering {
val childRenderings = children
.mapIndexed { index, child ->
val childRendering = context.compose(child, "$input[$index]", child.name)
val childRendering = context.composeChild(child, "$input[$index]", child.name)
Pair(child.name, childRendering)
}
.toMap()
Expand Down