Resolve duplicate save

This commit is contained in:
Karlatemp 2020-11-05 23:26:54 +08:00
parent d248188fac
commit a6ce62f1c3
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8

View File

@ -91,7 +91,7 @@ public open class AutoSavePluginData private constructor(
internal var lastAutoSaveJob_: Job? = null internal var lastAutoSaveJob_: Job? = null
@JvmField @JvmField
internal val currentFirstStartTime_ = atomic(0L) internal val currentFirstStartTime_ = atomic(MAGIC_NUMBER_CFST_INIT)
/** /**
* @return `true` , 一段时间后, 即使无属性改变, 也会进行保存. * @return `true` , 一段时间后, 即使无属性改变, 也会进行保存.
@ -103,7 +103,7 @@ public open class AutoSavePluginData private constructor(
private val updaterBlock: suspend CoroutineScope.() -> Unit = l@{ private val updaterBlock: suspend CoroutineScope.() -> Unit = l@{
if (::storage_.isInitialized) { if (::storage_.isInitialized) {
currentFirstStartTime_.updateWhen({ it == 0L }, { currentTimeMillis }) currentFirstStartTime_.updateWhen({ it == MAGIC_NUMBER_CFST_INIT }, { currentTimeMillis })
try { try {
delay(autoSaveIntervalMillis_.first.coerceAtLeast(1000)) // for safety delay(autoSaveIntervalMillis_.first.coerceAtLeast(1000)) // for safety
} catch (e: CancellationException) { } catch (e: CancellationException) {
@ -117,8 +117,8 @@ public open class AutoSavePluginData private constructor(
} }
} else { } else {
if (currentFirstStartTime_.updateWhen( if (currentFirstStartTime_.updateWhen(
{ currentTimeMillis - it >= autoSaveIntervalMillis_.last }, { it != MAGIC_NUMBER_CFST_INIT && currentTimeMillis - it >= autoSaveIntervalMillis_.last },
{ 0 }) { MAGIC_NUMBER_CFST_INIT })
) { ) {
withContext(owner_.coroutineContext) { withContext(owner_.coroutineContext) {
doSave() doSave()
@ -169,3 +169,5 @@ internal inline fun <R> MiraiLogger.runCatchingLog(message: (Throwable) -> Strin
error(message(it), it) error(message(it), it)
}.getOrNull() }.getOrNull()
} }
private const val MAGIC_NUMBER_CFST_INIT: Long = Long.MAX_VALUE