Skip to content

Commit

Permalink
Merge pull request #100 from matheus-corregiari/hotfix/1.6.3
Browse files Browse the repository at this point in the history
[Hotfix] Adjust MirrorFlow
  • Loading branch information
matheus-corregiari authored Jul 9, 2024
2 parents 2bb967f + 1a9cd62 commit baf787d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<T> : StateFlow<DataResult<T>> {
Expand Down Expand Up @@ -93,6 +98,31 @@ open class ResponseFlow<T> : StateFlow<DataResult<T>> {
mirror = innerFlow.shareIn(scope, started, replay)
)

/**
*
*/
fun mirror(other: Flow<DataResult<T>>) = ResponseFlow(
value = value,
scope = scope,
dispatcher = transformDispatcher,
mirror = other
)

/**
*
*/
@Suppress("RemoveExplicitTypeArguments")
fun <R> mirror(other: Flow<R>, transform: (R) -> T) = ResponseFlow(
value = value,
scope = scope,
dispatcher = transformDispatcher,
mirror = other.map<R, DataResult<T>> {
withContext(transformDispatcher) {
dataResultSuccess(it.let(transform))
}
}.catch { emit(dataResultError(it)) }
)

/**
* @return A new instance of ObserveWrapper<T>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fun <T : Any> 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,
Expand All @@ -60,4 +60,4 @@ fun <T : Any> oneShotMichelangelo(
*/
@Experimental
fun <T : Any> oneShotLeonardo(id: String = "", quiet: Boolean = false, request: suspend () -> T) =
oneShotDonatello(id, quiet, request).execute().flow
oneShotDonatello(id, quiet, request).flow
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,20 @@ class MirrorFlow<RESULT : Any> : Strategy<RESULT>() {
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!")
Expand Down

0 comments on commit baf787d

Please sign in to comment.