Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions app/src/main/java/com/dropbox/android/sample/Graph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import kotlin.time.seconds
object Graph {
private val moshi = Moshi.Builder().build()

fun provideRoomStore(context: SampleApp): Store<String, List<Post>> {
fun provideRoomStore(context: SampleApp): Store<String, List<Post>, Throwable> {
val db = provideRoom(context)
return StoreBuilder
.from(
Expand All @@ -58,10 +58,10 @@ object Graph {
.build()
}

fun provideRoomStoreMultiParam(context: SampleApp): Store<Pair<String, RedditConfig>, List<Post>> {
fun provideRoomStoreMultiParam(context: SampleApp): Store<Pair<String, RedditConfig>, List<Post>, Throwable> {
val db = provideRoom(context)
return StoreBuilder
.from<Pair<String, RedditConfig>, List<Post>, List<Post>>(
.from<Pair<String, RedditConfig>, List<Post>, List<Post>, Throwable>(
Fetcher.of { (query, config) ->
provideRetrofit().fetchSubreddit(query, config.limit)
.data.children.map(::toPosts)
Expand Down Expand Up @@ -89,7 +89,7 @@ object Graph {
return SourcePersisterFactory.create(cacheDir)
}

fun provideConfigStore(context: Context): Store<Unit, RedditConfig> {
fun provideConfigStore(context: Context): Store<Unit, RedditConfig, Throwable> {
val fileSystem = FileSystemFactory.create(context.cacheDir)
val fileSystemPersister =
FileSystemPersister.create(
Expand All @@ -100,7 +100,7 @@ object Graph {
)
val adapter = moshi.adapter<RedditConfig>(RedditConfig::class.java)
return StoreBuilder
.from<Unit, RedditConfig, RedditConfig>(
.from<Unit, RedditConfig, RedditConfig, Throwable>(
Fetcher.of {
delay(500)
RedditConfig(10)
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/dropbox/android/sample/SampleApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import okio.BufferedSource
import java.io.IOException

class SampleApp : Application() {
lateinit var roomStore: Store<String, List<Post>>
lateinit var roomStore: Store<String, List<Post>, Throwable>

lateinit var storeMultiParam: Store<Pair<String, RedditConfig>, List<Post>>
lateinit var storeMultiParam: Store<Pair<String, RedditConfig>, List<Post>, Throwable>

lateinit var configStore: Store<Unit, RedditConfig>
lateinit var configStore: Store<Unit, RedditConfig, Throwable>

lateinit var persister: Persister<BufferedSource, Pair<String, String>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class RoomFragment : Fragment() {
*/
@ExperimentalCoroutinesApi
internal class StoreState<Key : Any, Output : Any>(
private val store: Store<Key, Output>
private val store: Store<Key, Output, Throwable>
) {
private val keyFlow = Channel<Key>(capacity = Channel.CONFLATED)
private val _errors = Channel<String>(capacity = Channel.CONFLATED)
Expand Down Expand Up @@ -130,8 +130,7 @@ internal class StoreState<Key : Any, Output : Any>(
)
}
when (it) {
is StoreResponse.Error.Exception -> _errors.send(it.error.localizedMessage!!)
is StoreResponse.Error.Message -> _errors.send(it.message)
is StoreResponse.Error -> _errors.send(it.error.localizedMessage!!)
}
}.transform {
if (it is StoreResponse.Data) {
Expand Down
16 changes: 8 additions & 8 deletions store-rx2/src/main/kotlin/com/dropbox/store/rx2/RxFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import kotlinx.coroutines.reactive.asFlow
*
* @param flowableFactory a factory for a [Flowable] source of network records.
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofResultFlowable(
flowableFactory: (key: Key) -> Flowable<FetcherResult<Output>>
): Fetcher<Key, Output> = ofResultFlow { key: Key -> flowableFactory(key).asFlow() }
fun <Key : Any, Output : Any, Error : Any> Fetcher.Companion.ofResultFlowable(
flowableFactory: (key: Key) -> Flowable<FetcherResult<Output, Error>>
): Fetcher<Key, Output, Error> = ofResultFlow { key: Key -> flowableFactory(key).asFlow() }

/**
* "Creates" a [Fetcher] from a [singleFactory].
Expand All @@ -33,9 +33,9 @@ fun <Key : Any, Output : Any> Fetcher.Companion.ofResultFlowable(
*
* @param singleFactory a factory for a [Single] source of network records.
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofResultSingle(
singleFactory: (key: Key) -> Single<FetcherResult<Output>>
): Fetcher<Key, Output> = ofResultFlowable { key: Key -> singleFactory(key).toFlowable() }
fun <Key : Any, Output : Any, Error : Any> Fetcher.Companion.ofResultSingle(
singleFactory: (key: Key) -> Single<FetcherResult<Output, Error>>
): Fetcher<Key, Output, Error> = ofResultFlowable { key: Key -> singleFactory(key).toFlowable() }

/**
* "Creates" a [Fetcher] from a [flowableFactory] and translate the results to a [FetcherResult].
Expand All @@ -51,7 +51,7 @@ fun <Key : Any, Output : Any> Fetcher.Companion.ofResultSingle(
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofFlowable(
flowableFactory: (key: Key) -> Flowable<Output>
): Fetcher<Key, Output> = ofFlow { key: Key -> flowableFactory(key).asFlow() }
): Fetcher<Key, Output, Throwable> = ofFlow { key: Key -> flowableFactory(key).asFlow() }

/**
* Creates a new [Fetcher] from a [singleFactory] and translate the results to a [FetcherResult].
Expand All @@ -67,4 +67,4 @@ fun <Key : Any, Output : Any> Fetcher.Companion.ofFlowable(
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofSingle(
singleFactory: (key: Key) -> Single<Output>
): Fetcher<Key, Output> = ofFlowable { key: Key -> singleFactory(key).toFlowable() }
): Fetcher<Key, Output, Throwable> = ofFlowable { key: Key -> singleFactory(key).toFlowable() }
18 changes: 11 additions & 7 deletions store-rx2/src/main/kotlin/com/dropbox/store/rx2/RxStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,36 @@ import kotlinx.coroutines.rx2.rxSingle
* @param request - see [StoreRequest] for configurations
*/
@ExperimentalCoroutinesApi
fun <Key : Any, Output : Any> Store<Key, Output>.observe(request: StoreRequest<Key>): Flowable<StoreResponse<Output>> =
stream(request).asFlowable()
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.observe(
request: StoreRequest<Key>
): Flowable<StoreResponse<Output, Error>> = stream(request).asFlowable()

/**
* Purge a particular entry from memory and disk cache.
* Persistent storage will only be cleared if a delete function was passed to
* [StoreBuilder.persister] or [StoreBuilder.nonFlowingPersister] when creating the [Store].
*/
fun <Key : Any, Output : Any> Store<Key, Output>.observeClear(key: Key): Completable =
rxCompletable { clear(key) }
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.observeClear(
key: Key
): Completable = rxCompletable { clear(key) }

/**
* Purge all entries from memory and disk cache.
* Persistent storage will only be cleared if a deleteAll function was passed to
* [StoreBuilder.persister] or [StoreBuilder.nonFlowingPersister] when creating the [Store].
*/
@ExperimentalStoreApi
fun <Key : Any, Output : Any> Store<Key, Output>.observeClearAll(): Completable =
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.observeClearAll() =
rxCompletable { clearAll() }

/**
* Helper factory that will return data as a [Single] for [key] if it is cached otherwise will return fresh/network data (updating your caches)
*/
fun <Key : Any, Output : Any> Store<Key, Output>.getSingle(key: Key) = rxSingle { this@getSingle.get(key) }
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.getSingle(key: Key) =
rxSingle { this@getSingle.get(key) }

/**
* Helper factory that will return fresh data as a [Single] for [key] while updating your caches
*/
fun <Key : Any, Output : Any> Store<Key, Output>.freshSingle(key: Key) = rxSingle { this@freshSingle.fresh(key) }
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.freshSingle(key: Key) =
rxSingle { this@freshSingle.fresh(key) }
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import kotlinx.coroutines.rx2.asCoroutineDispatcher
*/
@FlowPreview
@ExperimentalCoroutinesApi
fun <Key : Any, Output : Any> StoreBuilder<Key, Output>.withScheduler(
fun <Key : Any, Output : Any, Error : Any> StoreBuilder<Key, Output, Error>.withScheduler(
scheduler: Scheduler
): StoreBuilder<Key, Output> {
): StoreBuilder<Key, Output, Error> {
return scope(CoroutineScope(scheduler.asCoroutineDispatcher()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class HotRxSingleStoreTest {
@Test
fun `GIVEN a hot fetcher WHEN two cached and one fresh call THEN fetcher is only called twice`() =
testScope.runBlockingTest {
val fetcher: FakeRxFetcher<Int, FetcherResult<String>> = FakeRxFetcher(
val fetcher: FakeRxFetcher<Int, FetcherResult<String, Throwable>> = FakeRxFetcher(
3 to FetcherResult.Data("three-1"),
3 to FetcherResult.Data("three-2")
)
val pipeline = StoreBuilder.from(Fetcher.ofResultSingle<Int, String> { fetcher.fetch(it) })
val pipeline = StoreBuilder.from(Fetcher.ofResultSingle<Int, String, Throwable> { fetcher.fetch(it) })
.scope(testScope)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RxFlowableStoreTest {
private val atomicInteger = AtomicInteger(0)
private val fakeDisk = mutableMapOf<Int, String>()
private val store =
StoreBuilder.from<Int, String, String>(
StoreBuilder.from<Int, String, String, Throwable>(
Fetcher.ofResultFlowable {
Flowable.create(
{ emitter ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.dropbox.android.external.store4.FetcherResult
import com.dropbox.android.external.store4.SourceOfTruth
import com.dropbox.android.external.store4.StoreBuilder
import com.dropbox.store.rx2.freshSingle
import com.dropbox.store.rx2.ofMaybe
import com.dropbox.store.rx2.getSingle
import com.dropbox.store.rx2.ofMaybe
import com.dropbox.store.rx2.ofResultSingle
import com.dropbox.store.rx2.withScheduler
import io.reactivex.Completable
Expand All @@ -29,7 +29,7 @@ class RxSingleStoreExtensionsTest {
private val atomicInteger = AtomicInteger(0)
private var fakeDisk = mutableMapOf<Int, String>()
private val store =
StoreBuilder.from<Int, String, String>(
StoreBuilder.from<Int, String, String, Throwable>(
fetcher = Fetcher.ofResultSingle {
Single.fromCallable { FetcherResult.Data("$it ${atomicInteger.incrementAndGet()}") }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import com.dropbox.android.external.store4.ExperimentalStoreApi
import com.dropbox.android.external.store4.Fetcher
import com.dropbox.android.external.store4.FetcherResult
import com.dropbox.android.external.store4.ResponseOrigin
import com.dropbox.android.external.store4.SourceOfTruth
import com.dropbox.android.external.store4.StoreBuilder
import com.dropbox.android.external.store4.StoreRequest
import com.dropbox.android.external.store4.StoreResponse
import com.dropbox.android.external.store4.SourceOfTruth
import com.dropbox.store.rx2.ofMaybe
import com.dropbox.store.rx2.observe
import com.dropbox.store.rx2.observeClear
import com.dropbox.store.rx2.observeClearAll
import com.dropbox.store.rx2.ofMaybe
import com.dropbox.store.rx2.ofResultSingle
import com.dropbox.store.rx2.withScheduler
import io.reactivex.Completable
Expand All @@ -33,7 +33,7 @@ class RxSingleStoreTest {
private val atomicInteger = AtomicInteger(0)
private var fakeDisk = mutableMapOf<Int, String>()
private val store =
StoreBuilder.from<Int, String, String>(
StoreBuilder.from<Int, String, String, Throwable>(
fetcher = Fetcher.ofResultSingle {
Single.fromCallable { FetcherResult.Data("$it ${atomicInteger.incrementAndGet()}") }
},
Expand Down
16 changes: 8 additions & 8 deletions store-rx3/src/main/kotlin/com/dropbox/store/rx3/RxFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import kotlinx.coroutines.reactive.asFlow
*
* @param flowableFactory a factory for a [Flowable] source of network records.
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofResultFlowable(
flowableFactory: (key: Key) -> Flowable<FetcherResult<Output>>
): Fetcher<Key, Output> = ofResultFlow { key: Key -> flowableFactory(key).asFlow() }
fun <Key : Any, Output : Any, Error : Any> Fetcher.Companion.ofResultFlowable(
flowableFactory: (key: Key) -> Flowable<FetcherResult<Output, Error>>
): Fetcher<Key, Output, Error> = ofResultFlow { key: Key -> flowableFactory(key).asFlow() }

/**
* "Creates" a [Fetcher] from a [singleFactory].
Expand All @@ -33,9 +33,9 @@ fun <Key : Any, Output : Any> Fetcher.Companion.ofResultFlowable(
*
* @param singleFactory a factory for a [Single] source of network records.
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofResultSingle(
singleFactory: (key: Key) -> Single<FetcherResult<Output>>
): Fetcher<Key, Output> = ofResultFlowable { key: Key -> singleFactory(key).toFlowable() }
fun <Key : Any, Output : Any, Error : Any> Fetcher.Companion.ofResultSingle(
singleFactory: (key: Key) -> Single<FetcherResult<Output, Error>>
): Fetcher<Key, Output, Error> = ofResultFlowable { key: Key -> singleFactory(key).toFlowable() }

/**
* "Creates" a [Fetcher] from a [flowableFactory] and translate the results to a [FetcherResult].
Expand All @@ -51,7 +51,7 @@ fun <Key : Any, Output : Any> Fetcher.Companion.ofResultSingle(
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofFlowable(
flowableFactory: (key: Key) -> Flowable<Output>
): Fetcher<Key, Output> = ofFlow { key: Key -> flowableFactory(key).asFlow() }
): Fetcher<Key, Output, Throwable> = ofFlow { key: Key -> flowableFactory(key).asFlow() }

/**
* Creates a new [Fetcher] from a [singleFactory] and translate the results to a [FetcherResult].
Expand All @@ -67,4 +67,4 @@ fun <Key : Any, Output : Any> Fetcher.Companion.ofFlowable(
*/
fun <Key : Any, Output : Any> Fetcher.Companion.ofSingle(
singleFactory: (key: Key) -> Single<Output>
): Fetcher<Key, Output> = ofFlowable { key: Key -> singleFactory(key).toFlowable() }
): Fetcher<Key, Output, Throwable> = ofFlowable { key: Key -> singleFactory(key).toFlowable() }
16 changes: 9 additions & 7 deletions store-rx3/src/main/kotlin/com/dropbox/store/rx3/RxStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,36 @@ import kotlinx.coroutines.rx3.rxSingle
* @param request - see [StoreRequest] for configurations
*/
@ExperimentalCoroutinesApi
fun <Key : Any, Output : Any> Store<Key, Output>.observe(request: StoreRequest<Key>): Flowable<StoreResponse<Output>> =
stream(request).asFlowable()
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.observe(
request: StoreRequest<Key>
): Flowable<StoreResponse<Output, Error>> = stream(request).asFlowable()

/**
* Purge a particular entry from memory and disk cache.
* Persistent storage will only be cleared if a delete function was passed to
* [StoreBuilder.persister] or [StoreBuilder.nonFlowingPersister] when creating the [Store].
*/
fun <Key : Any, Output : Any> Store<Key, Output>.observeClear(key: Key): Completable =
rxCompletable { clear(key) }
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.observeClear(
key: Key
): Completable = rxCompletable { clear(key) }

/**
* Purge all entries from memory and disk cache.
* Persistent storage will only be cleared if a deleteAll function was passed to
* [StoreBuilder.persister] or [StoreBuilder.nonFlowingPersister] when creating the [Store].
*/
@ExperimentalStoreApi
fun <Key : Any, Output : Any> Store<Key, Output>.observeClearAll(): Completable =
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.observeClearAll() =
rxCompletable { clearAll() }

/**
* Helper factory that will return data as a [Single] for [key] if it is cached otherwise will return fresh/network data (updating your caches)
*/
fun <Key : Any, Output : Any> Store<Key, Output>.getSingle(key: Key) =
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.getSingle(key: Key) =
rxSingle { this@getSingle.get(key) }

/**
* Helper factory that will return fresh data as a [Single] for [key] while updating your caches
*/
fun <Key : Any, Output : Any> Store<Key, Output>.freshSingle(key: Key) =
fun <Key : Any, Output : Any, Error : Any> Store<Key, Output, Error>.freshSingle(key: Key) =
rxSingle { this@freshSingle.fresh(key) }
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import kotlinx.coroutines.rx3.asCoroutineDispatcher
*/
@FlowPreview
@ExperimentalCoroutinesApi
fun <Key : Any, Output : Any> StoreBuilder<Key, Output>.withScheduler(
fun <Key : Any, Output : Any, Error : Any> StoreBuilder<Key, Output, Error>.withScheduler(
scheduler: Scheduler
): StoreBuilder<Key, Output> {
): StoreBuilder<Key, Output, Error> {
return scope(CoroutineScope(scheduler.asCoroutineDispatcher()))
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class HotRxSingleStoreTest {
@Test
fun `GIVEN a hot fetcher WHEN two cached and one fresh call THEN fetcher is only called twice`() =
testScope.runBlockingTest {
val fetcher: FakeRxFetcher<Int, FetcherResult<String>> =
val fetcher: FakeRxFetcher<Int, FetcherResult<String, Throwable>> =
FakeRxFetcher(
3 to FetcherResult.Data("three-1"),
3 to FetcherResult.Data("three-2")
)
val pipeline =
StoreBuilder.from(Fetcher.ofResultSingle<Int, String> { fetcher.fetch(it) })
StoreBuilder.from(Fetcher.ofResultSingle<Int, String, Throwable> { fetcher.fetch(it) })
.scope(testScope)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RxFlowableStoreTest {
private val atomicInteger = AtomicInteger(0)
private val fakeDisk = mutableMapOf<Int, String>()
private val store =
StoreBuilder.from<Int, String, String>(
StoreBuilder.from<Int, String, String, Throwable>(
Fetcher.ofResultFlowable {
Flowable.create(
{ emitter ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import com.dropbox.android.external.store4.FetcherResult
import com.dropbox.android.external.store4.SourceOfTruth
import com.dropbox.android.external.store4.StoreBuilder
import com.dropbox.store.rx3.freshSingle
import com.dropbox.store.rx3.ofMaybe
import com.dropbox.store.rx3.getSingle
import com.dropbox.store.rx3.ofMaybe
import com.dropbox.store.rx3.ofResultSingle
import com.dropbox.store.rx3.withScheduler
import io.reactivex.rxjava3.core.Completable
Expand All @@ -29,7 +29,7 @@ class RxSingleStoreExtensionsTest {
private val atomicInteger = AtomicInteger(0)
private var fakeDisk = mutableMapOf<Int, String>()
private val store =
StoreBuilder.from<Int, String, String>(
StoreBuilder.from<Int, String, String, Throwable>(
fetcher = Fetcher.ofResultSingle {
Single.fromCallable { FetcherResult.Data("$it ${atomicInteger.incrementAndGet()}") }
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RxSingleStoreTest {
private val atomicInteger = AtomicInteger(0)
private var fakeDisk = mutableMapOf<Int, String>()
private val store =
StoreBuilder.from<Int, String, String>(
StoreBuilder.from<Int, String, String, Throwable>(
fetcher = Fetcher.ofResultSingle {
Single.fromCallable { FetcherResult.Data("$it ${atomicInteger.incrementAndGet()}") }
},
Expand Down
Loading