
commonMain.io.lamart.lux.actions.toStreamActions.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lux-jvm Show documentation
Show all versions of lux-jvm Show documentation
Shiny state management for Kotlin, Android & iOS
package io.lamart.lux.actions
import io.lamart.lux.Async
import io.lamart.lux.Signal
import io.lamart.lux.Stream
import io.lamart.lux.focus.FocusedSetter
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.runningFold
val FocusedSetter<*, Stream>.toStreamActions: ActionsFactory>
get() = ActionsFactory(
onStart = { flow ->
flow
.runningFold, Stream>(Stream()) { stream, signal ->
when (signal) {
is Signal.Start -> Stream(state = Async.Executing(signal.input))
is Signal.Next -> stream.copy(result = signal.output)
is Signal.End -> signal.reason
?.let { stream.copy(state = Async.Failure(it)) }
?: stream.copy(state = Async.Success(Unit))
}
}
.onEach(::set)
},
onStop = { reason ->
modify { stream ->
when (stream.state) {
is Async.Executing -> stream.copy(state = Async.Failure(reason))
else -> stream
}
}
},
onReset = { set(Stream()) }
)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy