Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more operator overloading to Observable and CompositeDisposable #234

Open
wants to merge 4 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/kotlin/io/reactivex/rxjava3/kotlin/disposable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ operator fun CompositeDisposable.plusAssign(disposable: Disposable) {
add(disposable)
}


/**
* disposable -= observable.subscribe()
*/
operator fun CompositeDisposable.minusAssign(disposable: Disposable) {
delete(disposable)
}

/**
* Add the disposable to a CompositeDisposable.
* @param compositeDisposable CompositeDisposable to add this disposable to
* @return this instance
*/
fun Disposable.addTo(compositeDisposable: CompositeDisposable): Disposable =
apply { compositeDisposable.add(this) }
apply { compositeDisposable += this }
8 changes: 8 additions & 0 deletions src/main/kotlin/io/reactivex/rxjava3/kotlin/observable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,11 @@ fun <A : Any, B : Any> Observable<Pair<A, B>>.toMultimap(): Single<MutableMap<A,
@SchedulerSupport(SchedulerSupport.NONE)
fun <T : Any> Iterable<ObservableSource<T>>.concatAll(): Observable<T> =
Observable.concat(this)

/**
* Merge multiple Observable by using plus operator
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
operator fun <T: Any> Observable<T>.plus(other: Observable<T>): Observable<T> =
Observable.merge(this, other)
14 changes: 13 additions & 1 deletion src/test/kotlin/io/reactivex/rxjava3/kotlin/ObservableTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,19 @@ class ObservableTest {
.concatAll()
.toList()
.subscribe { result ->
Assert.assertEquals((0 until 30).toList(), result)
assertEquals((0 until 30).toList(), result)
}
}

@Test fun testMergeWithPlus() {
val output =
Observable.just(1, 2) +
Observable.just(3, 4) +
Observable.just(5, 6) +
Observable.just(7, 8) +
Observable.just(9, 10)
val result = mutableListOf<Int>()
output.subscribe { data -> result.add(data) }
assertEquals(result, (1..10).toMutableList())
}
}
45 changes: 36 additions & 9 deletions src/test/kotlin/io/reactivex/rxjava3/kotlin/SubscriptionTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,55 @@ package io.reactivex.rxjava3.kotlin

import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.CompositeDisposable
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.util.concurrent.TimeUnit

class SubscriptionTest {
@Test fun testSubscriptionAddTo() {
val compositeSubscription = CompositeDisposable()

// Create an asynchronous subscription
// The delay ensures that we don't automatically unsubscribe because data finished emitting
val subscription = Observable.just("test")
.delay(100, TimeUnit.MILLISECONDS)
.subscribe()
// Create an asynchronous subscription
// The delay ensures that we don't automatically unsubscribe because data finished emitting
private val subscription = Observable.just("test")
.delay(100, TimeUnit.MILLISECONDS)
.subscribe()

private lateinit var compositeSubscription: CompositeDisposable

@Before
fun setUp() {
compositeSubscription = CompositeDisposable()
}

@After
fun tearDown() {
compositeSubscription.dispose()
assert(compositeSubscription.isDisposed)
}

@Test
fun testSubscriptionAddTo() {
assert(!subscription.isDisposed)

subscription.addTo(compositeSubscription)

assert(compositeSubscription.size() > 0)
assert(!subscription.isDisposed)
}

compositeSubscription.dispose()
@Test
fun testSubscriptionAddToWithPlusAssign() {
compositeSubscription += subscription

assert(compositeSubscription.isDisposed)
assert(compositeSubscription.size() > 0)
}

@Test
fun testSubscriptionDeleteWithMinusAssign() {
this.testSubscriptionAddToWithPlusAssign()

compositeSubscription -= subscription

assert(compositeSubscription.size() == 0)
}
}