androidx.compose.foundation

MutatorMutex

Class · from org.jetbrains.compose.foundation:foundation:1.12.2

No hand-written docs yet.

Mutual exclusion for UI state mutation over time.

class MutatorMutex

Used by

Parameters that take a MutatorMutex.

Functions

mutate

fun <R> mutate(
    priority: MutatePriority = MutatePriority.Default,
    block: suspend () -> R,
)

Enforce that only a single caller may be active at a time.

mutateWith

fun <T, R> mutateWith(
    receiver: T,
    priority: MutatePriority = MutatePriority.Default,
    block: suspend T.() -> R,
)

Enforce that only a single caller may be active at a time.

tryMutate

fun tryMutate(block: () -> Unit): Boolean

Attempt to mutate synchronously if there is no other active caller. If there is no other active caller, the block will be executed in a lock. If there is another active caller, this method will return false, indicating that the active caller needs to be cancelled through a mutate or mutateWith call with an equal or higher mutation priority.

Library docs

Mutual exclusion for UI state mutation over time.

mutate permits interruptible state mutation over time using a standard MutatePriority. A MutatorMutex enforces that only a single writer can be active at a time for a particular state resource. Instead of queueing callers that would acquire the lock like a traditional Mutex, new attempts to mutate the guarded state will either cancel the current mutator or if the current mutator has a higher priority, the new caller will throw CancellationException.

MutatorMutex should be used for implementing hoisted state objects that many mutators may want to manipulate over time such that those mutators can coordinate with one another. The MutatorMutex instance should be hidden as an implementation detail. For example: