Skip to content

Commit

Permalink
Introduce LoadingStrategy for Dod (#43)
Browse files Browse the repository at this point in the history
* Introduce LoadingStrategy for Dod

---------

Co-authored-by: Artem Mochalov <artyom.mochalov@revolut.com>
  • Loading branch information
artyomychsvc and Artem Mochalov authored Nov 24, 2023
1 parent 08423ed commit 1ab0073
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 52 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ And then to the module level `build.gradle.kts`:

```
dependencies {
implementation 'com.revolut.rxdata:dod:1.4'
implementation 'com.revolut.rxdata:core:1.4'
implementation 'com.revolut.rxdata:scheduler:1.4'
implementation 'com.revolut.rxdata:dod:1.5.13'
implementation 'com.revolut.rxdata:core:1.5.13'
implementation 'com.revolut.rxdata:scheduler:1.5.13'
}
```

Expand Down
2 changes: 1 addition & 1 deletion dfd/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=dfd
VERSION_NAME=1.5.12
VERSION_NAME=1.5.13
POM_NAME=dfd
POM_PACKAGING=jar
GROUP=com.revolut.flowdata
2 changes: 1 addition & 1 deletion dod-wrapper/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=dod-wrapper
VERSION_NAME=1.5.12
VERSION_NAME=1.5.13
POM_NAME=dod-wrapper
POM_PACKAGING=jar
GROUP=com.revolut.rxdata
2 changes: 1 addition & 1 deletion dod/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
POM_ARTIFACT_ID=dod
VERSION_NAME=1.5.12
VERSION_NAME=1.5.13
POM_NAME=dod
POM_PACKAGING=jar
GROUP=com.revolut.rxdata
26 changes: 20 additions & 6 deletions dod/src/main/java/com/revolut/rxdata/dod/DataObservableDelegate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,22 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
* Requests data from network and subscribes to updates
* (can be triggered by other subscribers or manual cache overrides)
*
* @param forceReload - if true network request will be made even if data exists in caches
* @param loadingStrategy - [LoadingStrategy]
*/
@Suppress("RedundantLambdaArrow")
fun observe(params: Params, forceReload: Boolean = true): Observable<Data<Domain>> =
fun observe(params: Params, loadingStrategy: LoadingStrategy): Observable<Data<Domain>> =
Observable.defer {
val memCache = fromMemory(params)
val memoryIsEmpty = memCache == null
val subject = subject(params)
val loading = forceReload || memoryIsEmpty || failedNetworkRequests.containsKey(params)
val loading = loadingStrategy.refreshMemory || memoryIsEmpty || failedNetworkRequests.containsKey(params)

val observable: Observable<Data<Domain>> = if (memCache != null) {
concat(
just(Data(content = memCache, loading = loading)),
subject
).doAfterSubscribe {
if (loading) {
subject.onNext(Data(content = memCache, loading = loading))
subject.onNext(Data(content = memCache, loading = true))
fetchFromNetwork(memCache, params)
}
}
Expand All @@ -125,7 +124,9 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
just(cached),
subject
).doAfterSubscribe {
fetchFromNetwork(cached.content, params)
if (loadingStrategy.refreshStorage || cached.content == null) {
fetchFromNetwork(cached.content, params)
}
}
}
.startWith(Data(null, loading = true))
Expand All @@ -136,6 +137,19 @@ class DataObservableDelegate<Params : Any, Domain : Any> constructor(
.muteRepetitiveReloading()
}

/**
* Requests data from network and subscribes to updates
* (can be triggered by other subscribers or manual cache overrides)
*
* @param forceReload - if true network request will be made even if data exists in caches
*/
@Deprecated(
message = "please migrate to the method with the LoadingStrategy",
replaceWith = ReplaceWith("fun observe(params: Params, loadingStrategy: LoadingStrategy)")
)
fun observe(params: Params, forceReload: Boolean = true): Observable<Data<Domain>> =
observe(params, loadingStrategy = if (forceReload) LoadingStrategy.ForceReload else LoadingStrategy.Auto)

private fun Observable<Data<Domain>>.muteRepetitiveReloading(): Observable<Data<Domain>> =
this.scan(ReloadingDataScanner<Domain>()) { scanner, newEmit ->
scanner.registerData(newEmit)
Expand Down
36 changes: 36 additions & 0 deletions dod/src/main/java/com/revolut/rxdata/dod/LoadingStrategy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.revolut.rxdata.dod

/**
* [DataObservableDelegate] observation loading strategy
*
* @param refreshMemory - if true data will be fetched from network even if there is something in the memory
* @param refreshStorage - if true data will be fetched from network even if there is something in the storage
*/
sealed class LoadingStrategy(
internal val refreshMemory: Boolean,
internal val refreshStorage: Boolean,
) {
/**
* data will be fetched from the Network even if the data exists in the cache
*/
object ForceReload: LoadingStrategy(
refreshMemory = true,
refreshStorage = true,
)

/**
* data will not be fetched from the Network if the data exists in the memory cache
*/
object Auto: LoadingStrategy(
refreshMemory = false,
refreshStorage = true,
)

/**
* data will not be fetched from the Network if the data exists in the cache memory or storage
*/
object LazyReload: LoadingStrategy(
refreshMemory = false,
refreshStorage = false,
)
}
Loading

0 comments on commit 1ab0073

Please sign in to comment.