diff --git a/toolkit/event-observer/src/main/kotlin/br/com/arch/toolkit/flow/ResponseFlow.kt b/toolkit/event-observer/src/main/kotlin/br/com/arch/toolkit/flow/ResponseFlow.kt index 9bd12d80..60a66f1b 100644 --- a/toolkit/event-observer/src/main/kotlin/br/com/arch/toolkit/flow/ResponseFlow.kt +++ b/toolkit/event-observer/src/main/kotlin/br/com/arch/toolkit/flow/ResponseFlow.kt @@ -5,7 +5,9 @@ import br.com.arch.toolkit.annotation.Experimental import br.com.arch.toolkit.result.DataResult import br.com.arch.toolkit.result.DataResultStatus import br.com.arch.toolkit.result.ObserveWrapper +import br.com.arch.toolkit.util.dataResultError import br.com.arch.toolkit.util.dataResultNone +import br.com.arch.toolkit.util.dataResultSuccess import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -15,8 +17,11 @@ import kotlinx.coroutines.flow.FlowCollector import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext @Experimental open class ResponseFlow : StateFlow> { @@ -93,6 +98,31 @@ open class ResponseFlow : StateFlow> { mirror = innerFlow.shareIn(scope, started, replay) ) + /** + * + */ + fun mirror(other: Flow>) = ResponseFlow( + value = value, + scope = scope, + dispatcher = transformDispatcher, + mirror = other + ) + + /** + * + */ + @Suppress("RemoveExplicitTypeArguments") + fun mirror(other: Flow, transform: (R) -> T) = ResponseFlow( + value = value, + scope = scope, + dispatcher = transformDispatcher, + mirror = other.map> { + withContext(transformDispatcher) { + dataResultSuccess(it.let(transform)) + } + }.catch { emit(dataResultError(it)) } + ) + /** * @return A new instance of ObserveWrapper */ diff --git a/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/_starterMethod.kt b/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/_starterMethod.kt index e86b8acd..f6a8837c 100644 --- a/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/_starterMethod.kt +++ b/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/_starterMethod.kt @@ -46,7 +46,7 @@ fun oneShotMichelangelo( id: String = "", quiet: Boolean = false, request: suspend () -> T -) = oneShotDonatello(id, quiet, request).execute().liveData +) = oneShotDonatello(id, quiet, request).liveData /** * Method that creates Splinter instance and execute it, @@ -60,4 +60,4 @@ fun oneShotMichelangelo( */ @Experimental fun oneShotLeonardo(id: String = "", quiet: Boolean = false, request: suspend () -> T) = - oneShotDonatello(id, quiet, request).execute().flow + oneShotDonatello(id, quiet, request).flow diff --git a/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/strategy/MirrorFlow.kt b/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/strategy/MirrorFlow.kt index a4de7035..b08e968e 100644 --- a/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/strategy/MirrorFlow.kt +++ b/toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/strategy/MirrorFlow.kt @@ -47,10 +47,20 @@ class MirrorFlow : Strategy() { requireNotNull(config.flow) { "Flow value mist be set!" }.invoke() .catch { error -> onError.invoke(error) } .collect { data -> - executor.logInfo("\t[MirrorFlow] Received new data, data: $data") - executor.logInfo("\t[MirrorFlow] Emit - Loading Data! - $data") - if (config.emitOnlyDistinct && executor.get() != dataResultLoading(data)) { - collector.emitLoading(data) + executor.logInfo("\t[MirrorFlow] Received new data!") + when { + config.emitOnlyDistinct && executor.get() != dataResultLoading(data) -> { + executor.logInfo("\t[MirrorFlow] Emit - New Loading Data! - $data") + collector.emitLoading(data) + } + + config.emitOnlyDistinct -> + executor.logInfo("\t[MirrorFlow] Value is equal to the actual, skipping it!") + + else -> { + executor.logInfo("\t[MirrorFlow] Emit - Loading Data! - $data") + collector.emitLoading(data) + } } } executor.logInfo("\t[MirrorFlow] Finished flow!")