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.basic03 |
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.channels.* |
| 11 | import kotlinx.coroutines.reactive.* |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 12 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 13 | fun main() = runBlocking<Unit> { |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 14 | val source = Flowable.range(1, 5) // a range of five numbers |
| 15 | .doOnSubscribe { println("OnSubscribe") } // provide some insight |
Roman Elizarov | 0dffcfd | 2018-06-29 18:51:52 +0300 | [diff] [blame] | 16 | .doOnComplete { println("OnComplete") } // ... |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 17 | .doFinally { println("Finally") } // ... into what's going on |
| 18 | var cnt = 0 |
Vsevolod Tolstopyatov | 313978c | 2018-06-01 15:30:34 +0300 | [diff] [blame] | 19 | source.openSubscription().consume { // open channel to the source |
| 20 | for (x in this) { // iterate over the channel to receive elements from it |
| 21 | println(x) |
| 22 | if (++cnt >= 3) break // break when 3 elements are printed |
| 23 | } |
Roman Elizarov | 0dffcfd | 2018-06-29 18:51:52 +0300 | [diff] [blame] | 24 | // Note: `consume` cancels the channel when this block of code is complete |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 25 | } |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 26 | } |