Skip to content

Commit

Permalink
Allow passing in CoroutineContext to *QueryPagingSource
Browse files Browse the repository at this point in the history
  • Loading branch information
veyndan committed Jul 19, 2022
1 parent e4b1064 commit 21e5d53
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
4 changes: 2 additions & 2 deletions docs/android_sqlite/android_paging.md
Expand Up @@ -32,7 +32,7 @@ import com.squareup.sqldelight.android.paging3.QueryPagingSource
val pagingSource: PagingSource = QueryPagingSource(
countQuery = playerQueries.countPlayers(),
transacter = playerQueries,
dispatcher = Dispatchers.IO,
context = Dispatchers.IO,
queryProvider = playerQueries::players,
)
```
Expand Down Expand Up @@ -86,7 +86,7 @@ import com.squareup.sqldelight.android.paging3.QueryPagingSource

val keyedSource = QueryPagingSource(
transacter = playerQueries,
dispatcher = Dispatchers.IO,
context = Dispatchers.IO,
pageBoundariesProvider = playerQueries::pageBoundaries,
queryProvider = playerQueries::keyedQuery,
)
Expand Down
Expand Up @@ -3,14 +3,14 @@ package app.cash.sqldelight.paging3
import androidx.paging.PagingState
import app.cash.sqldelight.Query
import app.cash.sqldelight.Transacter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

internal class KeyedQueryPagingSource<Key : Any, RowType : Any>(
private val queryProvider: (beginInclusive: Key, endExclusive: Key?) -> Query<RowType>,
private val pageBoundariesProvider: (anchor: Key?, limit: Long) -> Query<Key>,
private val transacter: Transacter,
private val dispatcher: CoroutineDispatcher,
private val context: CoroutineContext,
) : QueryPagingSource<Key, RowType>() {

private var pageBoundaries: List<Key>? = null
Expand All @@ -27,7 +27,7 @@ internal class KeyedQueryPagingSource<Key : Any, RowType : Any>(
}

override suspend fun load(params: LoadParams<Key>): LoadResult<Key, RowType> {
return withContext(dispatcher) {
return withContext(context) {
try {
transacter.transactionWithResult {
val boundaries = pageBoundaries
Expand Down
Expand Up @@ -18,21 +18,21 @@ package app.cash.sqldelight.paging3
import androidx.paging.PagingState
import app.cash.sqldelight.Query
import app.cash.sqldelight.Transacter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext

internal class OffsetQueryPagingSource<RowType : Any>(
private val queryProvider: (limit: Long, offset: Long) -> Query<RowType>,
private val countQuery: Query<Long>,
private val transacter: Transacter,
private val dispatcher: CoroutineDispatcher,
private val context: CoroutineContext,
) : QueryPagingSource<Long, RowType>() {

override val jumpingSupported get() = true

override suspend fun load(
params: LoadParams<Long>,
): LoadResult<Long, RowType> = withContext(dispatcher) {
): LoadResult<Long, RowType> = withContext(context) {
try {
val key = params.key ?: 0L
transacter.transactionWithResult {
Expand Down
Expand Up @@ -19,8 +19,8 @@ import androidx.paging.PagingConfig
import androidx.paging.PagingSource
import app.cash.sqldelight.Query
import app.cash.sqldelight.Transacter
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
import kotlin.properties.Delegates

internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
Expand Down Expand Up @@ -55,19 +55,19 @@ internal abstract class QueryPagingSource<Key : Any, RowType : Any> :
* OFFSET 100;
* ```
*
* Queries will be executed on [dispatcher].
* Queries will be executed on [context].
*/
@Suppress("FunctionName")
fun <RowType : Any> QueryPagingSource(
countQuery: Query<Long>,
transacter: Transacter,
dispatcher: CoroutineDispatcher = Dispatchers.IO,
context: CoroutineContext = Dispatchers.IO,
queryProvider: (limit: Long, offset: Long) -> Query<RowType>,
): PagingSource<Long, RowType> = OffsetQueryPagingSource(
queryProvider,
countQuery,
transacter,
dispatcher,
context,
)

/**
Expand Down Expand Up @@ -125,20 +125,20 @@ fun <RowType : Any> QueryPagingSource(
* ORDER BY value ASC;
* ```
*
* Queries will be executed on [dispatcher].
* Queries will be executed on [context].
*
* This [PagingSource] _does not_ support jumping. If your use case requires jumping, use the
* offset based [QueryPagingSource] function.
*/
@Suppress("FunctionName")
fun <Key : Any, RowType : Any> QueryPagingSource(
transacter: Transacter,
dispatcher: CoroutineDispatcher,
context: CoroutineContext,
pageBoundariesProvider: (anchor: Key?, limit: Long) -> Query<Key>,
queryProvider: (beginInclusive: Key, endExclusive: Key?) -> Query<RowType>,
): PagingSource<Key, RowType> = KeyedQueryPagingSource(
queryProvider,
pageBoundariesProvider,
transacter,
dispatcher,
context,
)
Expand Up @@ -12,9 +12,9 @@ import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.junit.Before
import org.junit.Test
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.assertEquals
import kotlin.test.assertNull

Expand All @@ -36,7 +36,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -55,7 +55,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -74,7 +74,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = 5L, loadSize = 2, false)) }
Expand All @@ -87,7 +87,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = 9L, loadSize = 3, false)) }
Expand All @@ -102,7 +102,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

assertNull(
Expand All @@ -122,7 +122,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = null, loadSize = 3, false)) }
Expand All @@ -143,7 +143,7 @@ class KeyedQueryPagingSourceTest {
queryProvider = this::query,
pageBoundariesProvider = this::pageBoundaries,
transacter = transacter,
dispatcher = TestCoroutineDispatcher(),
context = EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(key = 6L, loadSize = 3, false)) }
Expand Down
Expand Up @@ -25,12 +25,12 @@ import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.test.assertFailsWith

@ExperimentalCoroutinesApi
Expand All @@ -52,7 +52,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(null, 2, false)) }
Expand All @@ -66,7 +66,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(null, 2, false)) }
Expand All @@ -80,7 +80,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(8, 2, false)) }
Expand All @@ -94,7 +94,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -113,7 +113,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(9, 2, false)) }
Expand All @@ -127,7 +127,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(1L, 2, false)) }
Expand All @@ -141,7 +141,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(null, 2, false)) }
Expand All @@ -155,7 +155,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(4, 2, false)) }
Expand All @@ -169,7 +169,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(8, 2, false)) }
Expand All @@ -183,7 +183,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(9, 2, false)) }
Expand All @@ -197,7 +197,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

val results = runBlocking { source.load(Refresh(1, 2, false)) }
Expand All @@ -211,7 +211,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -230,7 +230,7 @@ class OffsetQueryPagingSourceTest {
this::query,
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking {
Expand All @@ -246,7 +246,7 @@ class OffsetQueryPagingSourceTest {
{ _, _ -> query },
countQuery(),
transacter,
TestCoroutineDispatcher(),
EmptyCoroutineContext,
)

runBlocking { source.load(Refresh(null, 0, false)) }
Expand Down

0 comments on commit 21e5d53

Please sign in to comment.