blob: 320aeb699422976f4645895e550e07ef9732ed6b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.pitchedapps.frost.utils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.produce
import kotlinx.coroutines.isActive
@UseExperimental(ExperimentalCoroutinesApi::class)
fun <T> ReceiveChannel<T>.uniqueOnly(scope: CoroutineScope): ReceiveChannel<T> = scope.produce {
var previous: T? = null
for (current in this@uniqueOnly) {
if (!scope.isActive) {
cancel()
} else if (previous != current) {
previous = current
send(current)
}
}
}
|