MobileNativeFoundation / MobileNativeFoundation/Store

[Feature Request] Skip any caches if network emits first.

Open
#179 2 comments 2 reactions 1 assignee View on GitHub

@digitalbuddha is already working on this.

Since Nov 16, 2021.

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

Description

At present, fetcher emissions are locked until reader emits, in order to preserve emission order. This presents a problem in the case that you do not want to emit from reader for a given condition, but instead return an emptyFlow. In this case, fetcher will never emit because reader never emits.

A potential solution would be to add some sort of "disk required" property to StoreRequest in order to either enforce current the fetcher lock behavior or skip the reader emission in the case that fetcher emits first.

To work around this, I have moved the conditional logic in fetcher to a map on stream(). However, it would be ideal to instead provide this behavior as part of the library.

Here's an example of the above:

sealed class GetItemError {
  data class Missing(val itemId: ItemId): GetItemError()
  data class Failure(val error: Exception): GetItemError()
}

typealias GetItemResult = Either<GetItemError, Item>
typealias GetItem = (ItemId) -> Flow<GetItemResult>

val fetcher: GetItem =
  { itemId ->
    try {
      val item = itemApiService.getItemById(itemId)
      if (item != null) {
        flowOf(Right(item))
      } else {
        flowOf(Left(GetItemError.Missing(itemId)))
      }
    } catch(e: Exception) {
      flowOf(Left(GetItemError.Failure(e)))
    }
  }

val reader: GetItem =
  { itemId ->
    val item = itemDao.getItemById(itemId)
    if (item != null) {
      flowOf(Right(item))
    } else {
      // Return empty Flow instead of `GetItemError.Missing` because DB 
      // isn't populated yet and this is not an error case.
      emptyFlow()
    }
  }

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.