-
Notifications
You must be signed in to change notification settings - Fork 215
DO NOT MERGE - WIP Make fetcher a class to avoid the API issues #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package com.dropbox.android.external.store4 | ||
|
|
||
| import kotlinx.coroutines.flow.Flow | ||
| import kotlinx.coroutines.flow.catch | ||
| import kotlinx.coroutines.flow.flow | ||
| import kotlinx.coroutines.flow.map | ||
| import java.util.concurrent.CancellationException | ||
|
|
||
| /** | ||
| * The interface that defines a Fetcher, which is responsible to fetch data from a remote data | ||
| * source. (e.g. make API calls). | ||
| * | ||
| * To create a fetcher, use the convenience methods ([fromValueFetcher], [fromNonFlowingFetcher], | ||
| * [fromNonFlowingValueFetcher]). | ||
| */ | ||
| interface Fetcher<Key, Output> { | ||
| suspend operator fun invoke(key: Key): Flow<FetcherResponse<Output>> | ||
|
|
||
| companion object { | ||
| /** | ||
| * Creates a [Fetcher] from the given flow generating function. If the returned [Flow] emits | ||
| * an error, it will be wrapped in a [FetcherResponse.Error]. | ||
| */ | ||
| fun <Key, Output> fromValueFetcher( | ||
| doFetch: (key: Key) -> Flow<Output> | ||
| ): Fetcher<Key, Output> = FlowingValueFetcher(doFetch) | ||
|
|
||
| /** | ||
| * Creates a [Fetcher] from the given function. If it throws an error, the response will be | ||
| * wrapped in a [FetcherResponse.Error]. | ||
| */ | ||
| fun <Key, Output> fromNonFlowingValueFetcher( | ||
| doFetch: suspend (key: Key) -> Output | ||
| ): Fetcher<Key, Output> = NonFlowingValueFetcher(doFetch) | ||
|
|
||
| /** | ||
| * Creates a [Fetcher] that returns only 1 value (e.g. a single web request, not a stream). | ||
| * An exception thrown from this function will not be caught by Store. | ||
| */ | ||
| fun <Key, Output> fromNonFlowingFetcher( | ||
| doFetch: suspend (key: Key) -> FetcherResponse<Output> | ||
| ): Fetcher<Key, Output> = NonFlowingFetcher(doFetch) | ||
| } | ||
| } | ||
|
|
||
| internal class NonFlowingFetcher<Key, Output>( | ||
| private val doFetch: suspend (key: Key) -> FetcherResponse<Output> | ||
| ) : Fetcher<Key, Output> { | ||
| override suspend fun invoke(key: Key): Flow<FetcherResponse<Output>> { | ||
| return flow { | ||
| emit(doFetch(key)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal class NonFlowingValueFetcher<Key, Output>( | ||
| private val doFetch: suspend (key: Key) -> Output | ||
| ) : Fetcher<Key, Output> { | ||
| override suspend fun invoke(key: Key): Flow<FetcherResponse<Output>> { | ||
| return flow { | ||
| try { | ||
| emit(FetcherResponse.Value(doFetch(key))) | ||
| } catch (th: Throwable) { | ||
| if (th is CancellationException) { | ||
| throw th | ||
| } | ||
| emit( | ||
| FetcherResponse.Error(th) | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal class FlowingValueFetcher<Key, Output>( | ||
| private val doFetch: (key: Key) -> Flow<Output> | ||
| ) : Fetcher<Key, Output> { | ||
| override suspend fun invoke(key: Key): Flow<FetcherResponse<Output>> { | ||
| return doFetch(key).map { | ||
| FetcherResponse.Value(it) as FetcherResponse<Output> | ||
| }.catch { | ||
| emit(FetcherResponse.Error(it)) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package com.dropbox.android.external.store4 | ||
|
|
||
| /** | ||
| * Value type emitted by the [Fetcher]'s `Flow`. | ||
| */ | ||
| sealed class FetcherResponse<T> { | ||
| /** | ||
| * Success result, should include a non-null value. | ||
| */ | ||
| class Value<T>( | ||
| val value: T | ||
| ) : FetcherResponse<T>() | ||
|
|
||
| /** | ||
| * Error result, it should contain the error information | ||
| */ | ||
| // TODO support non-throwable errors ? | ||
| class Error<T>( | ||
| val error: Throwable | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the cost of supporting non throwable errors now |
||
| ) : FetcherResponse<T>() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,13 +118,14 @@ interface StoreBuilder<Key : Any, Output : Any> { | |
| * @param fetcher a function for fetching network records. | ||
| */ | ||
| @OptIn(ExperimentalTime::class) | ||
| @Deprecated(message = "Creating a flow from a function is deprecated, use Fetcher", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. eyal@ is this the one you want to keep instead of deprecating ? |
||
| replaceWith = ReplaceWith( | ||
| expression = "StoreBuilder.from(Fetcher.fromNonFlowingValueFetcher(fetcher))", | ||
| imports = ["com.dropbox.android.external.store4.Fetcher"] | ||
| )) | ||
| fun <Key : Any, Output : Any> fromNonFlow( | ||
| fetcher: suspend (key: Key) -> Output | ||
| ): StoreBuilder<Key, Output> = BuilderImpl { key: Key -> | ||
| flow { | ||
| emit(fetcher(key)) | ||
| } | ||
| } | ||
| ): StoreBuilder<Key, Output> = BuilderImpl(Fetcher.fromNonFlowingValueFetcher(fetcher)) | ||
|
|
||
| /** | ||
| * Creates a new [StoreBuilder] from a [Flow] fetcher. | ||
|
|
@@ -135,9 +136,18 @@ interface StoreBuilder<Key : Any, Output : Any> { | |
| * @param fetcher a function for fetching a flow of network records. | ||
| */ | ||
| @OptIn(ExperimentalTime::class) | ||
| @Deprecated(message = "Creating a flow from a function is deprecated, use Fetcher", | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this as well. cc: @eyalgu |
||
| replaceWith = ReplaceWith( | ||
| expression = "StoreBuilder.from(Fetcher.fromValueFetcher(fetcher))", | ||
| imports = ["com.dropbox.android.external.store4.Fetcher"] | ||
| )) | ||
| fun <Key : Any, Output : Any> from( | ||
| fetcher: (key: Key) -> Flow<Output> | ||
| ): StoreBuilder<Key, Output> = BuilderImpl(fetcher) | ||
| ): StoreBuilder<Key, Output> = BuilderImpl(Fetcher.fromValueFetcher(fetcher)) | ||
|
|
||
| fun <Key : Any, Output : Any> from( | ||
| fetcher: Fetcher<Key, Output> | ||
| ) : StoreBuilder<Key, Output> = BuilderImpl(fetcher) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -146,7 +156,7 @@ interface StoreBuilder<Key : Any, Output : Any> { | |
| @ExperimentalStdlibApi | ||
| @ExperimentalCoroutinesApi | ||
| private class BuilderImpl<Key : Any, Output : Any>( | ||
| private val fetcher: (key: Key) -> Flow<Output> | ||
| private val fetcher: Fetcher<Key, Output> | ||
| ) : StoreBuilder<Key, Output> { | ||
| private var scope: CoroutineScope? = null | ||
| private var cachePolicy: MemoryPolicy? = StoreDefaults.memoryPolicy | ||
|
|
@@ -249,7 +259,7 @@ private class BuilderImpl<Key : Any, Output : Any>( | |
| @ExperimentalStdlibApi | ||
| @ExperimentalCoroutinesApi | ||
| private class BuilderWithSourceOfTruth<Key : Any, Input : Any, Output : Any>( | ||
| private val fetcher: (key: Key) -> Flow<Input>, | ||
| private val fetcher: Fetcher<Key, Input>, | ||
| private val sourceOfTruth: SourceOfTruth<Key, Input, Output>? = null | ||
| ) : StoreBuilder<Key, Output> { | ||
| private var scope: CoroutineScope? = null | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: fromNonFlowingResponseFetcher sounds better imo