Skip to content

Instantly share code, notes, and snippets.

@aartikov
Created December 14, 2022 09:27
Show Gist options
  • Save aartikov/a56cc94bb306e05b7b7927353910da08 to your computer and use it in GitHub Desktop.
Save aartikov/a56cc94bb306e05b7b7927353910da08 to your computer and use it in GitHub Desktop.
Creates CoroutineScope for Decompose component
fun ComponentContext.componentCoroutineScope(): CoroutineScope {
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
if (lifecycle.state != Lifecycle.State.DESTROYED) {
lifecycle.doOnDestroy {
scope.cancel()
}
} else {
scope.cancel()
}
return scope
}
@zhombie
Copy link

zhombie commented Jan 21, 2023

@aartikov hi! Thanks for great library. How about ViewModel adapted viewModelScope extension for ComponentContext CoroutineScope. It is not the final version, just suggestion, in order to know what u think about it:

import com.arkivanov.decompose.ComponentContext
import com.arkivanov.essenty.instancekeeper.InstanceKeeper
import com.arkivanov.essenty.lifecycle.Lifecycle
import com.arkivanov.essenty.lifecycle.doOnDestroy
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlin.coroutines.CoroutineContext

private const val INSTANCE_KEY =
    "com.arkivanov.decompose.lifecycle.ComponentContextCoroutineScope.INSTANCE_KEY"

val ComponentContext.componentScope: CoroutineScope
    get() {
        val scope = instanceKeeper.get(INSTANCE_KEY)
        if (scope is CoroutineScope) return scope

        fun destroy() {
            try {
                scope?.onDestroy()
            } catch (e: Exception) {
                throw RuntimeException(e)
            } finally {
                instanceKeeper.remove(INSTANCE_KEY)
            }
        }

        if (lifecycle.state == Lifecycle.State.DESTROYED) {
            destroy()
        } else {
            lifecycle.doOnDestroy {
                destroy()
            }
        }

        return DestroyableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate).also {
            instanceKeeper.put(INSTANCE_KEY, it)
        }
    }

class DestroyableCoroutineScope(
    context: CoroutineContext
) : CoroutineScope, InstanceKeeper.Instance {

    override val coroutineContext: CoroutineContext = context

    override fun onDestroy() {
        coroutineContext.cancel()
    }

}

@aartikov
Copy link
Author

@zhombie
Hi. It's a great idea to make this an extension property and use InstanceKeeper for caching. Thank you!
By the way, the author of Decompose is @arkivanov. I am just a user and popularizer of the library.

@arkivanov
Copy link

arkivanov commented Nov 27, 2023

Usually InstanceKeeper instances outlive the hosting component. So scoping a job that captures the component would be a memory leak. Even though it's discouraged, you can still make all your components retained, so that you the original extension function would work just fine.

@arkivanov
Copy link

Btw, since Essenty version 1.3.0-beta01, Lifecycle#doOnDestroy {} callback is automatically called if the lifecycle is already destroyed. So there is no need to check for DESTROYED state manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment