generated from GSM-MSG/MSG-Repository-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from GSM-MSG/feature/1_change_logic_to_observe_…
…local 🔀 :: (#1) - change logic to observe local
- Loading branch information
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.drop | ||
import kotlinx.coroutines.flow.first | ||
|
||
class MacaroniFlow<T> constructor( | ||
onRemoteObservable: suspend () -> Flow<T>, | ||
onUpdateLocal: suspend (T) -> Unit, | ||
onLocalObservable: suspend () -> Flow<T>, | ||
) { | ||
|
||
private var onRemoteObservable: suspend () -> Flow<T> | ||
private var onUpdateLocal: suspend (T) -> Unit | ||
private var onLocalObservable: suspend () -> Flow<T> | ||
|
||
init { | ||
this.onRemoteObservable = onRemoteObservable | ||
this.onUpdateLocal = onUpdateLocal | ||
this.onLocalObservable = onLocalObservable | ||
} | ||
|
||
suspend fun fetch(onNext: (MacaroniStatus, T) -> Unit) { | ||
val localData = onLocalObservable().first() | ||
onLocalObservable() | ||
.drop(1) | ||
.collect { data -> | ||
onNext(Success, data) | ||
} | ||
try { | ||
runCatching { | ||
onNext(Loading, localData) | ||
onRemoteObservable() | ||
}.onSuccess { | ||
it.collect { data -> | ||
if (localData != data) { | ||
onUpdateLocal(data) | ||
} else { | ||
onNext(Success, data) | ||
} | ||
} | ||
}.onFailure { | ||
onNext(Error, localData) | ||
} | ||
} catch (e: Exception) { | ||
onNext(Error, localData) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.flow | ||
|
||
private suspend fun main() { | ||
val macaroni = MacaroniFlow( | ||
onRemoteObservable = { | ||
flow { | ||
delay(100) | ||
emit(TestStatus("onRemoteObservable", "test")) | ||
} | ||
}, | ||
onUpdateLocal = { | ||
delay(100) | ||
println("onUpdateLocal") | ||
}, | ||
onLocalObservable = { | ||
flow { | ||
emit(TestStatus("getLocalData", "first")) | ||
delay(200) | ||
emit(TestStatus("changeLocal", "nice")) | ||
} | ||
} | ||
) | ||
|
||
macaroni.fetch { status, result -> | ||
println("status: $status, result: $result") | ||
} | ||
} |