Skip to content

Commit 488ece7

Browse files
committed
IRxHttp#toXxxFlow方法增加CoroutineContext参数,并赋于默认值EmptyCoroutineContext
1 parent 58a1779 commit 488ece7

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

rxhttp/src/main/java/rxhttp/IRxHttp.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import rxhttp.wrapper.parse.*
2222
import rxhttp.wrapper.utils.length
2323
import java.io.File
2424
import kotlin.coroutines.CoroutineContext
25+
import kotlin.coroutines.EmptyCoroutineContext
2526

2627
/**
2728
* User: ljx
@@ -191,48 +192,54 @@ suspend fun IRxHttp.toAppendDownload(
191192

192193
fun IRxHttp.toDownloadFlow(
193194
destPath: String,
195+
coroutineContext: CoroutineContext = EmptyCoroutineContext,
194196
): Flow<ProgressT<String>> =
195197
flow {
196198
toSyncDownload(newOutputStreamFactory(destPath)) { emit(it) }
197199
.await()
198-
}.flowOn(Dispatchers.IO)
200+
}.flowOn(Dispatchers.IO + coroutineContext)
199201

200202
fun IRxHttp.toDownloadFlow(
201203
context: Context,
202204
uri: Uri,
205+
coroutineContext: CoroutineContext = EmptyCoroutineContext,
203206
): Flow<ProgressT<Uri>> =
204207
flow {
205208
toSyncDownload(newOutputStreamFactory(context, uri)) { emit(it) }
206209
.await()
207-
}.flowOn(Dispatchers.IO)
210+
}.flowOn(Dispatchers.IO + coroutineContext)
208211

209212
fun <T> IRxHttp.toDownloadFlow(
210213
osFactory: OutputStreamFactory<T>,
214+
coroutineContext: CoroutineContext = EmptyCoroutineContext,
211215
): Flow<ProgressT<T>> =
212216
flow {
213217
toSyncDownload(osFactory) { emit(it) }
214218
.await()
215-
}.flowOn(Dispatchers.IO)
219+
}.flowOn(Dispatchers.IO + coroutineContext)
216220

217221
fun IRxHttp.toAppendDownloadFlow(
218222
destPath: String,
223+
coroutineContext: CoroutineContext = EmptyCoroutineContext,
219224
): Flow<ProgressT<String>> {
220225
val fileLength = File(destPath).length()
221226
setRangeHeader(fileLength, -1, true)
222-
return toDownloadFlow(destPath)
227+
return toDownloadFlow(destPath, coroutineContext)
223228
}
224229

225230
suspend fun IRxHttp.toAppendDownloadFlow(
226231
context: Context,
227232
uri: Uri,
233+
coroutineContext: CoroutineContext = EmptyCoroutineContext,
228234
): Flow<ProgressT<Uri>> {
229235
val length = withContext(Dispatchers.IO) { uri.length(context) }
230236
if (length >= 0) setRangeHeader(length, -1, true)
231-
return toDownloadFlow(context, uri)
237+
return toDownloadFlow(context, uri, coroutineContext)
232238
}
233239

234240
suspend fun IRxHttp.toAppendDownloadFlow(
235241
uriFactory: UriFactory,
242+
coroutineContext: CoroutineContext = EmptyCoroutineContext,
236243
): Flow<ProgressT<Uri>> {
237244
val factory: OutputStreamFactory<Uri> = withContext(Dispatchers.IO) {
238245
uriFactory.query()?.let {
@@ -242,7 +249,7 @@ suspend fun IRxHttp.toAppendDownloadFlow(
242249
newOutputStreamFactory(uriFactory.context, it)
243250
} ?: uriFactory
244251
}
245-
return toDownloadFlow(factory)
252+
return toDownloadFlow(factory, coroutineContext)
246253
}
247254

248255
//All of the above methods will eventually call this method.

0 commit comments

Comments
 (0)