Skip to content

Commit d09bf20

Browse files
262: Deprecate Worker.createSideEffect in favour of RenderContext.runningSideEffect
Replace with runningSideEffect where possible or with a Worker<Unit> instead of Worker<Nothing>.
1 parent 7b3ff45 commit d09bf20

File tree

9 files changed

+256
-225
lines changed

9 files changed

+256
-225
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ public fun <PropsT, StateT, OutputT, ChildRenderingT>
246246
* Ensures a [Worker] that never emits anything is running. Since [worker] can't emit anything,
247247
* it can't trigger any [WorkflowAction]s.
248248
*
249-
* A simple way to create workers that don't output anything is using [Worker.createSideEffect].
249+
* If your [Worker] does not output anything, then simply use [runningSideEffect].
250250
*
251251
* @param key An optional string key that is used to distinguish between identical [Worker]s.
252252
*/

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public interface Worker<out OutputT> {
177177
* Shorthand for `flow { block() }.asWorker()`.
178178
*
179179
* Note: If your worker just needs to perform side effects and doesn't need to emit anything,
180-
* use [createSideEffect] instead (since `Nothing` can't be used as a reified type parameter).
180+
* do not use a [Worker] but instead all [BaseRenderContext::runningSideEffect]
181181
*/
182182
@OptIn(ExperimentalTypeInference::class)
183183
public inline fun <reified OutputT> create(
@@ -193,14 +193,25 @@ public interface Worker<out OutputT> {
193193
* fun logOnEntered(message: String) = Worker.createSideEffect() {
194194
* println("Entered state: $message")
195195
* }
196+
* ```
196197
*
197198
* Note that all workers created with this method are equivalent from the point of view of
198199
* their [Worker.doesSameWorkAs] methods. A workflow that needs multiple simultaneous
199200
* side effects can either bundle them all together into a single `createSideEffect`
200201
* call, or can use the `key` parameter to [BaseRenderContext.runningWorker] to prevent
201202
* conflicts.
202-
* ```
203+
*
204+
* Deprecated: This convenience extension is deprecated as redundant.
205+
* [BaseRenderContext.runningSideEffect] can be used instead with a suspend function
206+
* and a key to uniquely identify the side effect in the runtime.
203207
*/
208+
@Deprecated(
209+
message = "Worker not needed, simply call RenderContext.runningSideEffect " +
210+
"with a suspend fun.",
211+
ReplaceWith(
212+
expression = "runningSideEffect(key, block)"
213+
)
214+
)
204215
public fun createSideEffect(
205216
block: suspend () -> Unit
206217
): Worker<Nothing> = TypedWorker(TYPE_OF_NOTHING, flow { block() })

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,11 @@ class RenderWorkflowInTest {
364364
@Test fun `cancelling scope cancels runtime`() {
365365
var cancellationException: Throwable? = null
366366
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
367-
runningWorker(Worker.createSideEffect {
367+
runningSideEffect(key = "test1") {
368368
suspendCancellableCoroutine { continuation ->
369369
continuation.invokeOnCancellation { cause -> cancellationException = cause }
370370
}
371-
})
371+
}
372372
}
373373
renderWorkflowIn(workflow, expectedSuccessScope, MutableStateFlow(Unit)) {}
374374
assertNull(cancellationException)
@@ -383,11 +383,11 @@ class RenderWorkflowInTest {
383383
@Test fun `failing scope cancels runtime`() {
384384
var cancellationException: Throwable? = null
385385
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
386-
runningWorker(Worker.createSideEffect {
386+
runningSideEffect(key = "failing") {
387387
suspendCancellableCoroutine { continuation ->
388388
continuation.invokeOnCancellation { cause -> cancellationException = cause }
389389
}
390-
})
390+
}
391391
}
392392
renderWorkflowIn(workflow, expectedSuccessScope, MutableStateFlow(Unit)) {}
393393
assertNull(cancellationException)
@@ -418,13 +418,13 @@ class RenderWorkflowInTest {
418418
@Test fun `error from renderings collector cancels runtime`() {
419419
var cancellationException: Throwable? = null
420420
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
421-
runningWorker(Worker.createSideEffect {
421+
runningSideEffect(key = "test") {
422422
suspendCancellableCoroutine { continuation ->
423423
continuation.invokeOnCancellation { cause ->
424424
cancellationException = cause
425425
}
426426
}
427-
})
427+
}
428428
}
429429
val renderings = renderWorkflowIn(workflow, allowedToFailScope, MutableStateFlow(Unit)) {}
430430

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,11 @@ internal class WorkflowRunnerTest {
171171
@Test fun `cancelRuntime() cancels runtime`() {
172172
var cancellationException: Throwable? = null
173173
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
174-
runningWorker(Worker.createSideEffect {
174+
runningSideEffect(key = "test side effect") {
175175
suspendCancellableCoroutine { continuation ->
176176
continuation.invokeOnCancellation { cause -> cancellationException = cause }
177177
}
178-
})
178+
}
179179
}
180180
val runner = WorkflowRunner(workflow, MutableStateFlow(Unit))
181181
runner.nextRendering()
@@ -209,11 +209,11 @@ internal class WorkflowRunnerTest {
209209
@Test fun `cancelling scope cancels runtime`() {
210210
var cancellationException: Throwable? = null
211211
val workflow = Workflow.stateless<Unit, Nothing, Unit> {
212-
runningWorker(Worker.createSideEffect {
212+
runningSideEffect(key = "test") {
213213
suspendCancellableCoroutine { continuation ->
214214
continuation.invokeOnCancellation { cause -> cancellationException = cause }
215215
}
216-
})
216+
}
217217
}
218218
val runner = WorkflowRunner(workflow, MutableStateFlow(Unit))
219219
runner.nextRendering()

workflow-rx2/src/main/java/com/squareup/workflow1/rx2/RxWorkers.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,8 @@ public inline fun <reified T : Any> Single<out T?>.asWorker(): Worker<T> =
7878
*
7979
* The key is required for this operator because there is no type information available to
8080
* distinguish workers.
81+
*
82+
* TODO: https://github.com/square/workflow-kotlin/issues/526 once this is removed.
8183
*/
84+
@Suppress("DEPRECATION")
8285
public fun Completable.asWorker(): Worker<Nothing> = Worker.createSideEffect { await() }

workflow-testing/src/test/java/com/squareup/workflow1/WorkerCompositionIntegrationTest.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,9 @@ internal class WorkerCompositionIntegrationTest {
140140

141141
@Test fun `runningWorker gets error`() {
142142
val workflow = Workflow.stateless<Unit, Unit, Unit> {
143-
runningWorker(Worker.createSideEffect { throw ExpectedException() })
143+
runningWorker(Worker.from<Unit> { throw ExpectedException() }) {
144+
action { }
145+
}
144146
}
145147

146148
assertFailsWith<ExpectedException> {
@@ -156,8 +158,8 @@ internal class WorkerCompositionIntegrationTest {
156158
val channel = Channel<Unit>()
157159
val workflow = Workflow.stateless<Unit, Unit, Unit> {
158160
runningWorker(
159-
channel.consumeAsFlow()
160-
.asWorker()
161+
channel.consumeAsFlow()
162+
.asWorker()
161163
) { fail("Expected handler to not be invoked.") }
162164
}
163165

@@ -180,26 +182,26 @@ internal class WorkerCompositionIntegrationTest {
180182
}
181183

182184
val workflow = Workflow.stateful<Int, Int, () -> Unit>(
183-
initialState = 0,
184-
render = { state ->
185-
runningWorker(triggerOutput) { action { setOutput(state) } }
185+
initialState = 0,
186+
render = { state ->
187+
runningWorker(triggerOutput) { action { setOutput(state) } }
186188

187-
return@stateful { actionSink.send(incrementState) }
188-
}
189+
return@stateful { actionSink.send(incrementState) }
190+
}
189191
)
190192

191193
workflow.launchForTestingFromStartWith {
192194
triggerOutput.send(Unit)
193195
assertEquals(0, awaitNextOutput())
194196

195197
awaitNextRendering()
196-
.invoke()
198+
.invoke()
197199
triggerOutput.send(Unit)
198200

199201
assertEquals(1, awaitNextOutput())
200202

201203
awaitNextRendering()
202-
.invoke()
204+
.invoke()
203205
triggerOutput.send(Unit)
204206

205207
assertEquals(2, awaitNextOutput())
@@ -223,9 +225,11 @@ internal class WorkerCompositionIntegrationTest {
223225

224226
@Test fun `runningWorker doesn't throw when worker finishes`() {
225227
// No-op worker, completes immediately.
226-
val worker = Worker.createSideEffect {}
228+
val worker = Worker.from { }
227229
val workflow = Workflow.stateless<Unit, Unit, Unit> {
228-
runningWorker(worker)
230+
runningWorker(worker) {
231+
action { }
232+
}
229233
}
230234

231235
workflow.launchForTestingFromStartWith {

workflow-testing/src/test/java/com/squareup/workflow1/WorkerTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class WorkerTest {
4444
}
4545
}
4646

47+
@Suppress("DEPRECATION")
4748
@Test fun `createSideEffect returns equivalent workers`() {
4849
val worker1 = Worker.createSideEffect {}
4950
val worker2 = Worker.createSideEffect {}
@@ -52,6 +53,7 @@ class WorkerTest {
5253
assertTrue(worker1.doesSameWorkAs(worker2))
5354
}
5455

56+
@Suppress("DEPRECATION")
5557
@Test fun `createSideEffect runs`() {
5658
var ran = false
5759
val worker = Worker.createSideEffect {
@@ -63,6 +65,7 @@ class WorkerTest {
6365
}
6466
}
6567

68+
@Suppress("DEPRECATION")
6669
@Test fun `createSideEffect finishes`() {
6770
val worker = Worker.createSideEffect {}
6871

@@ -71,6 +74,7 @@ class WorkerTest {
7174
}
7275
}
7376

77+
@Suppress("DEPRECATION")
7478
@Test fun `createSideEffect propagates exceptions`() {
7579
val worker = Worker.createSideEffect { throw ExpectedException() }
7680

0 commit comments

Comments
 (0)