MobileNativeFoundation / MobileNativeFoundation/Store

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

Open
#602 0 comments 0 reactions 1 assignee View on GitHub

@matt-ramotar is already working on this.

Since Jan 28, 2024.

bug
Dominant language
Kotlin
Stars
3.4k
Forks
217
Avg merge
18m
Merged PRs (30d)
4

Description

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

  • First thought was stream from mutable store is not correctly observing updates. However, I verified stream is 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.