Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | db0ef0c | 2019-07-03 15:02:44 +0300 | [diff] [blame] | 2 | * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 6 | package kotlinx.coroutines.rx2.guide.context05 |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 7 | |
Roman Elizarov | 95981f3 | 2017-03-17 18:12:04 +0300 | [diff] [blame] | 8 | import io.reactivex.* |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 9 | import kotlinx.coroutines.* |
| 10 | import kotlinx.coroutines.reactive.* |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 11 | import io.reactivex.functions.BiFunction |
| 12 | import io.reactivex.schedulers.Schedulers |
| 13 | import java.util.concurrent.TimeUnit |
| 14 | |
| 15 | fun rangeWithIntervalRx(scheduler: Scheduler, time: Long, start: Int, count: Int): Flowable<Int> = |
| 16 | Flowable.zip( |
| 17 | Flowable.range(start, count), |
| 18 | Flowable.interval(time, TimeUnit.MILLISECONDS, scheduler), |
| 19 | BiFunction { x, _ -> x }) |
| 20 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 21 | fun main() = runBlocking<Unit> { |
Inego | 69c26df | 2019-04-21 14:51:25 +0700 | [diff] [blame] | 22 | val job = launch(Dispatchers.Unconfined) { // launch a new coroutine in Unconfined context (without its own thread pool) |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 23 | rangeWithIntervalRx(Schedulers.computation(), 100, 1, 3) |
Vsevolod Tolstopyatov | 0685dc4 | 2019-04-24 12:16:56 +0300 | [diff] [blame] | 24 | .collect { println("$it on thread ${Thread.currentThread().name}") } |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 25 | } |
| 26 | job.join() // wait for our coroutine to complete |
| 27 | } |