StateSource

fun interface StateSource<out T>(source)

An externally owned source of revisioned state snapshots.

subscribe atomically registers an observer and captures the latest snapshot at one linearization point, then returns that snapshot together with its close handle. Concurrent subscriptions are supported. Notifications may occur on any thread and may race the return from subscribe. Every source observation after the linearization point is delivered to the new observer without omission while the subscription remains open, including observations published during the return race. A conforming source serializes callbacks for one subscription so they never overlap or re-enter, and gives each callback a strictly greater revision than the previous callback. Equal revisions within one source identify the same logical observation for every subscription. An observer must return normally. Throwing is a subscriber contract violation; the exception may escape on the delivery thread and all subscription-state guarantees after that violation are unspecified. A successful StateSubscription.close prevents the source from starting another callback for that subscription. A notification whose delivery races StateSubscription.close may be delivered or suppressed. The source owns its values and subscription resources; the subscriber owns the returned handle.

Type Parameters

T

the observed value type.

Inheritors

Functions

Link copied to clipboard
fun <T, R> StateSource<T>.map(transform: (T) -> R): StateSource<R>

Creates a lazy, caller-owned projection of this source, supporting nullable values and chained projections. Construction does not subscribe or evaluate transform. Keep the returned identity when consumers share a projection. The transformation must be deterministic, side-effect-free, and return normally; ordinary subscriptions may transform on any notifying thread. Ordinary subscriptions preserve every revision, including equal results. Retained UI consumers instead share the upstream subscription, transform a frame-committed input, and suppress dependent UI work when the transformed result is equal by ==. No source history or global cache is retained. Heavy work and I/O belong outside the transformation.

Link copied to clipboard
abstract fun subscribe(observer: (StateSnapshot<T>) -> Unit): StateSubscription<T>

Establishes one observation and returns its initial value and close handle.