Skip to content

Commit

Permalink
refactor: extract lazySuspend
Browse files Browse the repository at this point in the history
  • Loading branch information
Handiwork committed Apr 19, 2024
1 parent 65da381 commit 0bf8512
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
9 changes: 9 additions & 0 deletions src/main/kotlin/plus/maa/backend/common/utils/KtExtensions.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
package plus.maa.backend.common.utils

inline fun <T> T?.requireNotNull(lazyMessage: () -> Any): T = requireNotNull(this, lazyMessage)

fun <T> lazySuspend(block: suspend () -> T): suspend () -> T {
var holder: T? = null
val access: suspend () -> T = {
val v = holder ?: block()
v.also { holder = it }
}
return access
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
import org.springframework.web.reactive.function.client.WebClient
import plus.maa.backend.common.utils.ArkLevelUtil
import plus.maa.backend.common.utils.awaitString
import plus.maa.backend.repository.entity.gamedata.ArkActivity
import plus.maa.backend.repository.entity.gamedata.ArkCharacter
Expand Down
20 changes: 4 additions & 16 deletions src/main/kotlin/plus/maa/backend/service/level/ArkLevelService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import org.springframework.data.domain.Pageable
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.util.DefaultUriBuilderFactory
import plus.maa.backend.common.utils.ArkLevelUtil
import plus.maa.backend.common.utils.awaitString
import plus.maa.backend.common.utils.converter.ArkLevelConverter
import plus.maa.backend.common.utils.lazySuspend
import plus.maa.backend.config.external.MaaCopilotProperties
import plus.maa.backend.controller.response.copilot.ArkLevelInfo
import plus.maa.backend.repository.ArkLevelRepository
Expand Down Expand Up @@ -51,8 +51,8 @@ class ArkLevelService(
private val webClient = webClientBuilder
.uriBuilderFactory(DefaultUriBuilderFactory().apply { encodingMode = DefaultUriBuilderFactory.EncodingMode.NONE })
.build()
private var dataHolder: ArkGameDataHolder? = null
private var levelParser: ArkLevelParserDelegate? = null
private val fetchDataHolder = lazySuspend { ArkGameDataHolder.fetch(webClient) }
private val fetchLevelParser = lazySuspend { ArkLevelParserDelegate(fetchDataHolder()) }

@get:Cacheable("arkLevelInfos")
val arkLevelInfos: List<ArkLevelInfo>
Expand Down Expand Up @@ -96,18 +96,6 @@ class ArkLevelService(
}
}

private suspend fun fetchDataHolder(): ArkGameDataHolder {
val holder = dataHolder ?: ArkGameDataHolder.fetch(webClient)
dataHolder = holder
return holder
}

private suspend fun fetchLevelParser(): ArkLevelParserDelegate {
val parser = levelParser ?: fetchDataHolder().run(::ArkLevelParserDelegate)
levelParser = parser
return parser
}

private suspend fun fetchTilePosGithubTreesToUpdate(commit: GithubCommit, path: String): List<GithubTree> {
var folder = getGithubTree(commit.sha)
val pathSegments = path.split("/").filter(String::isNotEmpty)
Expand Down Expand Up @@ -229,7 +217,7 @@ class ArkLevelService(
log.info { "[CRISIS-V2-OPEN-STATUS]危机合约开放状态更新完毕" }
}

suspend fun updateLevelsOfTypeInBatch(catOne: ArkLevelType, batchSize: Int = 1000, block: (page: ArkLevel) -> Unit) {
suspend fun updateLevelsOfTypeInBatch(catOne: ArkLevelType, batchSize: Int = 1000, block: (ArkLevel) -> Unit) {
var pageable = Pageable.ofSize(batchSize)
do {
val page = withContext(Dispatchers.IO) { arkLevelRepo.findAllByCatOne(catOne.display, pageable) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plus.maa.backend.common.utils
package plus.maa.backend.service.level

import java.util.regex.Pattern

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package plus.maa.backend.common.utils
package plus.maa.backend.service.level

import org.junit.jupiter.api.Test

Expand All @@ -12,7 +12,7 @@ class ArkLevelUtilTest {
"act11d0_ex08#f#" to "act11d0_s02",
"act11mini_03#f#" to "act11mini_04",
"act17side_01" to "act17side_s01_a",
"act17side_01_rep" to "act17side_02_perm"
"act17side_01_rep" to "act17side_02_perm",
)

val idsWithInfo = mapOf(
Expand All @@ -22,7 +22,7 @@ class ArkLevelUtilTest {
"act11d0_ex08#f#" to "act11d0",
"act11mini_03#f#" to "act11mini",
"act17side_01" to "act17side",
"act17side_01_rep" to "act17side"
"act17side_01_rep" to "act17side",
)

for ((key, value) in ids) {
Expand All @@ -37,4 +37,4 @@ class ArkLevelUtilTest {
}
}
}
}
}

0 comments on commit 0bf8512

Please sign in to comment.