Protect access to member timeline with a Mutex and clean the code.
This commit is contained in:
parent
a668e7e7b1
commit
243ce23055
1 changed files with 10 additions and 12 deletions
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue