SafeFetchFlow

class SafeFetchFlow<T : Any> : AbstractFlow<Fetch<T>> , Flow<Fetch<T>>

AbstractFlow implementation for FetchFlow.

Use fetch to provide an instance of this class.

// Typealias for Flow<Fetch<Int>>
val a: FetchFlow<Int> = fetch { 4 }

// Will throw TimeoutCancellationException if [block] is not emitted within 4 seconds
val b: FetchFlow<Int> = fetch(timeout = 4.seconds) { 4 }

// The recover param can map unexpected errors to a Fetch state.
val c: FetchFlow<Int> = fetch(timeout = 4.seconds, recover = { Prefetch }) { 4 }
  • Prefetch is used for initial states, and is not emitted by the FetchFlow.

  • Fetching is emitted automatically before block is executed.

  • Finished automatically encapsulates the result of block.

Return

FetchFlow -- a Flow that encapsulates the Fetch behaviour.

Parameters

timeout

The duration to wait withTimeout for the fetch block to complete once.

recover

The transformation to apply to any non-fatal Throwable that is caught.

block

The block of code to execute.

See also

Functions

Link copied to clipboard
inline fun <In, Out> ResultFlow<In>.andThen(crossinline success: suspend (In) -> Out): Flow<Result<Out>>

Returns a flow containing the output of applying KotlinResult.andThen to each value of the original flow.

inline fun <T> ResultFlow<T>.andThen(crossinline predicate: (T) -> Boolean, crossinline success: suspend (T) -> T): Flow<Result<T>>

Returns a flow containing the Out of applying KotlinResult.andIf to each value of the original flow.

Link copied to clipboard

TODO: Needs work / doc-comments / testing

Link copied to clipboard
suspend override fun collect(collector: FlowCollector<Fetch<T>>)
Link copied to clipboard
open suspend override fun collectSafely(collector: FetchCollector<T>)
Link copied to clipboard

Returns a flow containing only failed values of the original flow.

Link copied to clipboard
fun <T : Any> FetchFlow<T>.filterIsNotEmpty(): Flow<T>

Filter a FetchFlow by isNotEmpty, and unwrap the value if it exists.

Link copied to clipboard

TODO: Needs work / doc-comments / testing

Link copied to clipboard

TODO: Needs work / doc-comments / testing

Link copied to clipboard
fun <T> ResultFlow<T>.filterSuccess(): Flow<T>

Returns a flow containing only successful values of the original flow.

Link copied to clipboard
inline fun <In : Out, Out : Any> FetchFlow<In>.flatMapFetching(crossinline transform: suspend Fetching<In>.() -> Fetch<Out>): FetchFlow<Out>

Map a fetch flow to flatMapFetching each emission.

Link copied to clipboard
inline fun <In : Out, Out : Any> FetchFlow<In>.flatMapFinished(crossinline transform: suspend Finished<In>.() -> Fetch<Out>): FetchFlow<Out>

Map a fetch flow to flatMapFinished each emission.

Link copied to clipboard
inline fun <In : Out, Out : Any> FetchFlow<In>.flatMapPrefetch(crossinline transform: suspend Prefetch.() -> Fetch<Out>): FetchFlow<Out>

Map a fetch flow to flatMapPrefetch each emission.

Link copied to clipboard

Map a fetch flow to flatten each emission.

Returns a flow that KotlinResult.flatten a nested KotlinResult for each value of the original flow.

Link copied to clipboard
inline fun <Data : Any, Output> FetchFlow<Data>.fold(crossinline prefetch: suspend Prefetch.() -> Output, crossinline fetching: suspend Fetching<Data>.() -> Output, crossinline finished: suspend Finished<Data>.() -> Output): Flow<Output>

Fold a FetchFlow into a Flow of Output.

Link copied to clipboard
inline fun <Ok : Any, Error : Any, Output> OutcomeFlow<Ok, Error>.foldOutcome(crossinline success: suspend (Ok) -> Output, crossinline failure: suspend (Error) -> Output): Flow<Output>

TODO: Needs work / doc-comments / testing

Link copied to clipboard
inline fun <In, Out> ResultFlow<In>.foldResult(crossinline success: suspend (In) -> Out, crossinline failure: suspend (Throwable) -> Out): Flow<Out>

Returns a flow containing the output of applying KotlinResult.fold to each value of the original flow.

Link copied to clipboard
inline fun <Ok : Any, ErrorIn : Any, ErrorOut : Any> OutcomeFlow<Ok, ErrorIn>.mapFailure(crossinline transform: suspend (ErrorIn) -> ErrorOut): OutcomeFlow<Ok, ErrorOut>

TODO: Needs work / doc-comments / testing

inline fun <T> ResultFlow<T>.mapFailure(crossinline failure: suspend (Throwable) -> Throwable): Flow<Result<T>>

Returns a flow containing the output of applying KotlinResult.mapFailure to each value of the original flow.

Link copied to clipboard
inline fun <In : Out, Out : Any> FetchFlow<In>.mapFetching(crossinline transform: suspend Fetching<In>.() -> Out?): FetchFlow<Out>

Map a fetch flow to mapFetching each emission.

Link copied to clipboard
inline fun <In : Out, Out : Any> FetchFlow<In>.mapFinished(crossinline transform: suspend Finished<In>.() -> Out): FetchFlow<Out>

Map a fetch flow to mapFinished each emission.

Link copied to clipboard
inline fun <In : Any, Out : Any, Error : Any> OutcomeFlow<In, Error>.mapSuccess(crossinline transform: suspend (In) -> Out): OutcomeFlow<Out, Error>

TODO: Needs work / doc-comments / testing

inline fun <In, Out> ResultFlow<In>.mapSuccess(crossinline success: suspend (In) -> Out): Flow<Result<Out>>

Returns a flow containing the output of applying KotlinResult.map to each value of the original flow.

Link copied to clipboard
inline fun <In : Out, Out : Any> FetchFlow<In>.mapToCache(crossinline transform: suspend Finished<In>.() -> Out = { result }): FetchFlow<Out>

Map a fetch flow to mapToCache each emission.

Link copied to clipboard
Link copied to clipboard

Map a fetch flow to maybe each emission.

Link copied to clipboard
Link copied to clipboard
inline fun <T : Any> FetchFlow<T>.onEach(crossinline prefetch: suspend Prefetch.() -> Unit = {}, crossinline fetching: suspend Fetching<T>.() -> Unit = {}, crossinline finished: suspend Finished<T>.() -> Unit = {}): FetchFlow<T>

On each emission, execute a lambda according to Fetch state.

Link copied to clipboard
inline fun <Ok : Any, Error : Any> OutcomeFlow<Ok, Error>.onEachFailure(crossinline block: suspend (Error) -> Unit): OutcomeFlow<Ok, Error>

TODO: Needs work / doc-comments / testing

inline fun <T> ResultFlow<T>.onEachFailure(crossinline action: suspend (Throwable) -> Unit): ResultFlow<T>

Returns a flow that invokes the given action if it is a failure before each value of the upstream flow is emitted downstream.

Link copied to clipboard
inline fun <T> ResultFlow<T>.onEachResult(crossinline success: suspend (T) -> Unit, crossinline failure: suspend (Throwable) -> Unit): ResultFlow<T>

Returns a flow that invokes the given actions for each success and failure before each value of the upstream flow is emitted downstream.

Link copied to clipboard
inline fun <Ok : Any, Error : Any> OutcomeFlow<Ok, Error>.onEachSuccess(crossinline block: suspend (Ok) -> Unit): OutcomeFlow<Ok, Error>

TODO: Needs work / doc-comments / testing

inline fun <T> ResultFlow<T>.onEachSuccess(crossinline action: suspend (T) -> Unit): ResultFlow<T>

Returns a flow that invokes the given action if it is a success before each value of the upstream flow is emitted downstream.

Link copied to clipboard
inline fun <T> ResultFlow<T>.tryRecover(crossinline failure: suspend (Throwable) -> T): Flow<Result<T>>

Returns a flow containing the output of applying KotlinResult.tryRecover to each value of the original flow.