Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotfix] Adjust MirrorFlow #100

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Hotfix] Adjust MirrorFlow
matheus-corregiari committed Jul 9, 2024
commit 1a9cd62e285ac5ca08b8c2ef2ce9d6f128c75ed2
Original file line number Diff line number Diff line change
@@ -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<T> : StateFlow<DataResult<T>> {
@@ -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>
*/
Original file line number Diff line number Diff line change
@@ -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,
@@ -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
@@ -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!")