Skip to content

Commit

Permalink
支持强制获取 Source
Browse files Browse the repository at this point in the history
  • Loading branch information
tangwanchao committed Feb 5, 2021
1 parent 40fe5a2 commit a1162ea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions source/src/main/java/me/twc/source/ForceHttp.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@file:Suppress("unused")

package me.twc.source

import kotlinx.coroutines.delay

/**
* @author 唐万超
* @date 2021/02/05
*/

suspend fun <T> forceHttpSource(
block: suspend () -> ISource<T>
): Source<T> = forceHttp(runInIo = false, block)

suspend fun <T> forceHttpContext(
block: suspend () -> ISource<T>
): Source<T> = forceHttp(runInIo = true, block)


/**
* @param runInIo [true : 切换上下文到 IO],[false : 使用当前上下文]
*
* @param block 实际获取 ISource 的函数
*/
private suspend fun <T> forceHttp(
runInIo: Boolean,
block: suspend () -> ISource<T>
): Source<T> {
while (true) {
val source = if (runInIo) httpSource(block) else httpContext(block)
if (source is SuccessSource<T>) {
return source
}
delay(1_000L)
}
}

0 comments on commit a1162ea

Please sign in to comment.