From a6ce62f1c3841a497d2c9a630a322733f103410e Mon Sep 17 00:00:00 2001 From: Karlatemp Date: Thu, 5 Nov 2020 23:26:54 +0800 Subject: [PATCH] Resolve duplicate save --- backend/mirai-console/src/data/AutoSavePluginData.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/backend/mirai-console/src/data/AutoSavePluginData.kt b/backend/mirai-console/src/data/AutoSavePluginData.kt index 63bbd64d2..1d21c29c3 100644 --- a/backend/mirai-console/src/data/AutoSavePluginData.kt +++ b/backend/mirai-console/src/data/AutoSavePluginData.kt @@ -91,7 +91,7 @@ public open class AutoSavePluginData private constructor( internal var lastAutoSaveJob_: Job? = null @JvmField - internal val currentFirstStartTime_ = atomic(0L) + internal val currentFirstStartTime_ = atomic(MAGIC_NUMBER_CFST_INIT) /** * @return `true` 时, 一段时间后, 即使无属性改变, 也会进行保存. @@ -103,7 +103,7 @@ public open class AutoSavePluginData private constructor( private val updaterBlock: suspend CoroutineScope.() -> Unit = l@{ if (::storage_.isInitialized) { - currentFirstStartTime_.updateWhen({ it == 0L }, { currentTimeMillis }) + currentFirstStartTime_.updateWhen({ it == MAGIC_NUMBER_CFST_INIT }, { currentTimeMillis }) try { delay(autoSaveIntervalMillis_.first.coerceAtLeast(1000)) // for safety } catch (e: CancellationException) { @@ -117,8 +117,8 @@ public open class AutoSavePluginData private constructor( } } else { if (currentFirstStartTime_.updateWhen( - { currentTimeMillis - it >= autoSaveIntervalMillis_.last }, - { 0 }) + { it != MAGIC_NUMBER_CFST_INIT && currentTimeMillis - it >= autoSaveIntervalMillis_.last }, + { MAGIC_NUMBER_CFST_INIT }) ) { withContext(owner_.coroutineContext) { doSave() @@ -168,4 +168,6 @@ internal inline fun MiraiLogger.runCatchingLog(message: (Throwable) -> Strin }.onFailure { error(message(it), it) }.getOrNull() -} \ No newline at end of file +} + +private const val MAGIC_NUMBER_CFST_INIT: Long = Long.MAX_VALUE