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 11# DurianRx releases
22
33## [ 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.
47
58## [ 5.0.2] - 2025-01-31
69### Fixed
Original file line number Diff line number Diff line change @@ -20,9 +20,13 @@ import java.util.*
2020import java.util.concurrent.CompletionStage
2121import java.util.concurrent.Executor
2222import java.util.function.Supplier
23+ import kotlinx.coroutines.CoroutineScope
2324import kotlinx.coroutines.Deferred
2425import kotlinx.coroutines.Job
26+ import kotlinx.coroutines.SupervisorJob
27+ import kotlinx.coroutines.cancel
2528import kotlinx.coroutines.flow.Flow
29+ import kotlinx.coroutines.launch
2630
2731/* *
2832 * GuardedExecutor is an [Executor] and [RxSubscriber] which promises to cancel its subscriptions
@@ -31,6 +35,14 @@ import kotlinx.coroutines.flow.Flow
3135 * Useful for tying asynchronous tasks to gui elements.
3236 */
3337open 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+
3446 override fun execute (command : Runnable ) {
3547 delegate.executor.execute(guard.guard(command))
3648 }
Original file line number Diff line number Diff line change @@ -36,6 +36,9 @@ import kotlinx.coroutines.launch
3636class RxExecutor internal constructor(val executor : Executor , val dispatcher : CoroutineDispatcher ) :
3737 RxSubscriber {
3838
39+ fun launch (block : suspend CoroutineScope .() -> Unit ): Job =
40+ CoroutineScope (Job () + dispatcher).launch(block = block)
41+
3942 interface Has : Executor {
4043 val rxExecutor: RxExecutor
4144 }
You can’t perform that action at this time.
0 commit comments