File tree Expand file tree Collapse file tree 3 files changed +18
-0
lines changed
src/main/java/com/diffplug/common/rx Expand file tree Collapse file tree 3 files changed +18
-0
lines changed Original file line number Diff line number Diff line change 1
1
# DurianRx releases
2
2
3
3
## [ Unreleased]
4
+ ### Added
5
+ - ` RxExecutor.launch ` lets a user run ` suspend ` functions within that executor.
6
+ - ` GuardedExecutor ` now has a lazily populated scope which cancels when its guard disposes, as well as a ` launch ` method.
4
7
5
8
## [ 5.0.2] - 2025-01-31
6
9
### Fixed
Original file line number Diff line number Diff line change @@ -20,9 +20,13 @@ import java.util.*
20
20
import java.util.concurrent.CompletionStage
21
21
import java.util.concurrent.Executor
22
22
import java.util.function.Supplier
23
+ import kotlinx.coroutines.CoroutineScope
23
24
import kotlinx.coroutines.Deferred
24
25
import kotlinx.coroutines.Job
26
+ import kotlinx.coroutines.SupervisorJob
27
+ import kotlinx.coroutines.cancel
25
28
import kotlinx.coroutines.flow.Flow
29
+ import kotlinx.coroutines.launch
26
30
27
31
/* *
28
32
* GuardedExecutor is an [Executor] and [RxSubscriber] which promises to cancel its subscriptions
@@ -31,6 +35,14 @@ import kotlinx.coroutines.flow.Flow
31
35
* Useful for tying asynchronous tasks to gui elements.
32
36
*/
33
37
open class GuardedExecutor (val delegate : RxExecutor , val guard : Chit ) : Executor, RxSubscriber {
38
+ val scope: CoroutineScope by lazy {
39
+ CoroutineScope (SupervisorJob () + delegate.dispatcher).apply {
40
+ guard.runWhenDisposed { cancel() }
41
+ }
42
+ }
43
+
44
+ fun launch (block : suspend CoroutineScope .() -> Unit ): Job = scope.launch(block = block)
45
+
34
46
override fun execute (command : Runnable ) {
35
47
delegate.executor.execute(guard.guard(command))
36
48
}
Original file line number Diff line number Diff line change @@ -36,6 +36,9 @@ import kotlinx.coroutines.launch
36
36
class RxExecutor internal constructor(val executor : Executor , val dispatcher : CoroutineDispatcher ) :
37
37
RxSubscriber {
38
38
39
+ fun launch (block : suspend CoroutineScope .() -> Unit ): Job =
40
+ CoroutineScope (Job () + dispatcher).launch(block = block)
41
+
39
42
interface Has : Executor {
40
43
val rxExecutor: RxExecutor
41
44
}
You can’t perform that action at this time.
0 commit comments