{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

BroadcastFrameClock

public final class BroadcastFrameClock implements MonotonicFrameClock


A simple frame clock.

This implementation is intended for low-contention environments involving low total numbers of threads in a pool on the order of ~number of CPU cores available for UI recomposition work, while avoiding additional allocation where possible.

onNewAwaiters will be invoked whenever the number of awaiters has changed from 0 to 1. If onNewAwaiters fails by throwing an exception it will permanently fail this BroadcastFrameClock; all current and future awaiters will resume with the thrown exception.

Summary

Public constructors

BroadcastFrameClock(Function0<Unit> onNewAwaiters)

Public methods

final void

Permanently cancel this BroadcastFrameClock and cancel all current and future awaiters with cancellationException.

final boolean

true if there are any callers of withFrameNanos awaiting to run for a pending frame.

final void
sendFrame(long timeNanos)

Send a frame for time timeNanos to all current callers of withFrameNanos.

@NonNull R
<R extends Object> withFrameNanos(
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

Inherited methods

From kotlin.coroutines.CoroutineContext
From kotlin.coroutines.CoroutineContext.Element
@NonNull R
<R extends Object> fold(
    @NonNull R initial,
    @NonNull Function2<@NonNull R, @NonNull CoroutineContext.Element, @NonNull R> operation
)
E
@NonNull CoroutineContext
From androidx.compose.runtime.MonotonicFrameClock

Public constructors

BroadcastFrameClock

public BroadcastFrameClock(Function0<Unit> onNewAwaiters)

Public methods

cancel

public final void cancel(@NonNull <Error class: unknown class> cancellationException)

Permanently cancel this BroadcastFrameClock and cancel all current and future awaiters with cancellationException.

getHasAwaiters

public final boolean getHasAwaiters()

true if there are any callers of withFrameNanos awaiting to run for a pending frame.

sendFrame

public final void sendFrame(long timeNanos)

Send a frame for time timeNanos to all current callers of withFrameNanos. The onFrame callback for each caller is invoked synchronously during the call to sendFrame.

withFrameNanos

public @NonNull R <R extends Object> withFrameNanos(
    @NonNull Function1<@NonNull Long, @NonNull R> onFrame
)

Suspends until a new frame is requested, immediately invokes onFrame with the frame time in nanoseconds in the calling context of frame dispatch, then resumes with the result from onFrame.

frameTimeNanos should be used when calculating animation time deltas from frame to frame as it may be normalized to the target time for the frame, not necessarily a direct, "now" value.

The time base of the value provided by withFrameNanos is implementation defined. Time values provided are strictly monotonically increasing; after a call to withFrameNanos completes it must not provide the same value again for a subsequent call.