Skip to content

Commit 9bf388e

Browse files
committed
Avoid spamming sync flow by checking item origin
1 parent 68c2aa8 commit 9bf388e

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import io.element.android.libraries.matrix.impl.media.map
4141
import io.element.android.libraries.matrix.impl.room.location.toInner
4242
import io.element.android.libraries.matrix.impl.timeline.RustMatrixTimeline
4343
import io.element.android.libraries.matrix.impl.timeline.backPaginationStatusFlow
44+
import io.element.android.libraries.matrix.impl.timeline.eventOrigin
4445
import io.element.android.libraries.matrix.impl.timeline.timelineDiffFlow
4546
import io.element.android.libraries.sessionstorage.api.SessionData
4647
import io.element.android.services.toolbox.api.systemclock.SystemClock
@@ -54,6 +55,7 @@ import kotlinx.coroutines.flow.launchIn
5455
import kotlinx.coroutines.flow.onEach
5556
import kotlinx.coroutines.launch
5657
import kotlinx.coroutines.withContext
58+
import org.matrix.rustcomponents.sdk.EventItemOrigin
5759
import org.matrix.rustcomponents.sdk.RequiredState
5860
import org.matrix.rustcomponents.sdk.Room
5961
import org.matrix.rustcomponents.sdk.RoomListItem
@@ -119,9 +121,11 @@ class RustMatrixRoom(
119121
roomCoroutineScope.launch(roomDispatcher) {
120122
innerRoom.timelineDiffFlow { initialList ->
121123
_timeline.postItems(initialList)
122-
}.onEach {
123-
_syncUpdateFlow.value = systemClock.epochMillis()
124-
_timeline.postDiff(it)
124+
}.onEach { diff ->
125+
if (diff.eventOrigin() == EventItemOrigin.SYNC) {
126+
_syncUpdateFlow.value = systemClock.epochMillis()
127+
}
128+
_timeline.postDiff(diff)
125129
}.launchIn(this)
126130

127131
innerRoom.backPaginationStatusFlow()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2023 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.element.android.libraries.matrix.impl.timeline
18+
19+
import org.matrix.rustcomponents.sdk.EventItemOrigin
20+
import org.matrix.rustcomponents.sdk.TimelineChange
21+
import org.matrix.rustcomponents.sdk.TimelineDiff
22+
import org.matrix.rustcomponents.sdk.TimelineItem
23+
24+
/**
25+
* Tries to get an event origin from the TimelineDiff.
26+
* If there is multiple events in the diff, uses the first one as it should be a good indicator.
27+
*/
28+
internal fun TimelineDiff.eventOrigin(): EventItemOrigin? {
29+
return when (change()) {
30+
TimelineChange.APPEND -> {
31+
append()?.first()?.eventOrigin()
32+
}
33+
TimelineChange.PUSH_BACK -> {
34+
pushBack()?.eventOrigin()
35+
}
36+
TimelineChange.PUSH_FRONT -> {
37+
pushFront()?.eventOrigin()
38+
}
39+
TimelineChange.SET -> {
40+
set()?.item?.eventOrigin()
41+
}
42+
TimelineChange.INSERT -> {
43+
insert()?.item?.eventOrigin()
44+
}
45+
TimelineChange.RESET -> {
46+
reset()?.first()?.eventOrigin()
47+
}
48+
else -> null
49+
}
50+
}
51+
52+
private fun TimelineItem.eventOrigin(): EventItemOrigin? {
53+
return asEvent()?.origin()
54+
}

0 commit comments

Comments
 (0)