Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // This file was automatically generated from coroutines-guide-reactive.md by Knit tool. Do not edit. |
Roman Elizarov | a9687a3 | 2018-06-29 17:28:38 +0300 | [diff] [blame] | 18 | package kotlinx.coroutines.experimental.rx2.guide.basic05 |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 19 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 20 | import io.reactivex.schedulers.* |
Roman Elizarov | f1572ca | 2017-03-17 18:11:15 +0300 | [diff] [blame] | 21 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 22 | import kotlinx.coroutines.experimental.rx2.* |
| 23 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 24 | |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 25 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 26 | // coroutine -- fast producer of elements in the context of the main thread |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 27 | val source = rxFlowable(coroutineContext) { |
Roman Elizarov | 3f87164 | 2017-04-17 15:46:54 +0300 | [diff] [blame] | 28 | for (x in 1..3) { |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 29 | send(x) // this is a suspending function |
Roman Elizarov | 3f87164 | 2017-04-17 15:46:54 +0300 | [diff] [blame] | 30 | println("Sent $x") // print after successfully sent item |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | // subscribe on another thread with a slow subscriber using Rx |
| 34 | source |
| 35 | .observeOn(Schedulers.io(), false, 1) // specify buffer size of 1 item |
| 36 | .doOnComplete { println("Complete") } |
| 37 | .subscribe { x -> |
Roman Elizarov | 3f87164 | 2017-04-17 15:46:54 +0300 | [diff] [blame] | 38 | Thread.sleep(500) // 500ms to process each item |
| 39 | println("Processed $x") |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 40 | } |
Roman Elizarov | 3f87164 | 2017-04-17 15:46:54 +0300 | [diff] [blame] | 41 | delay(2000) // suspend the main thread for a few seconds |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 42 | } |