-
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.
- Loading branch information
1 parent
40fe5a2
commit a1162ea
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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) | ||
} | ||
} |