blob: 5ce8b54822ee0353e3cf357ad4e6f4466e0268ef [file] [log] [blame]
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 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 Elizarova9687a32018-06-29 17:28:38 +03006package kotlinx.coroutines.experimental.rx2.guide.basic02
Roman Elizarov8a4a8e12017-03-09 19:52:58 +03007
Roman Elizarov95981f32017-03-17 18:12:04 +03008import kotlinx.coroutines.experimental.*
9import kotlinx.coroutines.experimental.reactive.*
Roman Elizarov9fe5f462018-02-21 19:05:52 +030010import kotlin.coroutines.experimental.*
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030011
12fun main(args: Array<String>) = runBlocking<Unit> {
Roman Elizarov86349be2017-03-17 16:47:37 +030013 // create a publisher that produces numbers from 1 to 3 with 200ms delays between them
Roman Elizarov43e3af72017-07-21 16:01:31 +030014 val source = publish<Int>(coroutineContext) {
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030015 // ^^^^^^^ <--- Difference from the previous examples is here
16 println("Begin") // mark the beginning of this coroutine in output
Roman Elizarov86349be2017-03-17 16:47:37 +030017 for (x in 1..3) {
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030018 delay(200) // wait for 200ms
19 send(x) // send number x to the channel
20 }
21 }
Roman Elizarov86349be2017-03-17 16:47:37 +030022 // print elements from the source
23 println("Elements:")
24 source.consumeEach { // consume elements from it
25 println(it)
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030026 }
Roman Elizarov86349be2017-03-17 16:47:37 +030027 // print elements from the source AGAIN
28 println("Again:")
29 source.consumeEach { // consume elements from it
30 println(it)
Roman Elizarov8a4a8e12017-03-09 19:52:58 +030031 }
32}