ReusableComposeNode 中的 Reusable 在 Jetpack Compose 中是什么意思?
What does Reusable in ReusableComposeNode mean in Jetpack Compose?
在 Jetpack Compose 中,每个 Layout 可组合项都会发出一个 ReusableComposeNode。但是 Reusable 在这里意味着什么?下面是 ReusableComposeNode 的代码。
@Composable inline fun <T : Any, reified E : Applier<*>> ReusableComposeNode(
noinline factory: () -> T,
update: @DisallowComposableCalls Updater<T>.() -> Unit
) {
if (currentComposer.applier !is E) invalidApplier()
currentComposer.startReusableNode()
if (currentComposer.inserting) {
currentComposer.createNode { factory() }
} else {
currentComposer.useNode()
}
currentComposer.disableReusing()
Updater<T>(currentComposer).update()
currentComposer.enableReusing()
currentComposer.endNode()
}
我注意到这段代码中 ReusableComposeNode 和 ComposeNode 之间的唯一区别是创建组的方式。 ReusableComposeNode中使用的group创建方法startReusableNode()的KDoc是这样写的
Start a group that tracks a the code that will create or update a node
that is generated as part of the tree implied by the composition. A
reusable node can be reused in a reusable group even if the group key
is changed.
我不知道这里重用是什么意思。这是否意味着该节点按测量重用? (普通节点会从测量阶段开始。但是重用节点是否会跳过测量阶段?)
终于想通了
That will record the operation to navigate to the node by the Applier, but will skip the operations to create and initialize it.
我在 Compose Internals 一书中找到了答案。
在 Jetpack Compose 中,每个 Layout 可组合项都会发出一个 ReusableComposeNode。但是 Reusable 在这里意味着什么?下面是 ReusableComposeNode 的代码。
@Composable inline fun <T : Any, reified E : Applier<*>> ReusableComposeNode(
noinline factory: () -> T,
update: @DisallowComposableCalls Updater<T>.() -> Unit
) {
if (currentComposer.applier !is E) invalidApplier()
currentComposer.startReusableNode()
if (currentComposer.inserting) {
currentComposer.createNode { factory() }
} else {
currentComposer.useNode()
}
currentComposer.disableReusing()
Updater<T>(currentComposer).update()
currentComposer.enableReusing()
currentComposer.endNode()
}
我注意到这段代码中 ReusableComposeNode 和 ComposeNode 之间的唯一区别是创建组的方式。 ReusableComposeNode中使用的group创建方法startReusableNode()的KDoc是这样写的
Start a group that tracks a the code that will create or update a node that is generated as part of the tree implied by the composition. A reusable node can be reused in a reusable group even if the group key is changed.
我不知道这里重用是什么意思。这是否意味着该节点按测量重用? (普通节点会从测量阶段开始。但是重用节点是否会跳过测量阶段?)
终于想通了
That will record the operation to navigate to the node by the Applier, but will skip the operations to create and initialize it.
我在 Compose Internals 一书中找到了答案。