ObservableStateContainer

Decorator that adds observer capability to any StateContainer.

This wrapper allows any StateContainer implementation to support state change notifications through the observer pattern. Observers are notified only after successful state updates.

Parameters

T

The type of state being managed

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
open override val currentState: T

The current state of the container.

Link copied to clipboard

Returns true if any observers are registered.

Link copied to clipboard

Returns the number of registered observers.

Functions

Link copied to clipboard

Adds an observer to receive state change notifications.

Link copied to clipboard

Removes all observers.

Link copied to clipboard
fun <T> StateContainer<T>.compareAndSet(expectedState: T, newState: T): Boolean

Conditionally updates the state only if the current state matches the expected state.

Link copied to clipboard
fun <T> StateContainer<T>.getAndUpdate(updater: (T) -> T): T?

Updates the state and returns the previous state if successful, or null if failed.

Link copied to clipboard
inline fun <T, R : T> StateContainer<T>.getStateAs(): R?

Returns the current state cast to the specified type, or null if not of that type.

Link copied to clipboard
fun <T> StateContainer<T>.isState(predicate: (T) -> Boolean): Boolean

Checks if the state matches a predicate.

Link copied to clipboard
inline fun <T, R : T> StateContainer<T>.isStateType(): Boolean

Returns true if the current state is of the specified type.

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

Maps the current state to a different type.

Link copied to clipboard
fun observe(observer: (oldState: T, newState: T) -> Unit): Boolean

Adds an observer using a lambda for convenience.

Link copied to clipboard
fun observeChanges(observer: (oldState: T, newState: T) -> Unit): Boolean

Adds an observer that only reacts to actual state changes (not same state).

Link copied to clipboard
fun observeNewState(observer: (newState: T) -> Unit): Boolean

Adds an observer that only cares about the new state.

Link copied to clipboard
inline fun <R : T> observeStateType(crossinline observer: (oldState: T, newState: R) -> Unit): Boolean

Adds an observer that only reacts to specific state types.

Link copied to clipboard
fun observeWhen(predicate: (T, T) -> Boolean, observer: (oldState: T, newState: T) -> Unit): Boolean

Adds an observer that only reacts when a predicate is satisfied.

Link copied to clipboard

Removes an observer from receiving state change notifications.

Link copied to clipboard
fun <T> StateContainer<T>.trySet(newState: T): Boolean

Attempts to set the state to a specific value.

Link copied to clipboard
fun <T> StateContainer<T>.updateAndGet(updater: (T) -> T): T?

Updates the state and returns the new state if successful, or null if failed.

Link copied to clipboard
fun <T> StateContainer<T>.updateAtomic(maxRetries: Int = 10, updater: (T) -> T): StateUpdateResult<T>

Extension function for StateContainer to perform atomic updates.

Link copied to clipboard
open override fun updateState(updater: (T) -> T): StateUpdateResult<T>

Updates the state using the provided updater function.

Link copied to clipboard
suspend fun <T> StateContainer<T>.updateSuspending(updater: suspend (T) -> T): StateUpdateResult<T>

Transforms the state using a suspending function while maintaining thread safety.

Link copied to clipboard
open override fun validateState(state: T): ValidationResult

Validates the given state using the container's validator.

Link copied to clipboard
fun <T> StateContainer<T>.withState(action: (T) -> Unit)

Executes an action with the current state.