Browse Source

Merge pull request #834 from vector-im/feature/fga/avoid_multiple_sync_loops

Feature/fga/avoid multiple sync loops
pull/835/head
ganfra 1 year ago committed by GitHub
parent
commit
0e47fa870e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt

17
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt

@ -24,23 +24,30 @@ import kotlinx.coroutines.flow.SharingStarted @@ -24,23 +24,30 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn
import org.matrix.rustcomponents.sdk.RoomListService
import org.matrix.rustcomponents.sdk.RoomListServiceState
import timber.log.Timber
import java.util.concurrent.atomic.AtomicBoolean
class RustSyncService(
private val roomListService: RoomListService,
sessionCoroutineScope: CoroutineScope
) : SyncService {
private val isSyncing = AtomicBoolean(false)
override fun startSync() = runCatching {
if (!roomListService.isSyncing()) {
if (isSyncing.compareAndSet(false, true)) {
Timber.v("Start sync")
roomListService.sync()
}
}
override fun stopSync() = runCatching {
if (roomListService.isSyncing()) {
if (isSyncing.compareAndSet(true, false)) {
Timber.v("Stop sync")
roomListService.stopSync()
}
}
@ -49,6 +56,10 @@ class RustSyncService( @@ -49,6 +56,10 @@ class RustSyncService(
roomListService
.stateFlow()
.map(RoomListServiceState::toSyncState)
.onEach { state ->
Timber.v("Sync state=$state")
isSyncing.set(state == SyncState.Syncing)
}
.distinctUntilChanged()
.stateIn(sessionCoroutineScope, SharingStarted.WhileSubscribed(), SyncState.Idle)
.stateIn(sessionCoroutineScope, SharingStarted.Eagerly, SyncState.Idle)
}

Loading…
Cancel
Save