@@ -39,24 +39,24 @@ public abstract class SessionWorkflow<
39
39
* linking them to the lifetime of a Workflow session. For example,
40
40
* here is how you might safely combine two `StateFlow`s:
41
41
*
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
+ * )
46
46
*
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})
54
54
*
55
- * return MyState(
56
- * transformedStateFlow.value,
57
- * transformedStateFlow.asWorker()
58
- * )
59
- * }
55
+ * return MyState(
56
+ * transformedStateFlow.value,
57
+ * transformedStateFlow.asWorker()
58
+ * )
59
+ * }
60
60
*
61
61
* - set reliable teardown hooks, e.g. via [Job.invokeOnCompletion][kotlinx.coroutines.Job.invokeOnCompletion].
62
62
* Note however, that while these are reliable in the sense of being guaranteed to be executed
@@ -70,7 +70,29 @@ public abstract class SessionWorkflow<
70
70
* or [Flow.onCompletion][kotlinx.coroutines.flow.onCompletion] to handle that.
71
71
*
72
72
* 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
+ * }
74
96
*
75
97
* **Note Carefully**: Neither [workflowScope] nor any of these transformed/computed dependencies
76
98
* should be stored by this Workflow instance. This could be re-created, or re-used unexpectedly
0 commit comments