Composer

open class Composer<N>
kotlin.Any
   ↳ androidx.compose.Composer

Implementation of a composer for mutable tree.

Summary

Public constructors

<init>(slotTable: SlotTable, applier: Applier<N>, recomposer: Recomposer)

Implementation of a composer for mutable tree.

Public methods

Unit
apply(value: V, block: T.(V) -> Unit)

Schedule a change to be applied to a node's property.

Unit

Apply the changes to the tree that were collected during the last composition.

CompositionReference

Create or use a memoized CompositionReference instance at this position in the slot table.

Boolean
changed(value: T)

Determine if the current slot table value is equal to the given value, if true, the value is scheduled to be skipped during applyChanges and changes return false; otherwise applyChanges will update the slot table to value.

Unit

Start collecting key source information.

Unit
createNode(factory: () -> T)

Schedule a node to be created and inserted at the current location.

Unit
doneJoin(valid: Boolean)

End a join group.

Unit
emitNode(factory: () -> T)

Unit
emitNode(node: N)

Schedule the given node to be inserted.

Unit

End the current group.

Unit

Called to end the node group.

ScopeUpdateScope?

End a restart group.

Unit

End the composition.

Any
joinKey(left: Any?, right: Any?)

Create a composed key that can be used in calls to startGroup or startNode.

Any?

Return the next value in the slot table and advance the current location.

Boolean

Synchronously recompose all invalidated groups.

Unit

Skip a group.

Unit

Schedule a slot advance to happen during applyChanges.

Unit

Start a group with the given key.

(sync: Boolean) -> Unit
startJoin(key: Any, valid: Boolean, compose: (invalidate: (sync: Boolean) -> Unit) -> Unit)

Start a data join group which can be invalided by calling the lambda returned by this function.

Unit
startNode(key: Any)

Start emitting a node.

Unit

Start a restart group.

Unit

Start the composition.

Unit
updateValue(value: Any?)

Schedule the current value in the slot table to be updated to value.

N

Return the instance of the node that was inserted at the given location.

Protected methods

Unit
composeRoot(block: () -> Unit)

Extension functions

From androidx.compose
T
Composer<N>.cache(valid: Boolean = true, block: () -> T)

Cache a value in the composition.

Any?

Get the next value of the slot table.

V
Composer<N>.remember(block: () -> V)

Remember the value produced by block.

V
Composer<N>.remember(p1: P1, block: () -> V)

Remember the value returned by block if p1 is equal to the previous composition, otherwise produce and remember a new value by calling block.

V
Composer<N>.remember(p1: P1, p2: P2, block: () -> V)

Remember the value returned by block if p1 and p2 are equal to the previous composition, otherwise produce and remember a new value by calling block.

V
Composer<N>.remember(p1: P1, p2: P2, p3: P3, block: () -> V)

Remember the value returned by block if p1, p2 and p3 are equal to the previous composition, otherwise produce and remember a new value by calling block.

V
Composer<N>.remember(p1: P1, p2: P2, p3: P3, p4: P4, block: () -> V)

Remember the value returned by block if p1, p2, p3, p4 are equal to the previous composition, otherwise produce and remember a new value by calling block.

V
Composer<N>.remember(vararg args: Any?, block: () -> V)

Remember the value returned by block if all values of args are equal to the previous composition, otherwise produce and remember a new value by calling block.

actual T
Composer<*>.runWithCurrent(block: () -> T)

Properties

Int

The number of changes that have been scheduled to be applied during applyChanges.

Boolean

True if the composition is currently scheduling nodes to be inserted into the tree.

SlotTable

Backing storage for the composition

Public constructors

<init>

Composer(
    slotTable: SlotTable,
    applier: Applier<N>,
    recomposer: Recomposer)

Implementation of a composer for mutable tree.

Public methods

apply

fun <V, T> apply(
    value: V,
    block: T.(V) -> Unit
): Unit

Schedule a change to be applied to a node's property. This change will be applied to the node that is the current node in the tree which was either created by createNode, emitted by emitNode or returned by useNode.

applyChanges

fun applyChanges(): Unit

Apply the changes to the tree that were collected during the last composition.

buildReference

fun buildReference(): CompositionReference

Create or use a memoized CompositionReference instance at this position in the slot table.

changed

fun <T> changed(value: T): Boolean

Determine if the current slot table value is equal to the given value, if true, the value is scheduled to be skipped during applyChanges and changes return false; otherwise applyChanges will update the slot table to value. In either case the composer's slot table is advanced.

Parameters
value: T the value to be compared.

collectKeySourceInformation

fun collectKeySourceInformation(): Unit

Start collecting key source information. This enables enables the tool API to be able to determine the source location of where groups and nodes are created.

createNode

fun <T : N> createNode(factory: () -> T): Unit

Schedule a node to be created and inserted at the current location. This is only valid to call when the composer is inserting.

doneJoin

fun doneJoin(valid: Boolean): Unit

End a join group.

Parameters
valid: Boolean Deprecated and must always be true.

emitNode

fun <T : N> emitNode(factory: () -> T): Unit

emitNode

fun emitNode(node: N): Unit

Schedule the given node to be inserted. This is only valid to call when the composer is inserting.

endGroup

fun endGroup(): Unit

End the current group.

endNode

fun endNode(): Unit

Called to end the node group.

endRestartGroup

fun endRestartGroup(): ScopeUpdateScope?

End a restart group. If the recompose scope was marked used during composition then a ScopeUpdateScope is returned that allows attaching a lambda that will produce the same composition as was produced by this group (including calling startRestartGroup and endRestartGroup).

endRoot

fun endRoot(): Unit

End the composition. This should be called, and only be called, to end the first group in the composition.

joinKey

fun joinKey(
    left: Any?,
    right: Any?
): Any

Create a composed key that can be used in calls to startGroup or startNode. This will use the key stored at the current location in the slot table to avoid allocating a new key.

nextSlot

fun nextSlot(): Any?

Return the next value in the slot table and advance the current location.

recompose

fun recompose(): Boolean

Synchronously recompose all invalidated groups. This collects the changes which must be applied by applyChanges to have an effect.

skipCurrentGroup

fun skipCurrentGroup(): Unit

Skip a group. This is only valid to call if the composition is not inserting.

skipValue

fun skipValue(): Unit

Schedule a slot advance to happen during applyChanges.

startGroup

fun startGroup(key: Any): Unit

Start a group with the given key. During recomposition if the currently expected group does not match the given key a group the groups emitted in the same parent group are inspected to determine if one of them has this key and that group the first such group is moved (along with any nodes emitted by the group) to the current position and composition continues. If no group with this key is found, then the composition shifts into insert mode and new nodes are added at the current position.

Parameters
key: Any The key for the group

startJoin

fun startJoin(
    key: Any,
    valid: Boolean,
    compose: (invalidate: (sync: Boolean) -> Unit) -> Unit
): (sync: Boolean) -> Unit

Start a data join group which can be invalided by calling the lambda returned by this function.

Parameters
valid: Boolean Deprecated must always be true.
compose: (invalidate: (sync: Boolean) -> Unit) -> Unit a function that will produce the same composition that produced by this group.
Return
a lambda that will schedule recomposition of this group when invoked.

startNode

fun startNode(key: Any): Unit

Start emitting a node. It is required that one of emitNode or createNode is called after startNode. Similar to startGroup, if, during recomposition, the current node does not have the provided key a node with that key is scanned for and moved into the current position if found, if no such node is found the composition switches into insert mode and a the node is scheduled to be inserted at the current location.

Parameters
key: Any the key for the node.

startRestartGroup

fun startRestartGroup(key: Any): Unit

Start a restart group. A restart group creates a recompose scope and sets it as the current recompose scope of the composition. If the recompose scope is invalidated then this group will be recomposed. A recompose scope can be invalidated by calling the lambda returned by androidx.compose.invalidate.

startRoot

fun startRoot(): Unit

Start the composition. This should be called, and only be called, as the first group in the composition.

updateValue

fun updateValue(value: Any?): Unit

Schedule the current value in the slot table to be updated to value.

Parameters
value: Any? the value to schedule to be written to the slot table.

useNode

fun useNode(): N

Return the instance of the node that was inserted at the given location. This is only valid to call when the composition is not inserting. This must be called at the same location as emitNode or createNode as called even if the value is unused.

Protected methods

composeRoot

protected fun composeRoot(block: () -> Unit): Unit

Properties

changeCount

val changeCount: Int

The number of changes that have been scheduled to be applied during applyChanges.

Slot table movement (skipping groups and nodes) will be coalesced so this number is possibly less than the total changes detected.

inserting

val inserting: Boolean

True if the composition is currently scheduling nodes to be inserted into the tree. During first composition this is always true. During recomposition this is true when new nodes are being scheduled to be added to the tree.

slotTable

val slotTable: SlotTable

Backing storage for the composition