Skip to content

Commit

Permalink
Event life duration limited to 150ms.
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarty committed Jan 9, 2023
1 parent cceb1cd commit 1c51eda
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions vector/src/main/java/im/vector/app/core/utils/SharedEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package im.vector.app.core.utils

import android.os.SystemClock
import im.vector.app.core.platform.VectorViewEvents
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -43,15 +44,25 @@ class EventQueue<T : VectorViewEvents>(capacity: Int) : SharedEvents<T> {
*
* Keeps track of who has already handled its content.
*/
private class OneTimeEvent<out T : VectorViewEvents>(private val content: T) {

private class OneTimeEvent<out T : VectorViewEvents>(
private val content: T,
private val creationDate: Long = SystemClock.elapsedRealtime()
) {
private val handlers = CopyOnWriteArraySet<String>()

/**
* @param asker Used to identify, whether this "asker" has already handled this Event.
* @return Event content or null if it has been already handled by asker
* @return Event content or null if it has been already handled by asker or if it is too old.
*/
fun getIfNotHandled(asker: String): T? = if (handlers.add(asker)) content else null
fun getIfNotHandled(asker: String): T? {
val now = SystemClock.elapsedRealtime()
if (now > creationDate + LIFE_DURATION_MILLIS) return null
return if (handlers.add(asker)) content else null
}

companion object {
private const val LIFE_DURATION_MILLIS = 150L
}
}

private fun <T : VectorViewEvents> Flow<OneTimeEvent<T>>.filterNotHandledBy(consumerId: String): Flow<T> = transform { event ->
Expand Down

0 comments on commit 1c51eda

Please sign in to comment.