-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from matheus-corregiari/release/1.6.0
[Release] 1.6.0
- Loading branch information
Showing
6 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
toolkit/splinter/src/main/kotlin/br/com/arch/toolkit/splinter/strategy/MirrorFlow.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package br.com.arch.toolkit.splinter.strategy | ||
|
||
import androidx.annotation.WorkerThread | ||
import br.com.arch.toolkit.result.DataResult | ||
import br.com.arch.toolkit.result.DataResultStatus | ||
import br.com.arch.toolkit.splinter.Splinter | ||
import br.com.arch.toolkit.splinter.extension.emitData | ||
import br.com.arch.toolkit.splinter.extension.emitError | ||
import br.com.arch.toolkit.splinter.extension.emitLoading | ||
import br.com.arch.toolkit.splinter.extension.invokeCatching | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.FlowCollector | ||
import kotlinx.coroutines.flow.catch | ||
|
||
/** | ||
* Strategy to mirror results from another flow using the DataResult | ||
* | ||
* @see Strategy | ||
*/ | ||
class MirrorFlow<RESULT : Any> : Strategy<RESULT>() { | ||
|
||
/** | ||
* Block that configure how this Strategy will work ^^ | ||
* | ||
* @see MirrorFlow.Config | ||
*/ | ||
private val config = Config() | ||
fun config(config: Config.() -> Unit) = apply { this.config.run(config) } | ||
|
||
@WorkerThread | ||
override suspend fun execute( | ||
collector: FlowCollector<DataResult<RESULT>>, | ||
executor: Splinter<RESULT> | ||
) { | ||
val onError: suspend (Throwable) -> Unit = { error -> | ||
if (executor.status != DataResultStatus.SUCCESS) { | ||
executor.logError("\t[MirrorFlow] Emit - Error! - $error") | ||
flowError(error, collector, executor) | ||
} else { | ||
executor.logWarning("\t[MirrorFlow] Got some error but I did not emit it - $error") | ||
} | ||
} | ||
|
||
kotlin.runCatching { | ||
collector.emitLoading(executor.data) | ||
requireNotNull(config.flow) { "Flow value mist be set!" }.invoke() | ||
.catch { error -> onError.invoke(error) } | ||
.collect { data -> | ||
executor.logInfo("\t[MirrorFlow] Executed with success, data: $data") | ||
executor.logInfo("\t[MirrorFlow] Emit - Success Data! - $data") | ||
collector.emitLoading(data) | ||
} | ||
if (executor.error == null) executor.data?.let { collector.emitData(it) } | ||
}.onFailure { error -> onError.invoke(error) } | ||
} | ||
|
||
override suspend fun flowError( | ||
error: Throwable, | ||
collector: FlowCollector<DataResult<RESULT>>, | ||
executor: Splinter<RESULT> | ||
) = collector.emitError( | ||
error = config.mapError?.invoke(error) ?: error, | ||
data = executor.data ?: config.fallback?.invokeCatching(error)?.getOrNull() | ||
) | ||
|
||
/** | ||
* | ||
*/ | ||
inner class Config internal constructor() { | ||
internal var mapError: (suspend (Throwable) -> Throwable)? = null | ||
internal var fallback: (suspend (Throwable) -> RESULT)? = null | ||
internal var flow: (suspend () -> Flow<RESULT>)? = null | ||
|
||
fun flow(flow: suspend () -> Flow<RESULT>) = apply { this.flow = flow } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters