# decompose


03/19/2024, 8:19 AM
Anyhow, I thought I'd mention my use case because this change is relevant to the project I'm working on where I use a custom context which (among other things) includes a reference to the parent component/context. This is used to pass certain events up the component hierarchy which happens frequently enough that passing a lambda/state from the parent to the child via constructor arguments would be too bothersome. Basically, this is what it looks like: a custom context and the corresponding
function to shadow
Copy code
interface AppContext : ComponentContext {
    val parentContext: AppContext?

private class DefaultAppContext(override val parentContext: AppContext?, baseContext: ComponentContext) :
    ComponentContext by baseContext

fun AppContext.childContext(key: String) = DefaultAppContext(this, childContext(key)) // <- this actually resolves fine
All-in-all this actually works quite nicely; most details are abstracted away and I can easily provide some functionality to all components. With the proper TestContext testing is no problem too. With the new factory, I think it would change into this:
Copy code
private class DefaultAppContext(
    override val parentContext: AppContext?,
    override val lifecycle: Lifecycle,
    override val stateKeeper: StateKeeper
    override val instanceKeeper: InstanceKeeper
    override val backHandler: BackHandler
) : AppContext, ComponentContext {
    override val componentContextFactory = AppComponentContextFactory(this)

class AppComponentContextFactory(val self: AppContext) : ComponentContextFactory<AppContext> {
    operator fun invoke(
        lifecycle: Lifecycle,
        stateKeeper: StateKeeper,
        instanceKeeper: InstanceKeeper,
        backHandler: BackHandler,
    ) = DefaultAppContext(self, lifecycle, StateKeeper, instanceKeeper, backHandler)
Basically the same; probably a little nicer due to the removal of the childContext override. If you have an idea for a more elegant solution, I'd love to hear it!