Skip to content

Commit 7be9bf9

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 8802ef9 commit 7be9bf9

File tree

10 files changed

+249
-218
lines changed

10 files changed

+249
-218
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
@@ -258,7 +258,7 @@ public fun <PropsT, StateT, OutputT, ChildRenderingT>
258258
* Ensures a [Worker] that never emits anything is running. Since [worker] can't emit anything,
259259
* it can't trigger any [WorkflowAction]s.
260260
*
261-
* A simple way to create workers that don't output anything is using [Worker.createSideEffect].
261+
* If your [Worker] does not output anything, then simply use [runningSideEffect].
262262
*
263263
* @param key An optional string key that is used to distinguish between identical [Worker]s.
264264
*/

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public interface Worker<out OutputT> {
185185
* Shorthand for `flow { block() }.asWorker()`.
186186
*
187187
* Note: If your worker just needs to perform side effects and doesn't need to emit anything,
188-
* use [createSideEffect] instead (since `Nothing` can't be used as a reified type parameter).
188+
* do not use a [Worker] but instead all [BaseRenderContext::runningSideEffect]
189189
*/
190190
@OptIn(ExperimentalTypeInference::class)
191191
public inline fun <reified OutputT> create(
@@ -201,14 +201,25 @@ public interface Worker<out OutputT> {
201201
* fun logOnEntered(message: String) = Worker.createSideEffect() {
202202
* println("Entered state: $message")
203203
* }
204+
* ```
204205
*
205206
* Note that all workers created with this method are equivalent from the point of view of
206207
* their [Worker.doesSameWorkAs] methods. A workflow that needs multiple simultaneous
207208
* side effects can either bundle them all together into a single `createSideEffect`
208209
* call, or can use the `key` parameter to [BaseRenderContext.runningWorker] to prevent
209210
* conflicts.
210-
* ```
211+
*
212+
* Deprecated: This convenience extension is deprecated as redundant.
213+
* [BaseRenderContext.runningSideEffect] can be used instead with a suspend function
214+
* and a key to uniquely identify the side effect in the runtime.
211215
*/
216+
@Deprecated(
217+
message = "Worker not needed, simply call RenderContext.runningSideEffect " +
218+
"with a suspend fun.",
219+
ReplaceWith(
220+
expression = "runningSideEffect(key, block)"
221+
)
222+
)
212223
public fun createSideEffect(
213224
block: suspend () -> Unit
214225
): 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: 7 additions & 3 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> {
@@ -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)