Skip to content

Commit 512d4d6

Browse files
committed
Only notify on actual changes
1 parent 9779780 commit 512d4d6

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,15 @@ internal class FeedsCapabilityRepository(
4444
batcher.onBatch { ids, _, _ -> processBatch(ids) }
4545
}
4646

47-
/** Caches the provided [capabilities], replacing existing entries for the same feed IDs. */
47+
/** Caches the provided [capabilities] and notify listeners in case of changes. */
4848
fun cache(capabilities: Map<FeedId, Set<FeedOwnCapability>>) {
49+
val before = cache.toMap()
4950
cache.putAll(capabilities)
50-
// TODO [G.] do we really want to fire an event? The implication is that we'll notify all
51-
// listeners even if they don't care about these feeds. The alternative is to avoid
52-
// notifying and let `ActivityAdded` handlers/states pull capabilities on demand. Maybe
53-
// that's more error-prone though? We'd have to suspend on an hypothetical `getOrRequest`
54-
// -> is that fine or would it be prone to race conditions?
55-
// Another alternative is to debounce notifications. That should be relatively simple to do
56-
// by launching a coroutine that delays and then notifies, and cancelling the previous job
57-
// each time.
58-
subscriptionManager.onEvent(FeedCapabilitiesUpdated(cache.toMap()))
51+
val after = cache.toMap()
52+
53+
if (after != before) {
54+
subscriptionManager.onEvent(FeedCapabilitiesUpdated(cache.toMap()))
55+
}
5956
}
6057

6158
fun getOrRequest(id: FeedId): Set<FeedOwnCapability>? {

stream-feeds-android-client/src/test/kotlin/io/getstream/feeds/android/client/internal/repository/FeedsCapabilityRepositoryTest.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,20 @@ internal class FeedsCapabilityRepositoryTest {
5757
)
5858

5959
@Test
60-
fun `cache when given capabilities, store them in cache and notify`() {
61-
val feedId1 = FeedId("user:1")
62-
val feedId2 = FeedId("user:2")
63-
val capabilities =
64-
mapOf(
65-
feedId1 to setOf(FeedOwnCapability.ReadFeed, FeedOwnCapability.AddActivity),
66-
feedId2 to setOf(FeedOwnCapability.ReadFeed),
67-
)
60+
fun `cache when given same capabilities, notify only on changes`() {
61+
val capabilities1 = mapOf(FeedId("user:1") to setOf(FeedOwnCapability.ReadFeed))
62+
val capabilities2 = mapOf(FeedId("user:2") to setOf(FeedOwnCapability.AddActivity))
63+
64+
repository.cache(capabilities1)
65+
verify(exactly = 1) { stateEventListener.onEvent(FeedCapabilitiesUpdated(capabilities1)) }
6866

69-
repository.cache(capabilities)
67+
repository.cache(capabilities1)
68+
verify(exactly = 1) { stateEventListener.onEvent(any()) }
7069

71-
verify { stateEventListener.onEvent(FeedCapabilitiesUpdated(capabilities)) }
70+
repository.cache(capabilities2)
71+
val allCapabilities = capabilities1 + capabilities2
72+
verify(exactly = 1) { stateEventListener.onEvent(FeedCapabilitiesUpdated(allCapabilities)) }
73+
verify(exactly = 2) { stateEventListener.onEvent(any()) }
7274
}
7375

7476
@Test

0 commit comments

Comments
 (0)