Skip to content

Commit

Permalink
WIP allow disk to continue after fetcher ends
Browse files Browse the repository at this point in the history
This prototypes a change where if Fetcher is a Flow and never emits,
we can fallback to SoT.

This change does break another test we have (StoreTest#testFreshUsesOnlyNetwork) which
ensures that we don't use disk for fresh requests.

These two use cases seems at odds as I can also anticipate some users expecting
to see disk resutls if network returns an error.

Issue: #185
  • Loading branch information
yigit committed Jul 2, 2020
1 parent 61762f2 commit cc4dea7
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
1 change: 0 additions & 1 deletion buildsystem/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ ext.versions = [

// Plugins
androidGradlePlugin : '4.0.0',
androidGradlePlugin : '4.0.0-beta05',
dokkaGradlePlugin : '0.10.0',
ktlintGradle : '9.1.1',
spotlessGradlePlugin : '3.26.1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.transform
Expand Down Expand Up @@ -161,7 +162,10 @@ internal class RealStore<Key : Any, Input : Any, Output : Any>(
): Flow<StoreResponse<Output>> {
val diskLock = CompletableDeferred<Unit>()
val networkLock = CompletableDeferred<Unit>()
val networkFlow = createNetworkFlow(request, networkLock)
val networkFlow = createNetworkFlow(request, networkLock).onCompletion {
// if network finishes, always unlock the disk to let the rest of the machinery continue
diskLock.complete(Unit)
}
if (!request.shouldSkipCache(CacheType.DISK)) {
diskLock.complete(Unit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
Expand Down Expand Up @@ -628,6 +629,32 @@ class FlowStoreTest {
fetcher1Job.cancelAndJoin()
}

@Test
fun `GIVEN Flow Fetcher WHEN it completes with 0 emissions THEN SourceOfTruth should load`() {
// see https://github.com/dropbox/Store/issues/185
testScope.runBlockingTest {
val persister = InMemoryPersister<Int, String>()
persister.write(3, "local")
val pipeline = StoreBuilder.from(
fetcher = Fetcher.ofFlow {
flowOf<String>()
},
sourceOfTruth = persister.asSourceOfTruth()
).build()
assertThat(pipeline.stream(
StoreRequest.fresh(3)
)).emitsExactly(
StoreResponse.Loading(
origin = ResponseOrigin.Fetcher
),
StoreResponse.Data(
origin = ResponseOrigin.SourceOfTruth,
value = "local"
)
)
}
}

suspend fun Store<Int, String>.get(request: StoreRequest<Int>) =
this.stream(request).filter { it.dataOrNull() != null }.first()

Expand Down

0 comments on commit cc4dea7

Please sign in to comment.