Skip to content

Commit 503a8c3

Browse files
WIP Reimplemented Workers using side effects.
This is the Kotlin half of square/workflow#1021.
1 parent 998abd0 commit 503a8c3

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

Diff for: workflow-core/src/main/java/com/squareup/workflow/RenderContext.kt

+16-13
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,6 @@ interface RenderContext<StateT, in OutputT : Any> {
101101
handler: (ChildOutputT) -> WorkflowAction<StateT, OutputT>
102102
): ChildRenderingT
103103

104-
/**
105-
* Ensures [worker] is running. When the [Worker] emits an output, [handler] is called
106-
* to determine the [WorkflowAction] to take. When the worker finishes, nothing happens (although
107-
* another render pass may be triggered).
108-
*
109-
* @param key An optional string key that is used to distinguish between identical [Worker]s.
110-
*/
111-
fun <T> runningWorker(
112-
worker: Worker<T>,
113-
key: String = "",
114-
handler: (T) -> WorkflowAction<StateT, OutputT>
115-
)
116-
117104
/**
118105
* Ensures [sideEffect] is running with the given [key].
119106
*
@@ -195,6 +182,22 @@ fun <StateT, OutputT : Any> RenderContext<StateT, OutputT>.runningWorker(
195182
}
196183
}
197184

185+
/**
186+
* Ensures [worker] is running. When the [Worker] emits an output, [handler] is called
187+
* to determine the [WorkflowAction] to take. When the worker finishes, nothing happens (although
188+
* another render pass may be triggered).
189+
*
190+
* @param key An optional string key that is used to distinguish between identical [Worker]s.
191+
*/
192+
fun <T, StateT, OutputT : Any> RenderContext<StateT, OutputT>.runningWorker(
193+
worker: Worker<T>,
194+
key: String = "",
195+
handler: (T) -> WorkflowAction<StateT, OutputT>
196+
) {
197+
val workerWorkflow = WorkerWorkflow<T>()
198+
renderChild(workerWorkflow, props = worker, key = key, handler = handler)
199+
}
200+
198201
/**
199202
* Alternative to [RenderContext.actionSink] that allows externally defined
200203
* event types to be mapped to anonymous [WorkflowAction]s.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.squareup.workflow
2+
3+
/**
4+
* TODO write documentation
5+
*/
6+
// TODO remove upper bound
7+
@OptIn(ExperimentalWorkflowApi::class)
8+
internal class WorkerWorkflow<OutputT>(
9+
) : StatefulWorkflow<Worker<OutputT>, Int, OutputT, Unit>(), ImpostorWorkflow {
10+
11+
override fun initialState(
12+
props: Worker<OutputT>,
13+
snapshot: Snapshot?
14+
): Int = 0
15+
16+
override fun onPropsChanged(
17+
old: Worker<OutputT>,
18+
new: Worker<OutputT>,
19+
state: Int
20+
): Int = if (!old.doesSameWorkAs(new)) state + 1 else state
21+
22+
override fun render(
23+
props: Worker<OutputT>,
24+
state: Int,
25+
context: RenderContext<Int, OutputT>
26+
) {
27+
context.runningSideEffect(state.toString()) {
28+
props.run()
29+
.collectToSink(context.actionSink) { output ->
30+
action { setOutput(output) }
31+
}
32+
}
33+
}
34+
35+
override fun snapshotState(state: Int): Snapshot = Snapshot.EMPTY
36+
}

0 commit comments

Comments
 (0)