blob: e2f836f8e0fee8a3cec324e7d6c2163c21274bc3 [file] [log] [blame]
/*
* Copyright 2016-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit.
package kotlinx.coroutines.experimental.rx2.guide.basic05
import io.reactivex.schedulers.*
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.rx2.*
import kotlin.coroutines.experimental.*
fun main(args: Array<String>) = runBlocking<Unit> {
// coroutine -- fast producer of elements in the context of the main thread
val source = rxFlowable(coroutineContext) {
for (x in 1..3) {
send(x) // this is a suspending function
println("Sent $x") // print after successfully sent item
}
}
// subscribe on another thread with a slow subscriber using Rx
source
.observeOn(Schedulers.io(), false, 1) // specify buffer size of 1 item
.doOnComplete { println("Complete") }
.subscribe { x ->
Thread.sleep(500) // 500ms to process each item
println("Processed $x")
}
delay(2000) // suspend the main thread for a few seconds
}