MobileNativeFoundation / Store

A Kotlin Multiplatform library for building network-resilient applications
https://mobilenativefoundation.github.io/Store/
Apache License 2.0
3.16k stars 197 forks source link

[BUG] State Flow From `launchPagingStore` Does Not Reflect Latest Writes in Mutable Store #602

Open matt-ramotar opened 7 months ago

matt-ramotar commented 7 months ago

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

  1. Create and initialize a mutable store.
  2. Invoke launchPagingStore on the mutable store.
  3. Perform read operations on the mutable store and observe correct behavior.
  4. Perform a write operation on the mutable store.
  5. Observe the state flow from launchPagingStore does not reflect the changes made by the write operation.
  6. 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