MobileNativeFoundation / MobileNativeFoundation/Store
[Unintuitive behavior] Store.get returns can return stale cached data after sourceOfTruth update
@digitalbuddha is already working on this.
Since Nov 16, 2021.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.