BroadcastFrameClock


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(onNewAwaiters: (() -> Unit)?)
Cmn

Public functions

Unit
cancel(cancellationException: CancellationException)

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

Cmn
Unit
sendFrame(timeNanos: Long)

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

Cmn
open suspend R
<R : Any?> withFrameNanos(onFrame: (Long) -> R)

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.

Cmn

Public properties

Boolean

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

Cmn

Inherited functions

From kotlin.coroutines.CoroutineContext
open operator CoroutineContext
Cmn
From kotlin.coroutines.CoroutineContext.Element
open R
<R : Any?> fold(initial: R, operation: (CoroutineContext.Element, R) -> R)
Cmn
open operator E?
Cmn
open CoroutineContext
Cmn

Public constructors

BroadcastFrameClock

BroadcastFrameClock(onNewAwaiters: (() -> Unit)? = null)

Public functions

cancel

fun cancel(
    cancellationException: CancellationException = CancellationException("clock cancelled")
): Unit

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

sendFrame

fun sendFrame(timeNanos: Long): Unit

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

open suspend fun <R : Any?> withFrameNanos(onFrame: (Long) -> R): R

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.

Public properties

hasAwaiters

val hasAwaitersBoolean

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