|
4 | 4 |
|
5 | 5 | package kotlinx.coroutines.rx3
|
6 | 6 |
|
7 |
| -import io.reactivex.rxjava3.core.Scheduler |
| 7 | +import io.reactivex.rxjava3.core.* |
| 8 | +import io.reactivex.rxjava3.disposables.* |
| 9 | +import io.reactivex.rxjava3.plugins.* |
8 | 10 | import kotlinx.coroutines.*
|
9 |
| -import java.util.concurrent.TimeUnit |
10 |
| -import kotlin.coroutines.CoroutineContext |
| 11 | +import kotlinx.coroutines.channels.* |
| 12 | +import java.util.concurrent.* |
| 13 | +import kotlin.coroutines.* |
11 | 14 |
|
12 | 15 | /**
|
13 | 16 | * Converts an instance of [Scheduler] to an implementation of [CoroutineDispatcher]
|
14 | 17 | * and provides native support of [delay] and [withTimeout].
|
15 | 18 | */
|
16 |
| -public fun Scheduler.asCoroutineDispatcher(): SchedulerCoroutineDispatcher = SchedulerCoroutineDispatcher(this) |
| 19 | +public fun Scheduler.asCoroutineDispatcher(): CoroutineDispatcher = |
| 20 | + if (this is DispatcherScheduler) { |
| 21 | + dispatcher |
| 22 | + } else { |
| 23 | + SchedulerCoroutineDispatcher(this) |
| 24 | + } |
| 25 | + |
| 26 | +@Deprecated(level = DeprecationLevel.HIDDEN, message = "Since 1.4.2, binary compatibility with earlier versions") |
| 27 | +@JvmName("asCoroutineDispatcher") |
| 28 | +public fun Scheduler.asCoroutineDispatcher0(): SchedulerCoroutineDispatcher = |
| 29 | + SchedulerCoroutineDispatcher(this) |
17 | 30 |
|
18 | 31 | /**
|
19 | 32 | * Implements [CoroutineDispatcher] on top of an arbitrary [Scheduler].
|
@@ -50,3 +63,121 @@ public class SchedulerCoroutineDispatcher(
|
50 | 63 | /** @suppress */
|
51 | 64 | override fun hashCode(): Int = System.identityHashCode(scheduler)
|
52 | 65 | }
|
| 66 | + |
| 67 | +/** |
| 68 | + * Converts an instance of [CoroutineDispatcher] to an implementation of [Scheduler]. |
| 69 | + */ |
| 70 | +public fun CoroutineDispatcher.asScheduler(): Scheduler = |
| 71 | + if (this is SchedulerCoroutineDispatcher) { |
| 72 | + scheduler |
| 73 | + } else { |
| 74 | + DispatcherScheduler(this) |
| 75 | + } |
| 76 | + |
| 77 | +private class DispatcherScheduler(val dispatcher: CoroutineDispatcher) : Scheduler() { |
| 78 | + |
| 79 | + private val schedulerJob = SupervisorJob() |
| 80 | + |
| 81 | + private val scope = CoroutineScope(schedulerJob + dispatcher + CoroutineExceptionHandler { _, throwable -> |
| 82 | + RxJavaPlugins.onError(throwable) |
| 83 | + }) |
| 84 | + |
| 85 | + override fun scheduleDirect(block: Runnable): Disposable = |
| 86 | + scheduleDirect(block, 0, TimeUnit.MILLISECONDS) |
| 87 | + |
| 88 | + override fun scheduleDirect(block: Runnable, delay: Long, unit: TimeUnit): Disposable { |
| 89 | + if (!scope.isActive) return Disposable.disposed() |
| 90 | + val newBlock = RxJavaPlugins.onSchedule(block) |
| 91 | + return scope.launch { |
| 92 | + delay(unit.toMillis(delay)) |
| 93 | + newBlock.run() |
| 94 | + }.asDisposable() |
| 95 | + } |
| 96 | + |
| 97 | + override fun createWorker(): Worker = |
| 98 | + DispatcherWorker(dispatcher, schedulerJob).also { |
| 99 | + it.start() |
| 100 | + } |
| 101 | + |
| 102 | + override fun shutdown() { |
| 103 | + scope.cancel() |
| 104 | + } |
| 105 | + |
| 106 | + private class DispatcherWorker(dispatcher: CoroutineDispatcher, parentJob: Job) : Worker() { |
| 107 | + |
| 108 | + private val workerJob = SupervisorJob(parentJob) |
| 109 | + private val workerScope = CoroutineScope(workerJob + dispatcher) |
| 110 | + private val blockChannel = Channel<SchedulerChannelTask>(Channel.UNLIMITED) |
| 111 | + |
| 112 | + fun start() { |
| 113 | + workerScope.launch { |
| 114 | + while (isActive) { |
| 115 | + val task = blockChannel.receive() |
| 116 | + task.execute() |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + override fun isDisposed(): Boolean = !workerScope.isActive |
| 122 | + |
| 123 | + override fun schedule(block: Runnable): Disposable = |
| 124 | + schedule(block, 0, TimeUnit.MILLISECONDS) |
| 125 | + |
| 126 | + override fun schedule(block: Runnable, delay: Long, unit: TimeUnit): Disposable { |
| 127 | + if (!workerScope.isActive) return Disposable.disposed() |
| 128 | + |
| 129 | + val newBlock = RxJavaPlugins.onSchedule(block) |
| 130 | + |
| 131 | + val taskJob = Job(workerJob) |
| 132 | + val task = SchedulerChannelTask(newBlock, taskJob) |
| 133 | + |
| 134 | + if (delay <= 0L) { |
| 135 | + blockChannel.offer(task) |
| 136 | + } else { |
| 137 | + // Use `taskJob` as the parent here so the delay will also get cancelled if the Disposable |
| 138 | + // is disposed. |
| 139 | + workerScope.launch(taskJob) { |
| 140 | + // Delay *before* enqueuing the task, so other tasks (e.g. via schedule without delay) |
| 141 | + // aren't blocked by the delay. |
| 142 | + delay(unit.toMillis(delay)) |
| 143 | + // Once the task is ready to run, it still needs to be executed via the queue to comply |
| 144 | + // with the Scheduler contract of running all worker tasks in a non-overlapping manner. |
| 145 | + blockChannel.offer(task) |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + return taskJob.asDisposable() |
| 150 | + } |
| 151 | + |
| 152 | + override fun dispose() { |
| 153 | + workerScope.cancel() |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +/** |
| 159 | + * Represents a task to be queued sequentially on a [Channel] for a [Scheduler.Worker]. |
| 160 | + * |
| 161 | + * Delayed tasks do not block [Channel] from processing other tasks |
| 162 | + */ |
| 163 | +private class SchedulerChannelTask( |
| 164 | + private val block: Runnable, |
| 165 | + job: Job |
| 166 | +) : JobDisposable(job) { |
| 167 | + fun execute() { |
| 168 | + if (job.isActive) { |
| 169 | + block.run() |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +private open class JobDisposable(protected val job: Job) : Disposable { |
| 175 | + override fun isDisposed(): Boolean = !job.isActive |
| 176 | + |
| 177 | + override fun dispose() { |
| 178 | + job.cancel() |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +private fun Job.asDisposable(): Disposable = JobDisposable(this) |
| 183 | + |
0 commit comments