-
Notifications
You must be signed in to change notification settings - Fork 0
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 #14 from respawn-app/1.3.2
1.3.2
- Loading branch information
Showing
22 changed files
with
334 additions
and
466 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
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
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
44 changes: 44 additions & 0 deletions
44
coroutines/src/commonMain/kotlin/pro/respawn/kmmutils/coroutines/RetryFlow.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,44 @@ | ||
package pro.respawn.kmmutils.coroutines | ||
|
||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
|
||
/** | ||
* A [Flow] instance that can be retried by calling [retry]. | ||
* Whatever block or flow emission was before will be re-evaluated (re-created) again. | ||
*/ | ||
public interface RetryFlow<T> : Flow<T> { | ||
|
||
/** | ||
* Retry the invocation of this flow. The flow that originally was used with this wrapper will be recreated | ||
*/ | ||
public fun retry() | ||
} | ||
|
||
/** | ||
* Creates a new [RetryFlow] from [flow] builder | ||
*/ | ||
public fun <T> retryFlow(flow: suspend () -> Flow<T>): RetryFlow<T> = RetryFlowImpl(flow) | ||
|
||
/** | ||
* Creates a new [RetryFlow] from this [call] function, evaluated as a cold flow | ||
*/ | ||
public inline fun <T> retry(crossinline call: suspend () -> T): RetryFlow<T> = RetryFlowImpl({ flow { emit(call()) } }) | ||
|
||
@PublishedApi | ||
internal class RetryFlowImpl<T>( | ||
producer: suspend () -> Flow<T>, | ||
private val delegate: Channel<Unit> = Channel(Channel.CONFLATED), | ||
) : RetryFlow<T>, Flow<T> by delegate.receiveAsFlow().flatMapLatest(transform = { producer() }) { | ||
|
||
init { | ||
retry() | ||
} | ||
|
||
override fun retry() { | ||
delegate.trySend(Unit) | ||
} | ||
} |
Oops, something went wrong.