Skip to content

Commit eb13087

Browse files
committed
Use ConcurrentHashMap and rename connection flow property in FeedWatchHandler
1 parent 176d929 commit eb13087

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/client/Create.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ internal fun createFeedsClient(
251251

252252
val feedWatchHandler =
253253
FeedWatchHandler(
254-
connectedEvents = client.connectionState,
254+
connectionState = client.connectionState,
255255
feedsRepository = feedsRepository,
256256
scope = clientScope,
257257
)

stream-feeds-android-client/src/main/kotlin/io/getstream/feeds/android/client/internal/client/reconnect/FeedWatchHandler.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,46 @@ import kotlinx.coroutines.flow.Flow
2525
import kotlinx.coroutines.flow.filterIsInstance
2626
import kotlinx.coroutines.joinAll
2727
import kotlinx.coroutines.launch
28+
import java.util.concurrent.ConcurrentHashMap
2829

2930
/**
3031
* Handles re-watching feeds upon reconnection.
3132
*
3233
* Keeps track of feeds that are being watched and re-subscribes to them when the connection is
3334
* re-established.
3435
*
35-
* @property connectedEvents A [Flow] that emits events when the connection state changes to
36+
* @property connectionState A [Flow] that emits events when the connection state changes to
3637
* [StreamConnectionState.Connected].
3738
* @property feedsRepository The [FeedsRepository] used to rewatch feeds on connection.
3839
* @property scope The [CoroutineScope] in which to launch coroutines for re-watching feeds.
3940
*/
4041
internal class FeedWatchHandler(
41-
private val connectedEvents: Flow<StreamConnectionState>,
42+
private val connectionState: Flow<StreamConnectionState>,
4243
private val feedsRepository: FeedsRepository,
4344
scope: CoroutineScope,
4445
) {
45-
private val watched = mutableSetOf<FeedId>()
46+
private val watched = ConcurrentHashMap<FeedId, Unit>()
4647

4748
init {
4849
scope.launch {
49-
connectedEvents.filterIsInstance<StreamConnectionState.Connected>().collect {
50+
connectionState.filterIsInstance<StreamConnectionState.Connected>().collect {
5051
rewatchAll()
5152
}
5253
}
5354
}
5455

55-
@Synchronized
5656
fun onStartWatching(feedId: FeedId) {
57-
watched += feedId
57+
watched[feedId] = Unit
5858
}
5959

60-
@Synchronized
6160
fun onStopWatching(feedId: FeedId) {
6261
watched -= feedId
6362
}
6463

6564
private suspend fun rewatchAll() {
6665
coroutineScope {
67-
synchronized(this@FeedWatchHandler) { watched.toSet() }
68-
.map { launch { feedsRepository.getOrCreateFeed(FeedQuery(it, watch = true)) } }
66+
watched
67+
.map { launch { feedsRepository.getOrCreateFeed(FeedQuery(it.key, watch = true)) } }
6968
.joinAll()
7069
}
7170
}

0 commit comments

Comments
 (0)