Skip to content

Commit 785c4a5

Browse files
committed
Sync: should avoid having multiple sync loops
1 parent 3e69985 commit 785c4a5

File tree

1 file changed

+16
-3
lines changed
  • libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync

1 file changed

+16
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,30 @@ import kotlinx.coroutines.flow.SharingStarted
2424
import kotlinx.coroutines.flow.StateFlow
2525
import kotlinx.coroutines.flow.distinctUntilChanged
2626
import kotlinx.coroutines.flow.map
27+
import kotlinx.coroutines.flow.onEach
2728
import kotlinx.coroutines.flow.stateIn
2829
import org.matrix.rustcomponents.sdk.RoomListService
2930
import org.matrix.rustcomponents.sdk.RoomListServiceState
31+
import timber.log.Timber
32+
import java.util.concurrent.atomic.AtomicBoolean
3033

3134
class RustSyncService(
3235
private val roomListService: RoomListService,
3336
sessionCoroutineScope: CoroutineScope
3437
) : SyncService {
3538

39+
private val isSyncing = AtomicBoolean(false)
40+
3641
override fun startSync() = runCatching {
37-
if (!roomListService.isSyncing()) {
42+
if (isSyncing.compareAndSet(false, true)) {
43+
Timber.v("Start sync")
3844
roomListService.sync()
3945
}
4046
}
4147

4248
override fun stopSync() = runCatching {
43-
if (roomListService.isSyncing()) {
49+
if (isSyncing.compareAndSet(true, false)) {
50+
Timber.v("Stop sync")
4451
roomListService.stopSync()
4552
}
4653
}
@@ -49,6 +56,12 @@ class RustSyncService(
4956
roomListService
5057
.stateFlow()
5158
.map(RoomListServiceState::toSyncState)
59+
.onEach { state ->
60+
Timber.v("Sync state=$state")
61+
if (state == SyncState.InError || state == SyncState.Terminated) {
62+
isSyncing.set(false)
63+
}
64+
}
5265
.distinctUntilChanged()
53-
.stateIn(sessionCoroutineScope, SharingStarted.WhileSubscribed(), SyncState.Idle)
66+
.stateIn(sessionCoroutineScope, SharingStarted.Eagerly, SyncState.Idle)
5467
}

0 commit comments

Comments
 (0)