Skip to content

Commit 2cd7591

Browse files
authored
Merge pull request #177 from adamint/bug/43-flow-block-thread
Change flow complete to suspendComplete
2 parents 92b6d15 + 57050f4 commit 2cd7591

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/commonMain/kotlin/com.adamratzman.spotify/SpotifyRestAction.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,21 @@ class SpotifyRestActionPaging<Z : Any, T : AbstractPagingObject<Z>>(api: Spotify
137137
/**
138138
* Synchronously retrieve all [AbstractPagingObject] associated with this rest action
139139
*/
140-
fun getAll() = api.tracks.toAction { complete().getAllImpl() }
140+
fun getAll(context: CoroutineContext = Dispatchers.Default) = api.tracks.toAction { suspendComplete(context).getAllImpl() }
141141

142142
/**
143143
* Synchronously retrieve all [Z] associated with this rest action
144144
*/
145-
fun getAllItems() = api.tracks.toAction { complete().getAllImpl().toList().map { it.items }.flatten() }
145+
fun getAllItems(context: CoroutineContext = Dispatchers.Default) =
146+
api.tracks.toAction { suspendComplete(context)
147+
.getAllImpl().toList().map { it.items }.flatten() }
146148

147149
/**
148150
* Consume each [Z] by [consumer] as it is retrieved
149151
*/
150-
fun streamAllItems(consumer: (Z) -> Unit): SpotifyRestAction<Unit> {
152+
fun streamAllItems(context: CoroutineContext = Dispatchers.Default, consumer: (Z) -> Unit): SpotifyRestAction<Unit> {
151153
return api.tracks.toAction {
152-
complete().getAllImpl().toList().forEach { it.items.forEach { item -> consumer(item) } }
154+
suspendComplete(context).getAllImpl().toList().forEach { it.items.forEach { item -> consumer(item) } }
153155
}
154156
}
155157

@@ -159,7 +161,7 @@ class SpotifyRestActionPaging<Z : Any, T : AbstractPagingObject<Z>>(api: Spotify
159161
@FlowPreview
160162
@JvmOverloads
161163
@ExperimentalCoroutinesApi
162-
fun flowOrdered(context: CoroutineContext = Dispatchers.Default): Flow<Z> = flow<Z> {
164+
fun flowOrdered(context: CoroutineContext = Dispatchers.Default): Flow<Z> = flow {
163165
emitAll(flowPagingObjectsOrdered().flatMapConcat { it.asFlow() })
164166
}.flowOn(context)
165167

@@ -169,8 +171,8 @@ class SpotifyRestActionPaging<Z : Any, T : AbstractPagingObject<Z>>(api: Spotify
169171
@JvmOverloads
170172
@ExperimentalCoroutinesApi
171173
fun flowPagingObjectsOrdered(context: CoroutineContext = Dispatchers.Default): Flow<AbstractPagingObject<Z>> =
172-
flow<AbstractPagingObject<Z>> {
173-
complete().also { master ->
174+
flow {
175+
suspendComplete(context).also { master ->
174176
emitAll(master.flowStartOrdered())
175177
emit(master)
176178
emitAll(master.flowEndOrdered())
@@ -183,7 +185,7 @@ class SpotifyRestActionPaging<Z : Any, T : AbstractPagingObject<Z>>(api: Spotify
183185
@FlowPreview
184186
@JvmOverloads
185187
@ExperimentalCoroutinesApi
186-
fun flow(context: CoroutineContext = Dispatchers.Default): Flow<Z> = flow<Z> {
188+
fun flow(context: CoroutineContext = Dispatchers.Default): Flow<Z> = flow {
187189
emitAll(flowPagingObjects().flatMapConcat { it.asFlow() })
188190
}.flowOn(context)
189191

@@ -193,8 +195,8 @@ class SpotifyRestActionPaging<Z : Any, T : AbstractPagingObject<Z>>(api: Spotify
193195
@JvmOverloads
194196
@ExperimentalCoroutinesApi
195197
fun flowPagingObjects(context: CoroutineContext = Dispatchers.Default): Flow<AbstractPagingObject<Z>> =
196-
flow<AbstractPagingObject<Z>> {
197-
complete().also { master ->
198+
flow {
199+
suspendComplete(context).also { master ->
198200
emitAll(master.flowBackward())
199201
emit(master)
200202
emitAll(master.flowForward())

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

Lines changed: 10 additions & 4 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

0 commit comments

Comments
 (0)