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

Question: how to implement a custom LifecycleScopeProvider #467

Closed
ericntd opened this issue Jul 12, 2022 · 1 comment
Closed

Question: how to implement a custom LifecycleScopeProvider #467

ericntd opened this issue Jul 12, 2022 · 1 comment

Comments

@ericntd
Copy link

ericntd commented Jul 12, 2022

Library version: 1.1.0

I've read https://uber.github.io/AutoDispose/ and looked, looked at Javadoc and https://github.com/uber/AutoDispose/blob/455ec23037d6e1af234d5fda9cd571c490d1b306/autodispose/src/test/java/autodispose2/TestScopeProviderTest.java but it's still unclear
I hope someone can shed some light on how I can achieve my goal: dispose the disposables when a specific event happens (user logs out

I have this custom LifecycleScopeProvider

class AuthenticationScopeProvider : LifecycleScopeProvider<AuthenticationEvent> {
    private val relay: BehaviorRelay<AuthenticationEvent> = BehaviorRelay.createDefault(
        AuthenticationEvent.PRE_AUTHENTICATED
    )

    private val function : CorrespondingEventsFunction<AuthenticationEvent> by lazy {
        CorrespondingEventsFunction<AuthenticationEvent> { event ->
            when (event) {
                AuthenticationEvent.PRE_AUTHENTICATED,
                AuthenticationEvent.AUTHENTICATED -> AuthenticationEvent.LOGGED_OUT
                AuthenticationEvent.LOGGED_OUT -> throw LifecycleEndedException()
            }
        }
    }

    override fun lifecycle(): Observable<AuthenticationEvent> {
        return relay.hide()
    }

    override fun correspondingEvents(): CorrespondingEventsFunction<AuthenticationEvent> {
        return function
    }

    override fun peekLifecycle(): AuthenticationEvent? {
        return relay.value
    }

    /**
     * Call this when the user is authenticated with the CMB backend.
     */
    fun becomeAuthenticated() {
        if (relay.value != AuthenticationEvent.AUTHENTICATED) {
            relay.accept(AuthenticationEvent.AUTHENTICATED)
        }
    }

    /**
     * Call this when a logout has started.
     */
    fun becomeUnAuthenticated() {
        if (relay.value != AuthenticationEvent.LOGGED_OUT) {
            relay.accept(AuthenticationEvent.LOGGED_OUT)
        }
    }

But I'm not sure whether the disposal is actually happening
Both this test

    @Test
    fun `test dispose`() {
        val provider = AuthenticationScopeProvider()
    val obs = Single.fromCallable {
        Thread.sleep(2000L)
        return@fromCallable "something"
    }.subscribeOn(Schedulers.io())
    val proxy = obs
            .`as`(AutoDispose.autoDisposable(provider))

            val disposable = proxy.subscribe()
        provider.becomeUnAuthenticated();
    Assert.assertTrue(disposable.isDisposed)
    }

and this

@Test(expected = LifecycleEndedException::class)
    fun `test dispose`() {
        val provider = AuthenticationScopeProvider()
    val obs = Single.fromCallable {
        Thread.sleep(2000L)
        return@fromCallable "something"
    }.subscribeOn(Schedulers.io())
    val proxy = obs
            .`as`(AutoDispose.autoDisposable(provider))

            val disposable = proxy.subscribe()
        provider.becomeUnAuthenticated();
    }

fails

When I explicitly call provider.requestScope().subscribe(observer), the 2nd test passes

fun becomeUnAuthenticated() {
        if (relay.value != AuthenticationEvent.LOGGED_OUT) {
            relay.accept(AuthenticationEvent.LOGGED_OUT)
        }
        requestScope().subscribe(object : CompletableObserver {
            override fun onSubscribe(d: Disposable) {
                // no-op
            }

            override fun onComplete() {
                println("completed") // never triggered
            }

            override fun onError(e: Throwable) {
                e.printStackTrace()
            }
        })
    }

However, the onComplete() is never called for the observer
What am I missing?

@ZacSweers
Copy link
Collaborator

Please use the discussions section for questions, this is the issue tracker

@ZacSweers ZacSweers closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants