aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2018-12-28 21:45:46 -0500
committerGitHub <noreply@github.com>2018-12-28 21:45:46 -0500
commit9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea (patch)
tree8e6202efb768d954145038cb8642453c62650c5e /app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt
parentc9769223cb014f588d93c1a73da157010e68a1c8 (diff)
parent8c4db7d79d4f9557d0eef2ef707663c5e8a7aac6 (diff)
downloadfrost-9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea.tar.gz
frost-9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea.tar.bz2
frost-9c3d7c8b6cca17dc10fc310d41e547d1fe1725ea.zip
Merge pull request #1269 from AllanWang/enhancement/coroutine-auth
Enhancement/coroutine
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt20
1 files changed, 20 insertions, 0 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt
new file mode 100644
index 00000000..320aeb69
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/KotlinUtils.kt
@@ -0,0 +1,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)
+ }
+ }
+} \ No newline at end of file