Костыль для подавления цикла синхронизации

This commit is contained in:
2026-05-22 00:37:00 +03:00
parent 07d54b5996
commit 35ba6dd377
2 changed files with 46 additions and 18 deletions

View File

@@ -9,6 +9,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
@@ -29,7 +30,12 @@ class StorageSyncBootstrap @Inject constructor(
fun start() {
scheduler.ensureScheduled()
scope.launch {
vaultsManager.allStorages.collectLatest { storages ->
combine(
vaultsManager.allStorages,
vaultsManager.unlockManager.openedStorages,
) { rootStorages, opened ->
(rootStorages + opened.values).distinctBy { it.uuid }
}.collectLatest { storages ->
if (storages.isEmpty()) {
return@collectLatest
}
@@ -52,18 +58,23 @@ class StorageSyncBootstrap @Inject constructor(
)
}
merge(*triggers.toTypedArray())
.debounce(DEBOUNCE_AFTER_CHANGE_MS)
.filter { shouldScheduleDebounceSync() }
.debounce(RunStorageSyncUseCase.DEBOUNCE_AFTER_CHANGE_MS)
.collect {
if (syncRunner.syncRunning.value) {
return@collect
}
syncRunner.enqueue(StorageSyncTriggerReason.Debounce)
}
}
}
}
private companion object {
private const val DEBOUNCE_AFTER_CHANGE_MS = 60_000L
/**
* Игнорировать события во время sync и сразу после него: запись файлов при sync
* (в т.ч. через [EncryptedStorageAccessor]) иначе снова запускает debounce через 60 с.
*/
private fun shouldScheduleDebounceSync(): Boolean {
if (syncRunner.syncRunning.value) {
return false
}
return System.currentTimeMillis() >= syncRunner.debounceSuppressUntilMs.value
}
}