blob: 8e17ac9cd8eaa050716f5e8ae3e71f96759609e6 [file] [log] [blame]
Roman Elizarov4b0ef7b2017-04-17 12:39:29 +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 Elizarov4b0ef7b2017-04-17 12:39:29 +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.basic08
Roman Elizarov4b0ef7b2017-04-17 12:39:29 +03007
Roman Elizarov9fe5f462018-02-21 19:05:52 +03008import io.reactivex.subjects.*
Roman Elizarov0950dfa2018-07-13 10:33:25 +03009import kotlinx.coroutines.*
10import kotlinx.coroutines.rx2.*
11import kotlin.coroutines.*
Roman Elizarov4b0ef7b2017-04-17 12:39:29 +030012
Prendota65e6c8c2018-10-17 11:51:08 +030013fun main() = runBlocking<Unit> {
Roman Elizarov4b0ef7b2017-04-17 12:39:29 +030014 val subject = BehaviorSubject.create<String>()
15 subject.onNext("one")
16 subject.onNext("two")
Roman Elizarovd24ad362017-05-16 17:43:09 +030017 // now launch a coroutine to print the most recent update
Vsevolod Tolstopyatovbbaf99d2018-09-11 15:55:56 +030018 launch { // use the context of the main thread for a coroutine
Vsevolod Tolstopyatov0685dc42019-04-24 12:16:56 +030019 subject.collect { println(it) }
Roman Elizarov4b0ef7b2017-04-17 12:39:29 +030020 }
21 subject.onNext("three")
Roman Elizarov4b0ef7b2017-04-17 12:39:29 +030022 subject.onNext("four")
Roman Elizarovd24ad362017-05-16 17:43:09 +030023 yield() // yield the main thread to the launched coroutine <--- HERE
Roman Elizarov3ed7a7d2019-06-17 20:54:16 -070024 subject.onComplete() // now complete the subject's sequence to cancel the consumer, too
Roman Elizarov4b0ef7b2017-04-17 12:39:29 +030025}