Skip to content

Commit 56a06d0

Browse files
authored
Remove the use of deprecated BroadcastChannel. (#659)
* Remove the use of deprecated BroadcastChannel. Signed-off-by: Roman Kalukiewicz <romkal@google.com> * Renamed channels to flows. Signed-off-by: Roman Kalukiewicz <romkal@google.com> * Run ./gradlew ktlintFormat Signed-off-by: Matt Ramotar <matt.ramotar@uber.com> --------- Signed-off-by: Roman Kalukiewicz <romkal@google.com> Signed-off-by: Matt Ramotar <matt.ramotar@uber.com>
1 parent 8b5db52 commit 56a06d0

File tree

1 file changed

+30
-35
lines changed
  • store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/util

1 file changed

+30
-35
lines changed

store/src/commonTest/kotlin/org/mobilenativefoundation/store/store5/util/AsFlowable.kt

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.mobilenativefoundation.store.store5.util
22

3-
import kotlinx.coroutines.channels.BroadcastChannel
4-
import kotlinx.coroutines.channels.Channel
3+
import kotlinx.coroutines.NonCancellable
4+
import kotlinx.coroutines.channels.BufferOverflow
55
import kotlinx.coroutines.flow.Flow
6+
import kotlinx.coroutines.flow.MutableSharedFlow
67
import kotlinx.coroutines.flow.emitAll
78
import kotlinx.coroutines.flow.flow
89
import kotlinx.coroutines.sync.Mutex
910
import kotlinx.coroutines.sync.withLock
11+
import kotlinx.coroutines.withContext
1012
import org.mobilenativefoundation.store.store5.SourceOfTruth
1113

1214
/**
@@ -58,75 +60,68 @@ fun <Key : Any, Output : Any> SimplePersisterAsFlowable<Key, Output>.asSourceOfT
5860
internal class KeyTracker<Key> {
5961
private val lock = Mutex()
6062

61-
// list of open key channels
62-
private val channels = mutableMapOf<Key, KeyChannel>()
63+
// list of open key flows
64+
private val flows = mutableMapOf<Key, KeyFlow>()
6365

6466
// for testing
65-
internal fun activeKeyCount() = channels.size
67+
internal fun activeKeyCount() = flows.size
6668

6769
/**
6870
* invalidates the given key. If there are flows returned from [keyFlow] for the given [key],
6971
* they'll receive a new emission
7072
*/
7173
suspend fun invalidate(key: Key) {
7274
lock.withLock {
73-
channels[key]
74-
}?.channel?.send(Unit)
75+
flows[key]
76+
}?.flow?.emit(Unit)
7577
}
7678

7779
/**
7880
* Returns a Flow that emits once and then every time the given [key] is invalidated via
7981
* [invalidate]
8082
*/
8183
suspend fun keyFlow(key: Key): Flow<Unit> {
82-
// it is important to allocate KeyChannel lazily (ony when the returned flow is collected
84+
// it is important to allocate KeyFlow lazily (ony when the returned flow is collected
8385
// from). Otherwise, we might just create many of them that are never observed hence never
8486
// cleaned up
8587
return flow {
86-
val keyChannel =
88+
val keyFlow =
8789
lock.withLock {
88-
channels.getOrPut(key) {
89-
KeyChannel(
90-
channel =
91-
BroadcastChannel<Unit>(Channel.CONFLATED).apply {
92-
// start w/ an initial value.
93-
trySend(Unit).isSuccess
94-
},
95-
)
96-
}.also {
97-
it.acquire() // refcount
90+
flows.getOrPut(key) { KeyFlow() }.also {
91+
it.acquire()
9892
}
9993
}
94+
emit(Unit)
10095
try {
101-
emitAll(keyChannel.channel.openSubscription())
96+
emitAll(keyFlow.flow)
10297
} finally {
103-
lock.withLock {
104-
keyChannel.release()
105-
if (keyChannel.channel.isClosedForSend) {
106-
channels.remove(key)
98+
withContext(NonCancellable) {
99+
lock.withLock {
100+
if (keyFlow.release()) {
101+
flows.remove(key)
102+
}
107103
}
108104
}
109105
}
110106
}
111107
}
112108

113109
/**
114-
* A data structure to count how many active flows we have on this channel
110+
* A data structure to count how many active flows we have on this flow
115111
*/
116-
private data class KeyChannel(
117-
val channel: BroadcastChannel<Unit>,
118-
var collectors: Int = 0,
119-
) {
112+
private class KeyFlow {
113+
val flow =
114+
MutableSharedFlow<Unit>(
115+
extraBufferCapacity = 1,
116+
onBufferOverflow = BufferOverflow.DROP_OLDEST,
117+
)
118+
private var collectors: Int = 0
119+
120120
fun acquire() {
121121
collectors++
122122
}
123123

124-
fun release() {
125-
collectors--
126-
if (collectors == 0) {
127-
channel.close()
128-
}
129-
}
124+
fun release() = (--collectors) == 0
130125
}
131126
}
132127

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy