|
|
|
@ -16,6 +16,8 @@
@@ -16,6 +16,8 @@
|
|
|
|
|
|
|
|
|
|
package io.element.android.libraries.matrix.impl.timeline |
|
|
|
|
|
|
|
|
|
import io.element.android.libraries.core.data.tryOrNull |
|
|
|
|
import io.element.android.libraries.matrix.impl.util.cancelAndDestroy |
|
|
|
|
import io.element.android.libraries.matrix.impl.util.destroyAll |
|
|
|
|
import io.element.android.libraries.matrix.impl.util.mxCallbackFlow |
|
|
|
|
import kotlinx.coroutines.channels.Channel |
|
|
|
@ -24,10 +26,10 @@ import kotlinx.coroutines.channels.trySendBlocking
@@ -24,10 +26,10 @@ import kotlinx.coroutines.channels.trySendBlocking
|
|
|
|
|
import kotlinx.coroutines.flow.Flow |
|
|
|
|
import kotlinx.coroutines.flow.buffer |
|
|
|
|
import kotlinx.coroutines.flow.callbackFlow |
|
|
|
|
import kotlinx.coroutines.flow.catch |
|
|
|
|
import org.matrix.rustcomponents.sdk.BackPaginationStatus |
|
|
|
|
import org.matrix.rustcomponents.sdk.BackPaginationStatusListener |
|
|
|
|
import org.matrix.rustcomponents.sdk.Room |
|
|
|
|
import org.matrix.rustcomponents.sdk.RoomTimelineListenerResult |
|
|
|
|
import org.matrix.rustcomponents.sdk.TimelineDiff |
|
|
|
|
import org.matrix.rustcomponents.sdk.TimelineItem |
|
|
|
|
import org.matrix.rustcomponents.sdk.TimelineListener |
|
|
|
@ -35,26 +37,26 @@ import timber.log.Timber
@@ -35,26 +37,26 @@ import timber.log.Timber
|
|
|
|
|
|
|
|
|
|
internal fun Room.timelineDiffFlow(onInitialList: suspend (List<TimelineItem>) -> Unit): Flow<TimelineDiff> = |
|
|
|
|
callbackFlow { |
|
|
|
|
val roomId = id() |
|
|
|
|
Timber.d("Open timelineDiffFlow for room $roomId") |
|
|
|
|
val listener = object : TimelineListener { |
|
|
|
|
override fun onUpdate(diff: TimelineDiff) { |
|
|
|
|
trySendBlocking(diff) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
var result: RoomTimelineListenerResult? = null |
|
|
|
|
val roomId = id() |
|
|
|
|
Timber.d("Open timelineDiffFlow for room $roomId") |
|
|
|
|
val result = addTimelineListener(listener) |
|
|
|
|
try { |
|
|
|
|
result = addTimelineListener(listener) |
|
|
|
|
onInitialList(result.items) |
|
|
|
|
} catch (exception: Exception) { |
|
|
|
|
Timber.d(exception, "Catch failure in timelineDiffFlow of room $roomId") |
|
|
|
|
} |
|
|
|
|
awaitClose { |
|
|
|
|
Timber.d("Close timelineDiffFlow for room $roomId") |
|
|
|
|
result?.itemsStream?.cancel() |
|
|
|
|
result?.itemsStream?.destroy() |
|
|
|
|
result?.items?.destroyAll() |
|
|
|
|
result.itemsStream.cancelAndDestroy() |
|
|
|
|
result.items.destroyAll() |
|
|
|
|
} |
|
|
|
|
}.catch { |
|
|
|
|
Timber.d(it, "timelineDiffFlow() failed") |
|
|
|
|
}.buffer(Channel.UNLIMITED) |
|
|
|
|
|
|
|
|
|
internal fun Room.backPaginationStatusFlow(): Flow<BackPaginationStatus> = |
|
|
|
@ -64,5 +66,7 @@ internal fun Room.backPaginationStatusFlow(): Flow<BackPaginationStatus> =
@@ -64,5 +66,7 @@ internal fun Room.backPaginationStatusFlow(): Flow<BackPaginationStatus> =
|
|
|
|
|
trySendBlocking(status) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
tryOrNull { |
|
|
|
|
subscribeToBackPaginationStatus(listener) |
|
|
|
|
} |
|
|
|
|
}.buffer(Channel.UNLIMITED) |
|
|
|
|