|
| 1 | +@file:Suppress("PackageDirectoryMismatch") |
| 2 | + |
| 3 | +package playground.rxjava |
| 4 | + |
| 5 | +import io.reactivex.rxjava3.core.Observable |
| 6 | +import io.reactivex.rxjava3.schedulers.Schedulers |
| 7 | + |
| 8 | +/** |
| 9 | + * ReactiveX/RxJava - a library for composing asynchronous and event-based programs using observable sequences for the Java VM. |
| 10 | + * |
| 11 | + * - [ReactiveX/RxJava: GitHub](https://github.com/ReactiveX/RxJava) |
| 12 | + * - [JavaDoc](http://reactivex.io/RxJava/3.x/javadoc/) |
| 13 | + */ |
| 14 | + |
| 15 | +fun main() { |
| 16 | + println() |
| 17 | + println("# RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.") |
| 18 | + |
| 19 | + onlyAdultsObservable(persons()).subscribe( |
| 20 | + { v -> println("Received: $v") }, |
| 21 | + { e -> println("Error: $e") }, |
| 22 | + { println("Complete!") } |
| 23 | + ) |
| 24 | + |
| 25 | + personsUnder30() |
| 26 | + concurrentPersonNames() |
| 27 | + numbers() |
| 28 | +} |
| 29 | + |
| 30 | +private fun onlyAdultsObservable(persons: List<Person>) = |
| 31 | + Observable.create<Person> { emitter -> |
| 32 | + persons.forEach { person -> |
| 33 | + if (person.age < 18) { |
| 34 | + emitter.onError(Exception("The person is underage")) |
| 35 | + } |
| 36 | + emitter.onNext(person) |
| 37 | + } |
| 38 | + emitter.onComplete() |
| 39 | + } |
| 40 | + |
| 41 | +private fun numbers() { |
| 42 | + Observable.range(1, 10) |
| 43 | + .map { e -> e * 10 } |
| 44 | + .subscribeOn(Schedulers.newThread()) |
| 45 | + .blockingSubscribe( |
| 46 | + { value -> println("Recived: $value") }, |
| 47 | + { error -> println("Error: $error") }, |
| 48 | + { println("Completed!") } |
| 49 | + ) |
| 50 | +} |
| 51 | + |
| 52 | +private fun personsUnder30() { |
| 53 | + Observable.fromIterable(persons()) |
| 54 | + .filter { p -> p.age < 30 } |
| 55 | + .map { it.name.toUpperCase() } |
| 56 | + .subscribe( |
| 57 | + { value -> println("Received: $value") }, |
| 58 | + { error -> println("Error: $error") }, |
| 59 | + { println("Completed!") } |
| 60 | + ) |
| 61 | +} |
| 62 | + |
| 63 | +private fun concurrentPersonNames() { |
| 64 | + val sherlock = Person("Sherlock", "Holmes", 20) |
| 65 | + val mycroft = Person("Mycroft", "Holmes", 30) |
| 66 | + val john = Person("John", "Watson", 25) |
| 67 | + |
| 68 | + Observable.just(sherlock, mycroft, john) |
| 69 | + .flatMap { p -> Observable.just(p.name.toUpperCase()).subscribeOn(Schedulers.io()) } |
| 70 | + .subscribe( |
| 71 | + { value -> println("Recived: $value") }, |
| 72 | + { error -> println("Error: $error") }, |
| 73 | + { println("Completed!") } |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +private fun persons() = listOf( |
| 78 | + Person("Sherlock", "Holmes", 20), |
| 79 | + Person("Mycroft", "Holmes", 30), |
| 80 | + Person("John", "Watson", 17) |
| 81 | +) |
| 82 | + |
| 83 | +data class Person(var name: String, var lastName: String, var age: Int) |
0 commit comments