I understand that `this.setContent {/* ... */}` wh...
# compose
m
I understand that
this.setContent {/* ... */}
where
this: ComponentActivity
creates or otherwise obtains a
ComposeView
which usually uses the window
Recomposer
as the
CompositionContext
of the toplevel
Composition
of the view hierarchy. I also see that the
.composition: Composition
of the
ComposeView
has a private
.composer: ComposerImpl
field. (Note
ComposerImpl: Composer
)
Copy code
private val composer: ComposerImpl =
        ComposerImpl(
            applier = applier,
            parentContext = parent,
            slotTable = slotTable,
            abandonSet = abandonSet,
            changes = changes,
            lateChanges = lateChanges,
            composition = this
        ).also {
            parent.registerComposer(it)
        }
As far as I can tell, the
.parent: CompositionContext
is typically the window
Recomposer
as that’s the default, but the implementation of
registerComposer
by
Recomposer
does nothing; It’s actually a no-op. So the activity has a window, which has • A
CompositionContext
(a
Recomposer
) • A
ComposeView
view hierarchy with a
Composition
that has a
ComposerImpl: Composer
But the
Recomposer
does nothing when the
ComposerImpl
is passed to it via the
.registerComposer(it)
. So how is it made aware of the composer, and how is everything put together?
c
m
@Chrimaeon Could you please elaborate? What does the plugin do to tell the
Recomposer
about the
ComposerImpl
?
c
As suggested in your previous post - read the book if you want to learn the internals. It’s not that easy how compose runtime works and just from looking at the code you will not get the whole picture.
m
@Chrimaeon What previous post? This is a different question
m
@Chrimaeon That question was about how
ComponentActivity
installed snapshot observers 🤔 But speaking of, the pointers Albert Chang provided let me eventually find out that the
setContent
method creates/obtains and mounts a
ComposeView
, which has a
.composition: Composition
field and a
.resolveParentCompositionContext(): CompositionContext
method to return the parent of the composition it hosts, which defaults to the window
Recomposer
. The
ComposeView
also gets/has a child
AbstractComposeView
which creates and returns the composition to store in `.composition: Composition`: In
androidx.compose.ui.platform.doSetContent
, a
WrappedComposition
is either obtained if already present or created by wrapping a
CompositionImpl
. The
.setContent(content)
call on the wrapped composition in turn calls the
setContent
method on the wrapped composition (which in this case is a
CompositionImpl
) which in turn calls the
composeInitial
method of its context (which in this case is the window
Recomposer
). The
composeInitial
method of
Recomposer
calls
composing
on the receiver, which contains…
Copy code
val snapshot = Snapshot.takeMutableSnapshot(
    readObserverOf(composition), writeObserverOf(composition, modifiedValues)
)
try {
    return snapshot.enter(block)
} finally {
    applyAndCheck(snapshot)
}
…this code to take and observe a snapshot. So why did you tell me to have a look at
Applier
? It didn’t turn out to be relevant, unless I’m missing something. If I am, please let me know in the other thread since I’m asking a new different question in this thread
c
There is a link to a book in the post. 🤦🏼‍♂️
👎 1
m
@Chrimaeon Why the facepalm emoji? I’ve checked out the links and one of them has seemingly turned out to be a false lead, so I was politely asking for a correction in case I’m wrong. I’m already reading the book FYI, but it’s 200 pages long and it hasn’t got around to covering things that’d answer my question yet. I don’t expect an explanation over Slack to be comprehensive, but I also reckon it’s not unreasonable to ask for an executive summary of sorts so I know what to look out for while going through the book. English is not my first language, so I apologise if I’m reading you wrong, but I think it’s clear that I’m not asking ‘can you do my homework’ questions and that I’m doing a lot of due research on my part. You brought up Applier, which didn’t seem relevant to either question I’ve asked so I made sure to explain why I failed to see the connection too, and you decided to reply with a facepalm emoji – I’m not sure what I’ve done to elicit this dismissive response
s
Recomposer is the one that initializes the composition, check how
composeInitial
works