mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 21:32:34 +00:00
Protect access to member timeline with a Mutex and clean the code.
This commit is contained in:
+10
-12
@@ -18,6 +18,8 @@ import io.element.android.libraries.mediaviewer.impl.model.GroupedMediaItems
|
|||||||
import io.element.android.libraries.mediaviewer.impl.model.MediaItem
|
import io.element.android.libraries.mediaviewer.impl.model.MediaItem
|
||||||
import io.element.android.libraries.mediaviewer.impl.model.hasEvent
|
import io.element.android.libraries.mediaviewer.impl.model.hasEvent
|
||||||
import kotlinx.collections.immutable.persistentListOf
|
import kotlinx.collections.immutable.persistentListOf
|
||||||
|
import kotlinx.coroutines.sync.Mutex
|
||||||
|
import kotlinx.coroutines.sync.withLock
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
interface MediaTimeline {
|
interface MediaTimeline {
|
||||||
@@ -37,19 +39,15 @@ class LiveMediaTimeline @Inject constructor(
|
|||||||
private val room: MatrixRoom,
|
private val room: MatrixRoom,
|
||||||
) : MediaTimeline {
|
) : MediaTimeline {
|
||||||
private var timeline: Timeline? = null
|
private var timeline: Timeline? = null
|
||||||
override suspend fun getTimeline(): Result<Timeline> {
|
private val mutex = Mutex()
|
||||||
return if (timeline == null) {
|
|
||||||
room.mediaTimeline(null).fold(
|
override suspend fun getTimeline(): Result<Timeline> = mutex.withLock {
|
||||||
{
|
val currentTimeline = timeline
|
||||||
timeline = it
|
if (currentTimeline == null) {
|
||||||
Result.success(it)
|
room.mediaTimeline(null)
|
||||||
},
|
.onSuccess { timeline = it }
|
||||||
{
|
|
||||||
Result.failure(it)
|
|
||||||
},
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
Result.success(timeline!!)
|
Result.success(currentTimeline)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user