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

Don't use replay(1) to cache View references #137

Merged
merged 1 commit into from
May 8, 2019
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ package com.nhaarman.acorn.presentation
import androidx.annotation.CallSuper
import arrow.core.Option
import arrow.core.toOption
import com.nhaarman.acorn.presentation.RxScene.ContainerEvent.Attached
import com.nhaarman.acorn.presentation.RxScene.ContainerEvent.Detached
import com.nhaarman.acorn.state.SceneState
import io.reactivex.Observable
import io.reactivex.disposables.CompositeDisposable
Expand Down Expand Up @@ -75,20 +73,13 @@ abstract class RxScene<V : Container>(

private val startedEventsSubject = BehaviorSubject.createDefault(false)

private val containerEventsSubject = BehaviorSubject.createDefault<ContainerEvent<V>>(Detached)
private val viewSubject = BehaviorSubject.createDefault(Option.empty<V>())

/**
* Publishes a stream of optional [V] instances that are attached to this
* Scene.
*/
protected val view: Observable<Option<V>> = containerEventsSubject
.map { event ->
when (event) {
is Attached<V> -> event.v.toOption()
is Detached -> Option.empty()
}
}
.replay(1).autoConnect()
protected val view: Observable<Option<V>> = viewSubject.hide()

@CallSuper
override fun onStart() {
Expand All @@ -99,12 +90,12 @@ abstract class RxScene<V : Container>(
@CallSuper
override fun attach(v: V) {
super.attach(v)
containerEventsSubject.onNext(Attached(v))
viewSubject.onNext(v.toOption())
}

@CallSuper
override fun detach(v: V) {
containerEventsSubject.onNext(Detached)
viewSubject.onNext(Option.empty())
super.detach(v)
}

Expand All @@ -119,12 +110,6 @@ abstract class RxScene<V : Container>(
sceneDisposables.dispose()
}

@Suppress("unused")
private sealed class ContainerEvent<out V> {
class Attached<V>(val v: V) : ContainerEvent<V>()
object Detached : ContainerEvent<Nothing>()
}

/**
* A utility function to automatically subscribe and dispose of source
* [Observable] instance when this Scene starts and stops.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ class RxSceneTest {
expect(observer.lastValue).toBe(Option.just(testView))
}

@Test
fun `subscribing after view attach`() {
/* Given */
scene.attach(testView)

/* When */
val observer = scene.viewObservable.test()

/* Then */
expect(observer.lastValue).toBe(Option.just(testView))
nhaarman marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
fun `detaching a view notifies observers`() {
/* Given */
Expand Down