-
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 1 commit
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,66 @@ | ||
| 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 | ||
|
|
||
| interface Fetcher<Key, Output> { | ||
| suspend operator fun invoke(key: Key): Flow<FetcherResponse<Output>> | ||
|
|
||
| companion object { | ||
| fun <Key, Output> fromValueFetcher( | ||
| doFetch: (key: Key) -> Flow<Output> | ||
| ): Fetcher<Key, Output> = FlowingValueFetcher(doFetch) | ||
|
|
||
| fun <Key, Output> fromNonFlowingValueFetcher( | ||
| doFetch: suspend (key: Key) -> Output | ||
| ): Fetcher<Key, Output> = NonFlowingValueFetcher(doFetch) | ||
|
|
||
| fun <Key, Output> fromNonFlowingFetcher( | ||
| doFetch: suspend (key: Key) -> FetcherResponse<Output> | ||
| ): Fetcher<Key, Output> = NonFlowingFetcher(doFetch) | ||
| } | ||
| } | ||
|
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. This would simplify the builder indeed (taking the number of builder create function from 8 to 2 in exchange for adding 4 entry points for the fetcher). I would add some builder extension functions to make the common case easy though. I would like to keep the fetch and transform as separate functions though. The reasons are:
How about something like this?
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. this looks good to me
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. Hmm i'm not sure how much we benefit from |
||
|
|
||
| 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<Output>(it)) | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.dropbox.android.external.store4 | ||
|
|
||
| sealed class FetcherResponse<T> { | ||
| class Value<T>( | ||
| val value: T | ||
| ) : FetcherResponse<T>() | ||
|
|
||
| // 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