File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed
src/main/java/com/diffplug/common/rx Expand file tree Collapse file tree 1 file changed +12
-0
lines changed 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 }
You can’t perform that action at this time.
0 commit comments