diff options
author | Robert Jaros <rjaros@finn.pl> | 2020-05-20 18:26:12 +0200 |
---|---|---|
committer | Robert Jaros <rjaros@finn.pl> | 2020-05-20 18:26:12 +0200 |
commit | 18af38d4ac7bbc33a43115c99ed47a586acf6020 (patch) | |
tree | 0784c1a80e0f9194bb692d66894b84487ca1238a /kvision-modules/kvision-event-flow/src/main/kotlin/pl/treksoft/kvision | |
parent | 043ce3ebb6c5b0301b5f954b7b74942be404da2d (diff) | |
download | kvision-18af38d4ac7bbc33a43115c99ed47a586acf6020.tar.gz kvision-18af38d4ac7bbc33a43115c99ed47a586acf6020.tar.bz2 kvision-18af38d4ac7bbc33a43115c99ed47a586acf6020.zip |
Add stateFlow extension property for an ObservableState.
Diffstat (limited to 'kvision-modules/kvision-event-flow/src/main/kotlin/pl/treksoft/kvision')
-rw-r--r-- | kvision-modules/kvision-event-flow/src/main/kotlin/pl/treksoft/kvision/event/EventFlow.kt | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/kvision-modules/kvision-event-flow/src/main/kotlin/pl/treksoft/kvision/event/EventFlow.kt b/kvision-modules/kvision-event-flow/src/main/kotlin/pl/treksoft/kvision/event/EventFlow.kt index f9556e62..dc40beba 100644 --- a/kvision-modules/kvision-event-flow/src/main/kotlin/pl/treksoft/kvision/event/EventFlow.kt +++ b/kvision-modules/kvision-event-flow/src/main/kotlin/pl/treksoft/kvision/event/EventFlow.kt @@ -24,11 +24,17 @@ package pl.treksoft.kvision.event import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.callbackFlow import org.w3c.dom.events.Event import pl.treksoft.kvision.core.Widget import pl.treksoft.kvision.core.onEvent +import pl.treksoft.kvision.state.ObservableState +/** + * Extension property returning Flow<Pair<Widget, Event>> for a given event + */ @OptIn(ExperimentalCoroutinesApi::class) inline fun <reified T : Widget> T.eventFlow(event: String): Flow<Pair<T, Event>> = callbackFlow { val id = onEvent { @@ -41,6 +47,9 @@ inline fun <reified T : Widget> T.eventFlow(event: String): Flow<Pair<T, Event>> } } +/** + * Extension property returning Flow<Widget> for a click event. + */ @OptIn(ExperimentalCoroutinesApi::class) inline val <reified T : Widget> T.clickFlow: Flow<T> get() = callbackFlow { @@ -54,6 +63,9 @@ inline val <reified T : Widget> T.clickFlow: Flow<T> } } +/** + * Extension property returning Flow<Widget> for an input event. + */ @OptIn(ExperimentalCoroutinesApi::class) inline val <reified T : Widget> T.inputFlow: Flow<T> get() = callbackFlow { @@ -67,6 +79,9 @@ inline val <reified T : Widget> T.inputFlow: Flow<T> } } +/** + * Extension property returning Flow<Widget> for a change event. + */ @OptIn(ExperimentalCoroutinesApi::class) inline val <reified T : Widget> T.changeFlow: Flow<T> get() = callbackFlow { @@ -79,3 +94,12 @@ inline val <reified T : Widget> T.changeFlow: Flow<T> removeEventListener(id) } } + +/** + * Extension property returning StateFlow<S> for an ObservableState<S>. + */ +@OptIn(ExperimentalCoroutinesApi::class) +inline val <S> ObservableState<S>.stateFlow: StateFlow<S> + get() = MutableStateFlow(getState()).apply { + this@stateFlow.subscribe { this.value = it } + } |