blob: b84fc08f53fa7bbd72d066ebc59a2ab098dba93e [file] [log] [blame]
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03001/*
Roman Elizarovdb0ef0c2019-07-03 15:02:44 +03002 * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03003 */
4
5// This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit.
Roman Elizarov0950dfa2018-07-13 10:33:25 +03006package kotlinx.coroutines.rx2.guide.basic03
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03007
Roman Elizarov95981f32017-03-17 18:12:04 +03008import io.reactivex.*
Roman Elizarov0950dfa2018-07-13 10:33:25 +03009import kotlinx.coroutines.*
10import kotlinx.coroutines.channels.*
11import kotlinx.coroutines.reactive.*
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030012
Prendota65e6c8c2018-10-17 11:51:08 +030013fun main() = runBlocking<Unit> {
Roman Elizarov86349be2017-03-17 16:47:37 +030014 val source = Flowable.range(1, 5) // a range of five numbers
15 .doOnSubscribe { println("OnSubscribe") } // provide some insight
Roman Elizarov0dffcfd2018-06-29 18:51:52 +030016 .doOnComplete { println("OnComplete") } // ...
Roman Elizarov86349be2017-03-17 16:47:37 +030017 .doFinally { println("Finally") } // ... into what's going on
18 var cnt = 0
Vsevolod Tolstopyatov313978c2018-06-01 15:30:34 +030019 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 Elizarov0dffcfd2018-06-29 18:51:52 +030024 // Note: `consume` cancels the channel when this block of code is complete
Roman Elizarov86349be2017-03-17 16:47:37 +030025 }
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030026}