Skip to content

Instantly share code, notes, and snippets.

View RoryKelly's full-sized avatar

Rory Kelly RoryKelly

View GitHub Profile
@RoryKelly
RoryKelly / StateMachine.kt
Created December 9, 2020 22:09
Kotlin Flow State Machine
fun <T> Flow<T>.stateMachine(configuration: StateChanges<T>.() -> Unit): Flow<StateChanges<T>> = flow {
var oldState: T? = null
distinctUntilChanged().collect { value ->
emit(StateChanges(oldState, value))
oldState = value
}
}.onEach {
configuration(it)
}