-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add coroutines/flow support * Add another note * Remove unneeded casts and suppress the needed ones. * Fixed nits * Update based on build changes * Prepare for release * Feedback * Remove from README for now * Undo changes to rxjava3 dep * Add initial tests * Corrections to start and error emissions
- Loading branch information
Showing
19 changed files
with
629 additions
and
6 deletions.
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
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
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 @@ | ||
/build |
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,7 @@ | ||
# lce-coroutines | ||
Coroutines/Flow support for LCE. | ||
|
||
If coming over from RxJava, here is the typical mapping | ||
|
||
- `switchMapContent` -> `flatMapLatestContent` | ||
- `onlyContentEvents` -> `onlyContentEvents` (same name, with `flatMapMerge` used under the hood) |
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,26 @@ | ||
plugins { | ||
id("kotlin") | ||
id("org.jetbrains.dokka") | ||
} | ||
|
||
apply { | ||
from("$rootDir/.buildscript/configure-signing.gradle") | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion.set(JavaLanguageVersion.of(17)) | ||
} | ||
} | ||
kotlin { | ||
jvmToolchain(17) | ||
} | ||
|
||
dependencies { | ||
api(project(":lce")) | ||
api(libs.coroutines) | ||
|
||
testImplementation(libs.truth) | ||
testImplementation(libs.junit) | ||
testImplementation(libs.coroutines.test) | ||
} |
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,3 @@ | ||
POM_ARTIFACT_ID=lce-coroutines | ||
POM_NAME=LCE type Coroutines Support | ||
POM_PACKAGING=jar |
145 changes: 145 additions & 0 deletions
145
lce-coroutines/src/main/java/com/laimiux/lce/flow/ContentEvents.kt
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,145 @@ | ||
@file:OptIn(ExperimentalCoroutinesApi::class) | ||
|
||
package com.laimiux.lce.flow | ||
|
||
import com.laimiux.lce.CT | ||
import com.laimiux.lce.LC | ||
import com.laimiux.lce.LCE | ||
import com.laimiux.lce.UC | ||
import com.laimiux.lce.UCE | ||
import com.laimiux.lce.UCT | ||
import com.laimiux.lce.fold | ||
import com.laimiux.lce.foldTypes | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.emptyFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flatMapMerge | ||
import kotlinx.coroutines.flow.flowOf | ||
|
||
@JvmName("onlyContentEventsUCE") | ||
fun <C, E> Flow<UCE<C, E>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onError = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsUCT") | ||
fun <C> Flow<UCT<C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onError = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsCT") | ||
fun <C> Flow<CT<C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onError = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsLC") | ||
fun <L, C> Flow<LC<L, C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("onlyContentEventsUC") | ||
fun <C> Flow<UC<C>>.onlyContentEvents(): Flow<C> { | ||
return flatMapMerge { | ||
it.fold( | ||
onLoading = { emptyFlow() }, | ||
onContent = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentLCE") | ||
inline fun <L, C, E, NewC> Flow<LCE<L, C, E>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<LCE<L, NewC, E>> | ||
): Flow<LCE<L, NewC, E>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentUCE") | ||
inline fun <C, E, NewC> Flow<UCE<C, E>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<UCE<NewC, E>> | ||
): Flow<UCE<NewC, E>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentUCT") | ||
inline fun <C, NewC> Flow<UCT<C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<UCT<NewC>> | ||
): Flow<UCT<NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentCT") | ||
inline fun <C, NewC> Flow<CT<C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<CT<NewC>> | ||
): Flow<CT<NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onContent = { transform(it.value) }, | ||
onError = { flowOf(it) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentLC") | ||
inline fun <L, C, NewC> Flow<LC<L, C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<LC<L, NewC>> | ||
): Flow<LC<L, NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) } | ||
) | ||
} | ||
} | ||
|
||
@JvmName("flatMapLatestContentContentUC") | ||
inline fun <C, NewC> Flow<UC<C>>.flatMapLatestContent( | ||
crossinline transform: (C) -> Flow<UC<NewC>> | ||
): Flow<UC<NewC>> { | ||
return flatMapLatest { | ||
it.foldTypes( | ||
onLoading = { flowOf(it) }, | ||
onContent = { transform(it.value) } | ||
) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
lce-coroutines/src/main/java/com/laimiux/lce/flow/Convert.kt
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,26 @@ | ||
@file:OptIn(ExperimentalCoroutinesApi::class) | ||
|
||
package com.laimiux.lce.flow | ||
|
||
import com.laimiux.lce.CT | ||
import com.laimiux.lce.UCT | ||
import com.laimiux.lce.asCT | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.emptyFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.flowOf | ||
|
||
/** | ||
* Converts [UCT] type to [CT]. Essentially it ignores loading events. | ||
*/ | ||
fun <C> Flow<UCT<C>>.toCT(): Flow<CT<C>> { | ||
return flatMapLatest { | ||
val ct = it.asCT() | ||
if (ct != null) { | ||
flowOf(ct) | ||
} else { | ||
emptyFlow() | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
lce-coroutines/src/main/java/com/laimiux/lce/flow/DoOnContent.kt
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,73 @@ | ||
package com.laimiux.lce.flow | ||
|
||
import com.laimiux.lce.CE | ||
import com.laimiux.lce.CT | ||
import com.laimiux.lce.LC | ||
import com.laimiux.lce.LCE | ||
import com.laimiux.lce.UC | ||
import com.laimiux.lce.UCE | ||
import com.laimiux.lce.UCT | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
@JvmName("doOnContentLCE") | ||
inline fun <L, C, E> Flow<LCE<L, C, E>>.doOnContent( | ||
crossinline action: (C) -> Unit | ||
): Flow<LCE<L, C, E>> { | ||
return doOnEvent( | ||
onContent = action | ||
) | ||
} | ||
|
||
@JvmName("doOnContentUCE") | ||
inline fun <C, E> Flow<UCE<C, E>>.doOnContent( | ||
crossinline action: (C) -> Unit | ||
): Flow<UCE<C, E>> { | ||
return doOnEvent( | ||
onContent = action | ||
) | ||
} | ||
|
||
@JvmName("doOnContentUCT") | ||
inline fun <C> Flow<UCT<C>>.doOnContent( | ||
crossinline action: (C) -> Unit | ||
): Flow<UCT<C>> { | ||
return doOnEvent( | ||
onContent = action | ||
) | ||
} | ||
|
||
@JvmName("doOnContentCE") | ||
inline fun <C, E> Flow<CE<C, E>>.doOnContent( | ||
crossinline action: (C) -> Unit | ||
): Flow<CE<C, E>> { | ||
return doOnEvent( | ||
onContent = action | ||
) | ||
} | ||
|
||
@JvmName("doOnContentCT") | ||
inline fun <C> Flow<CT<C>>.doOnContent( | ||
crossinline action: (C) -> Unit | ||
): Flow<CT<C>> { | ||
return doOnEvent( | ||
onContent = action | ||
) | ||
} | ||
|
||
@JvmName("doOnContentLC") | ||
inline fun <L, C> Flow<LC<L, C>>.doOnContent( | ||
crossinline action: (C) -> Unit | ||
): Flow<LC<L, C>> { | ||
return doOnEvent( | ||
onContent = action | ||
) | ||
} | ||
|
||
@JvmName("doOnContentUC") | ||
inline fun <C> Flow<UC<C>>.doOnContent( | ||
crossinline action: (C) -> Unit | ||
): Flow<UC<C>> { | ||
return doOnEvent( | ||
onContent = action | ||
) | ||
} |
Oops, something went wrong.