Skip to content

Commit 57050f4

Browse files
committed
make PagingObject methods non-blocking
1 parent 21640b6 commit 57050f4

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/commonMain/kotlin/com.adamratzman.spotify/models/PagingObjects.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
package com.adamratzman.spotify.models
33

44
import com.adamratzman.spotify.SpotifyApi
5+
import com.adamratzman.spotify.SpotifyRestAction
56
import com.adamratzman.spotify.http.SpotifyEndpoint
67
import com.adamratzman.spotify.models.serialization.toCursorBasedPagingObject
78
import com.adamratzman.spotify.models.serialization.toPagingObject
89
import com.adamratzman.spotify.utils.runBlocking
9-
import kotlin.reflect.KClass
1010
import kotlinx.coroutines.Dispatchers
1111
import kotlinx.coroutines.ExperimentalCoroutinesApi
1212
import kotlinx.coroutines.flow.Flow
@@ -18,6 +18,8 @@ import kotlinx.coroutines.flow.toList
1818
import kotlinx.serialization.SerialName
1919
import kotlinx.serialization.Serializable
2020
import kotlinx.serialization.Transient
21+
import kotlin.coroutines.CoroutineContext
22+
import kotlin.reflect.KClass
2123

2224
/*
2325
Types used in PagingObjects and CursorBasedPagingObjects:
@@ -102,6 +104,7 @@ class PagingObject<T : Any>(
102104
pagingObjects.add(nxt)
103105
nxt = nxt.next?.let { nxt?.getNext() }
104106
}
107+
105108
// we don't need to reverse here, as it's in order
106109
return pagingObjects.asSequence()
107110
}
@@ -115,7 +118,8 @@ class PagingObject<T : Any>(
115118
/**
116119
* Get all items of type [T] associated with the request
117120
*/
118-
suspend fun getAllItems() = endpoint!!.toAction { getAll().complete().map { it.items }.flatten() }
121+
override suspend fun getAllItems(context: CoroutineContext)
122+
= endpoint!!.toAction { getAll().suspendComplete(context).map { it.items }.flatten().asSequence() }
119123
}
120124

121125
/**
@@ -150,8 +154,8 @@ class CursorBasedPagingObject<T : Any>(
150154
/**
151155
* Get all items of type [T] associated with the request
152156
*/
153-
fun getAllItems() = endpoint!!.toAction {
154-
getAll().complete().map { it.items }.flatten().toList()
157+
override suspend fun getAllItems(context: CoroutineContext) = endpoint!!.toAction {
158+
getAll().suspendComplete(context).map { it.items }.flatten().asSequence()
155159
}
156160

157161
@Suppress("UNCHECKED_CAST")
@@ -219,6 +223,8 @@ abstract class AbstractPagingObject<T : Any>(
219223
internal abstract suspend fun getImpl(type: PagingTraversalType): AbstractPagingObject<T>?
220224
internal abstract suspend fun getAllImpl(): Sequence<AbstractPagingObject<T>>
221225

226+
internal abstract suspend fun getAllItems(context: CoroutineContext = Dispatchers.Default): SpotifyRestAction<Sequence<T>>
227+
222228
private suspend fun getNextImpl() = getImpl(PagingTraversalType.FORWARDS)
223229
private suspend fun getPreviousImpl() = getImpl(PagingTraversalType.BACKWARDS)
224230

@@ -253,7 +259,7 @@ abstract class AbstractPagingObject<T : Any>(
253259

254260
@ExperimentalCoroutinesApi
255261
fun flowStartOrdered(): Flow<AbstractPagingObject<T>> =
256-
flow<AbstractPagingObject<T>> {
262+
flow {
257263
if (previous == null) return@flow
258264
flowBackward().toList().reversed().also {
259265
emitAll(it.asFlow())

0 commit comments

Comments
 (0)