Skip to content

Commit

Permalink
Remove default params to flow extensions (#3489)
Browse files Browse the repository at this point in the history
* Remove default params to flow extensions

Would rather be verbose and safe than introduce the default dispatcher unknowingly in situations where it would cause issues

* Update FlowExtensions.kt
  • Loading branch information
Alec Strong committed Sep 29, 2022
1 parent fe3a2fd commit f171b21
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
Expand Up @@ -22,7 +22,6 @@ import app.cash.sqldelight.Query
import app.cash.sqldelight.async.coroutines.awaitAsList
import app.cash.sqldelight.async.coroutines.awaitAsOne
import app.cash.sqldelight.async.coroutines.awaitAsOneOrNull
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.Channel.Factory.CONFLATED
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -58,7 +57,7 @@ fun <T : Any> Query<T>.asFlow(): Flow<Query<T>> = flow {

@JvmOverloads
fun <T : Any> Flow<Query<T>>.mapToOne(
context: CoroutineContext = Dispatchers.Default,
context: CoroutineContext,
): Flow<T> = map {
withContext(context) {
it.awaitAsOne()
Expand All @@ -68,7 +67,7 @@ fun <T : Any> Flow<Query<T>>.mapToOne(
@JvmOverloads
fun <T : Any> Flow<Query<T>>.mapToOneOrDefault(
defaultValue: T,
context: CoroutineContext = Dispatchers.Default,
context: CoroutineContext,
): Flow<T> = map {
withContext(context) {
it.awaitAsOneOrNull() ?: defaultValue
Expand All @@ -77,7 +76,7 @@ fun <T : Any> Flow<Query<T>>.mapToOneOrDefault(

@JvmOverloads
fun <T : Any> Flow<Query<T>>.mapToOneOrNull(
context: CoroutineContext = Dispatchers.Default,
context: CoroutineContext,
): Flow<T?> = map {
withContext(context) {
it.awaitAsOneOrNull()
Expand All @@ -86,7 +85,7 @@ fun <T : Any> Flow<Query<T>>.mapToOneOrNull(

@JvmOverloads
fun <T : Any> Flow<Query<T>>.mapToOneNotNull(
context: CoroutineContext = Dispatchers.Default,
context: CoroutineContext,
): Flow<T> = mapNotNull {
withContext(context) {
it.awaitAsOneOrNull()
Expand All @@ -95,7 +94,7 @@ fun <T : Any> Flow<Query<T>>.mapToOneNotNull(

@JvmOverloads
fun <T : Any> Flow<Query<T>>.mapToList(
context: CoroutineContext = Dispatchers.Default,
context: CoroutineContext,
): Flow<List<T>> = map {
withContext(context) {
it.awaitAsList()
Expand Down
Expand Up @@ -6,6 +6,7 @@ import app.cash.sqldelight.coroutines.Employee.Companion.USERNAME
import app.cash.sqldelight.coroutines.TestDb.Companion.TABLE_EMPLOYEE
import app.cash.turbine.test
import kotlinx.coroutines.CoroutineStart.UNDISPATCHED
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.yield
import kotlin.test.Test
Expand Down Expand Up @@ -103,7 +104,7 @@ class QueryAsFlowTest : DbTest {
@Test fun queryCanBeCollectedMoreThanOnce() = runTest { db ->
val flow = db.createQuery(TABLE_EMPLOYEE, "$SELECT_EMPLOYEES WHERE $USERNAME = 'john'", MAPPER)
.asFlow()
.mapToOneNotNull()
.mapToOneNotNull(Dispatchers.Default)

val employee = Employee("john", "John Johnson")

Expand Down

0 comments on commit f171b21

Please sign in to comment.