Add tryCount param to lambda

This commit is contained in:
Him188 2020-02-19 12:16:17 +08:00
parent 3b7a9b6d42
commit 4f7b266e03

View File

@ -15,12 +15,12 @@ expect fun Throwable.addSuppressed(e: Throwable)
@MiraiInternalAPI @MiraiInternalAPI
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
inline fun <R> tryNTimes(repeat: Int, block: () -> R): R { inline fun <R> tryNTimes(repeat: Int, block: (Int) -> R): R {
var lastException: Throwable? = null var lastException: Throwable? = null
repeat(repeat) { repeat(repeat) {
try { try {
return block() return block(it)
} catch (e: Throwable) { } catch (e: Throwable) {
if (lastException == null) { if (lastException == null) {
lastException = e lastException = e
@ -34,12 +34,12 @@ inline fun <R> tryNTimes(repeat: Int, block: () -> R): R {
@MiraiInternalAPI @MiraiInternalAPI
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
inline fun <R> tryNTimesOrNull(repeat: Int, block: () -> R): R? { inline fun <R> tryNTimesOrNull(repeat: Int, block: (Int) -> R): R? {
var lastException: Throwable? = null var lastException: Throwable? = null
repeat(repeat) { repeat(repeat) {
try { try {
return block() return block(it)
} catch (e: Throwable) { } catch (e: Throwable) {
if (lastException == null) { if (lastException == null) {
lastException = e lastException = e
@ -53,12 +53,12 @@ inline fun <R> tryNTimesOrNull(repeat: Int, block: () -> R): R? {
@MiraiInternalAPI @MiraiInternalAPI
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
inline fun <R> tryNTimesOrException(repeat: Int, block: () -> R): Throwable? { inline fun <R> tryNTimesOrException(repeat: Int, block: (Int) -> R): Throwable? {
var lastException: Throwable? = null var lastException: Throwable? = null
repeat(repeat) { repeat(repeat) {
try { try {
block() block(it)
return null return null
} catch (e: Throwable) { } catch (e: Throwable) {
if (lastException == null) { if (lastException == null) {