Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@

# Jenv
.java-version

# Claude
.claude/settings.local.json
29 changes: 29 additions & 0 deletions lce-coroutines/src/main/java/com/laimiux/lce/flow/Init.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,42 @@ import com.laimiux.lce.LC
import com.laimiux.lce.UC
import com.laimiux.lce.UCE
import com.laimiux.lce.UCT
import com.laimiux.lce.asUCT
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart

/**
* Runs the operation and returns a [CT] result.
*/
@Suppress("TooGenericExceptionCaught")
suspend inline fun <Result> runCT(execute: suspend () -> Result): CT<Result> {
return try {
CT.content(execute())
} catch (e: CancellationException) {
// We need to emit cancellation errors
throw e
} catch (e: Throwable) {
CT.error(e)
}
}

/**
* Creates a flow that executes [operation] and emits a [UCT] for the operation [Result].
*/
inline fun <Result> uctFlow(crossinline operation: suspend () -> Result): Flow<UCT<Result>> {
return flow {
emit(UCT.loading())

val result = runCT(operation)
emit(result.asUCT())
}
}

@Suppress("USELESS_CAST")
inline fun <C, E> Flow<C>.toUCE(
crossinline mapError: (Throwable) -> E
Expand Down
61 changes: 61 additions & 0 deletions lce-coroutines/src/test/java/com/laimiux/lce/flow/FlowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,65 @@ class FlowTest {
assertThat(e.message).isEqualTo("cancelled")
}
}

@Test
fun `runCT returns content on success`() = runTest {
val result = runCT { "Success" }
assertThat(result).isEqualTo(CT.content("Success"))
}

@Test
fun `runCT returns error on exception`() = runTest {
val exception = RuntimeException("Error")
val result = runCT { throw exception }
assertThat(result).isEqualTo(CT.error(exception))
}

@Test
fun `runCT rethrows CancellationException`() = runTest {
try {
runCT { throw CancellationException("cancelled") }
throw AssertionError("Expected CancellationException to be thrown")
} catch (e: CancellationException) {
assertThat(e.message).isEqualTo("cancelled")
}
}

@Test
fun `uctFlow emits loading first`() = runTest {
val flow = uctFlow { "Content" }
assertThat(flow.first()).isEqualTo(UCT.loading())
}

@Test
fun `uctFlow emits content on success`() = runTest {
val flow = uctFlow { "Success" }
val emissions = flow.toList()

assertThat(emissions).hasSize(2)
assertThat(emissions[0]).isEqualTo(UCT.loading())
assertThat(emissions[1]).isEqualTo(UCT.content("Success"))
}

@Test
fun `uctFlow emits error on exception`() = runTest {
val exception = RuntimeException("Error")
val flow = uctFlow<String> { throw exception }
val emissions = flow.toList()

assertThat(emissions).hasSize(2)
assertThat(emissions[0]).isEqualTo(UCT.loading())
assertThat(emissions[1]).isEqualTo(UCT.error(exception))
}

@Test
fun `uctFlow rethrows CancellationException`() = runTest {
val flow = uctFlow<String> { throw CancellationException("cancelled") }
try {
flow.collect()
throw AssertionError("Expected CancellationException to be thrown")
} catch (e: CancellationException) {
assertThat(e.message).isEqualTo("cancelled")
}
}
}
Loading