Add constraint on T. It has to extend VectorViewEvents

This commit is contained in:
Benoit Marty 2023-01-09 16:39:12 +01:00
parent ffba01cabd
commit cceb1cd66c

View file

@ -16,16 +16,17 @@
package im.vector.app.core.utils
import im.vector.app.core.platform.VectorViewEvents
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.transform
import java.util.concurrent.CopyOnWriteArraySet
interface SharedEvents<out T> {
interface SharedEvents<out T : VectorViewEvents> {
fun stream(consumerId: String): Flow<T>
}
class EventQueue<T>(capacity: Int) : SharedEvents<T> {
class EventQueue<T : VectorViewEvents>(capacity: Int) : SharedEvents<T> {
private val innerQueue = MutableSharedFlow<OneTimeEvent<T>>(replay = capacity)
@ -42,7 +43,7 @@ class EventQueue<T>(capacity: Int) : SharedEvents<T> {
*
* Keeps track of who has already handled its content.
*/
private class OneTimeEvent<out T>(private val content: T) {
private class OneTimeEvent<out T : VectorViewEvents>(private val content: T) {
private val handlers = CopyOnWriteArraySet<String>()
@ -53,6 +54,6 @@ private class OneTimeEvent<out T>(private val content: T) {
fun getIfNotHandled(asker: String): T? = if (handlers.add(asker)) content else null
}
private fun <T> Flow<OneTimeEvent<T>>.filterNotHandledBy(consumerId: String): Flow<T> = transform { event ->
private fun <T : VectorViewEvents> Flow<OneTimeEvent<T>>.filterNotHandledBy(consumerId: String): Flow<T> = transform { event ->
event.getIfNotHandled(consumerId)?.let { emit(it) }
}