Store
Store copied to clipboard
[BUG] State Flow From `launchPagingStore` Does Not Reflect Latest Writes in Mutable Store
Summary
Updates made through write operations to mutable store are not being accurately reflected in the StateFlow returned by launchPagingStore.
Description
Expected behavior is any write operation to a mutable store should be observed in the StateFlow emitted by launchPagingStore. However, it appears that after performing a write operation, the state flow still reflects the state of the store prior to the write.
Steps To Reproduce
- Create and initialize a mutable store.
- Invoke
launchPagingStoreon the mutable store. - Perform read operations on the mutable store and observe correct behavior.
- Perform a write operation on the mutable store.
- Observe the state flow from
launchPagingStoredoes not reflect the changes made by the write operation. - Read directly from the mutable store and observe correct value.
Failing Test
@Test
fun multipleKeysWithReadsAndWrites() = testScope.runTest {
val api = FakePostApi()
val db = FakePostDatabase(userId)
val factory = PostStoreFactory(api = api, db = db)
val mutableStore = factory.create()
val key1 = PostKey.Cursor("1", 10)
val key2 = PostKey.Cursor("11", 10)
val keys = flowOf(key1, key2)
val stateFlow = mutableStore.launchPagingStore(this, keys)
stateFlow.test {
val initialState = awaitItem()
assertIs<StoreReadResponse.Initial>(initialState)
val loadingState = awaitItem()
assertIs<StoreReadResponse.Loading>(loadingState)
val loadedState1 = awaitItem()
assertIs<StoreReadResponse.Data<PostData.Feed>>(loadedState1)
val data1 = loadedState1.value
assertEquals(10, data1.posts.size)
val loadedState2 = awaitItem()
assertIs<StoreReadResponse.Data<PostData.Feed>>(loadedState2)
val data2 = loadedState2.value
assertEquals(20, data2.posts.size)
}
mutableStore.write(StoreWriteRequest.of(PostKey.Single("2"), PostData.Post("2", "2-modified")))
stateFlow.test {
val loadedState3 = awaitItem()
assertIs<StoreReadResponse.Data<PostData.Feed>>(loadedState3)
val data3 = loadedState3.value
assertEquals(20, data3.posts.size)
assertEquals("2-modified", data3.posts[1].title) // Actual is "2"
}
Investigation So Far
- First thought was
streamfrom mutable store is not correctly observing updates. However, I verifiedstreamis correctly observing updates. - Reading from store appears to be working correctly.
- Suspect root cause is how we are handling streams for successive keys. Will dig in later this week.