MobileNativeFoundation / MobileNativeFoundation/Store

[Unintuitive behavior] Store.get returns can return stale cached data after sourceOfTruth update

Open
#170 10 comments 0 reactions 1 assignee View on GitHub

@digitalbuddha is already working on this.

Since Nov 16, 2021.

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

Description

Maybe more of a question than a bug. This is related to #78, I believe. When calling Store.get() with no active streams, it will return the memory cached value instead of the sourceOfTruth value. This was initially quite confusing to me, though seems obvious now that I understand it. Is this the intended behavior?

To Reproduce
Here is a test case to reproduce the behavior:

    @Test
    fun `get returns source of truth`() {
        runBlocking {
            val ch = ConflatedBroadcastChannel<Int?>(null)
            val store = StoreBuilder.from<Unit, Int, Int>(
                fetcher = nonFlowValueFetcher { 0 },
                sourceOfTruth = SourceOfTruth.from(
                    reader = { ch.asFlow() },
                    writer = { _, value -> ch.offer(value) }
                )
            ).build()
            assert(store.get(Unit) == 0) // prime the memory cache
            ch.send(1)
            delay(500) // wait for flow to update
            assert(store.get(Unit) == 1)
        }
    }

Expected behavior
It would be nice if Store could track if there were active streams and default to fetching from the sourceOfTruth if non were active. As it stands, perhaps just updating the documentation to make clear what will happen in this scenario would work.

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.