Skip to content

Commit 4bf0412

Browse files
Add More SessionWorkflow lifecycle documentation/suggestions
1 parent de3674a commit 4bf0412

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

workflow-core/src/commonMain/kotlin/com/squareup/workflow1/SessionWorkflow.kt

+39-17
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,24 @@ public abstract class SessionWorkflow<
3939
* linking them to the lifetime of a Workflow session. For example,
4040
* here is how you might safely combine two `StateFlow`s:
4141
*
42-
* data class MyState(
43-
* val derivedValue: String,
44-
* val derivedWorker: Worker<String>
45-
* )
42+
* data class MyState(
43+
* val derivedValue: String,
44+
* val derivedWorker: Worker<String>
45+
* )
4646
*
47-
* override fun initialState(
48-
* props: Unit,
49-
* snapshot: Snapshot?,
50-
* workflowScope: CoroutineScope
51-
* ): MyState {
52-
* val transformedStateFlow = stateFlow1.combine(stateFlow2, {val1, val2 -> val1 - val2}).
53-
* stateIn(workflowScope, SharingStarted.Eagerly, ${stateFlow1.value}-${stateFlow2.value})
47+
* override fun initialState(
48+
* props: Unit,
49+
* snapshot: Snapshot?,
50+
* workflowScope: CoroutineScope
51+
* ): MyState {
52+
* val transformedStateFlow = stateFlow1.combine(stateFlow2, {val1, val2 -> val1 - val2}).
53+
* stateIn(workflowScope, SharingStarted.Eagerly, ${stateFlow1.value}-${stateFlow2.value})
5454
*
55-
* return MyState(
56-
* transformedStateFlow.value,
57-
* transformedStateFlow.asWorker()
58-
* )
59-
* }
55+
* return MyState(
56+
* transformedStateFlow.value,
57+
* transformedStateFlow.asWorker()
58+
* )
59+
* }
6060
*
6161
* - set reliable teardown hooks, e.g. via [Job.invokeOnCompletion][kotlinx.coroutines.Job.invokeOnCompletion].
6262
* Note however, that while these are reliable in the sense of being guaranteed to be executed
@@ -70,7 +70,29 @@ public abstract class SessionWorkflow<
7070
* or [Flow.onCompletion][kotlinx.coroutines.flow.onCompletion] to handle that.
7171
*
7272
* If you have a general cleanup operation that is fast and thread-safe then you could use
73-
* [Job.invokeOnCompletion][kotlinx.coroutines.Job.invokeOnCompletion].
73+
* [Job.invokeOnCompletion][kotlinx.coroutines.Job.invokeOnCompletion]. Otherwise use
74+
* [awaitCancellation][kotlinx.coroutines.awaitCancellation] with a finally block. Examples:
75+
*
76+
* override fun initialState(
77+
* props: Unit,
78+
* snapshot: Snapshot?,
79+
* workflowScope: CoroutineScope
80+
* ): MyState {
81+
* someService.start()
82+
* workflowScope.coroutineContext.job.invokeOnCompletion {
83+
* // If quick and does not need to execute on the Workflow runtime's thread.
84+
* someService.stop()
85+
* }
86+
* workflowScope.launch(start = CoroutineStart.UNDISPATCHED) {
87+
* try {
88+
* awaitCancellation()
89+
* } finally {
90+
* // If should be executed on the Workflow runtime's thread.
91+
* someService.stop()
92+
* }
93+
* }
94+
* return MyState()
95+
* }
7496
*
7597
* **Note Carefully**: Neither [workflowScope] nor any of these transformed/computed dependencies
7698
* should be stored by this Workflow instance. This could be re-created, or re-used unexpectedly

0 commit comments

Comments
 (0)