@@ -162,12 +162,12 @@ fun main() = runBlocking<Unit> {
162
162
}
163
163
// print elements from the source
164
164
println (" Elements:" )
165
- source.consumeEach { // consume elements from it
165
+ source.collect { // collect elements from it
166
166
println (it)
167
167
}
168
168
// print elements from the source AGAIN
169
169
println (" Again:" )
170
- source.consumeEach { // consume elements from it
170
+ source.collect { // collect elements from it
171
171
println (it)
172
172
}
173
173
}
@@ -198,7 +198,7 @@ elements is produced. It becomes the actual stream of elements on _subscription_
198
198
a different stream of elements, depending on how the corresponding implementation of ` Publisher ` works.
199
199
200
200
The [ publish] coroutine builder, that is used in the above example, launches a fresh coroutine on each subscription.
201
- Every [ Publisher.consumeEach ] [ org.reactivestreams.Publisher.consumeEach ] invocation creates a fresh subscription.
201
+ Every [ Publisher.collect ] [ org.reactivestreams.Publisher.collect ] invocation creates a fresh subscription.
202
202
We have two of them in this code and that is why we see "Begin" printed twice.
203
203
204
204
In Rx lingo this is called a _ cold_ publisher. Many standard Rx operators produce cold streams, too. We can iterate
@@ -217,9 +217,9 @@ method with it.
217
217
218
218
### Subscription and cancellation
219
219
220
- An example in the previous section uses ` source.consumeEach { ... } ` snippet to open a subscription
220
+ An example in the previous section uses ` source.collect { ... } ` snippet to open a subscription
221
221
and receive all the elements from it. If we need more control on how what to do with
222
- the elements that are being received from the channel, we can use [ Publisher.openSubscription ] [ org.reactivestreams.Publisher.openSubscription ]
222
+ the elements that are being received from the channel, we can use [ Publisher.collect ] [ org.reactivestreams.Publisher.collect ]
223
223
as shown in the following example:
224
224
225
225
<!-- - INCLUDE
@@ -269,7 +269,7 @@ listener prints "Finally" to confirm that the subscription is actually being clo
269
269
is never printed because we did not consume all of the elements.
270
270
271
271
We do not need to use an explicit ` cancel ` either if iteration is performed over all the items that are emitted
272
- by the publisher, because it is being cancelled automatically by ` consumeEach ` :
272
+ by the publisher, because it is being cancelled automatically by ` collect ` :
273
273
274
274
<!-- - INCLUDE
275
275
import io.reactivex.*
@@ -285,7 +285,7 @@ fun main() = runBlocking<Unit> {
285
285
.doOnComplete { println (" OnComplete" ) } // ...
286
286
.doFinally { println (" Finally" ) } // ... into what's going on
287
287
// iterate over the source fully
288
- source.consumeEach { println (it) }
288
+ source.collect { println (it) }
289
289
}
290
290
```
291
291
@@ -308,7 +308,7 @@ Finally
308
308
309
309
Notice, how "OnComplete" and "Finally" are printed before the last element "5". It happens because our ` main ` function in this
310
310
example is a coroutine that we start with [ runBlocking] coroutine builder.
311
- Our main coroutine receives on the channel using ` source.consumeEach { ... } ` expression.
311
+ Our main coroutine receives on the flowable using ` source.collect { ... } ` expression.
312
312
The main coroutine is _ suspended_ while it waits for the source to emit an item.
313
313
When the last item is emitted by ` Flowable.range(1, 5) ` it
314
314
_ resumes_ the main coroutine, which gets dispatched onto the main thread to print this
@@ -422,7 +422,7 @@ You can subscribe to subjects from a coroutine just as with any other reactive s
422
422
<!-- - INCLUDE
423
423
import io.reactivex.subjects.BehaviorSubject
424
424
import kotlinx.coroutines.*
425
- import kotlinx.coroutines.rx2.consumeEach
425
+ import kotlinx.coroutines.rx2.collect
426
426
-->
427
427
428
428
``` kotlin
@@ -432,7 +432,7 @@ fun main() = runBlocking<Unit> {
432
432
subject.onNext(" two" )
433
433
// now launch a coroutine to print everything
434
434
GlobalScope .launch(Dispatchers .Unconfined ) { // launch coroutine in unconfined context
435
- subject.consumeEach { println (it) }
435
+ subject.collect { println (it) }
436
436
}
437
437
subject.onNext(" three" )
438
438
subject.onNext(" four" )
@@ -476,7 +476,7 @@ fun main() = runBlocking<Unit> {
476
476
subject.onNext(" two" )
477
477
// now launch a coroutine to print the most recent update
478
478
launch { // use the context of the main thread for a coroutine
479
- subject.consumeEach { println (it) }
479
+ subject.collect { println (it) }
480
480
}
481
481
subject.onNext(" three" )
482
482
subject.onNext(" four" )
@@ -584,7 +584,7 @@ It is straightforward to use from a coroutine:
584
584
``` kotlin
585
585
fun main () = runBlocking<Unit > {
586
586
// Range inherits parent job from runBlocking, but overrides dispatcher with Dispatchers.Default
587
- range(Dispatchers .Default , 1 , 5 ).consumeEach { println (it) }
587
+ range(Dispatchers .Default , 1 , 5 ).collect { println (it) }
588
588
}
589
589
```
590
590
@@ -623,7 +623,7 @@ fun <T, R> Publisher<T>.fusedFilterMap(
623
623
predicate : (T ) -> Boolean , // the filter predicate
624
624
mapper : (T ) -> R // the mapper function
625
625
) = GlobalScope .publish<R >(context) {
626
- consumeEach { // consume the source stream
626
+ collect { // consume the source stream
627
627
if (predicate(it)) // filter part
628
628
send(mapper(it)) // map part
629
629
}
@@ -644,7 +644,7 @@ fun CoroutineScope.range(start: Int, count: Int) = publish<Int> {
644
644
fun main () = runBlocking<Unit > {
645
645
range(1 , 5 )
646
646
.fusedFilterMap(coroutineContext, { it % 2 == 0 }, { " $it is even" })
647
- .consumeEach { println (it) } // print all the resulting strings
647
+ .collect { println (it) } // print all the resulting strings
648
648
}
649
649
```
650
650
@@ -717,7 +717,7 @@ The following code shows how `takeUntil` works:
717
717
fun main () = runBlocking<Unit > {
718
718
val slowNums = rangeWithInterval(200 , 1 , 10 ) // numbers with 200ms interval
719
719
val stop = rangeWithInterval(500 , 1 , 10 ) // the first one after 500ms
720
- slowNums.takeUntil(coroutineContext, stop).consumeEach { println (it) } // let's test it
720
+ slowNums.takeUntil(coroutineContext, stop).collect { println (it) } // let's test it
721
721
}
722
722
```
723
723
@@ -749,9 +749,9 @@ import kotlin.coroutines.*
749
749
750
750
``` kotlin
751
751
fun <T > Publisher<Publisher<T>>.merge (context : CoroutineContext ) = GlobalScope .publish<T >(context) {
752
- consumeEach { pub -> // for each publisher received on the source channel
752
+ collect { pub -> // for each publisher collected
753
753
launch { // launch a child coroutine
754
- pub.consumeEach { send(it) } // resend all element from this publisher
754
+ pub.collect { send(it) } // resend all element from this publisher
755
755
}
756
756
}
757
757
}
@@ -792,7 +792,7 @@ The test code is to use `merge` on `testPub` and to display the results:
792
792
793
793
``` kotlin
794
794
fun main () = runBlocking<Unit > {
795
- testPub().merge(coroutineContext).consumeEach { println (it) } // print the whole stream
795
+ testPub().merge(coroutineContext).collect { println (it) } // print the whole stream
796
796
}
797
797
```
798
798
@@ -975,7 +975,7 @@ fun rangeWithIntervalRx(scheduler: Scheduler, time: Long, start: Int, count: Int
975
975
976
976
fun main () = runBlocking<Unit > {
977
977
rangeWithIntervalRx(Schedulers .computation(), 100 , 1 , 3 )
978
- .consumeEach { println (" $it on thread ${Thread .currentThread().name} " ) }
978
+ .collect { println (" $it on thread ${Thread .currentThread().name} " ) }
979
979
}
980
980
```
981
981
@@ -1021,7 +1021,7 @@ fun rangeWithIntervalRx(scheduler: Scheduler, time: Long, start: Int, count: Int
1021
1021
fun main () = runBlocking<Unit > {
1022
1022
val job = launch(Dispatchers .Unconfined ) { // launch a new coroutine in Unconfined context (without its own thread pool)
1023
1023
rangeWithIntervalRx(Schedulers .computation(), 100 , 1 , 3 )
1024
- .consumeEach { println (" $it on thread ${Thread .currentThread().name} " ) }
1024
+ .collect { println (" $it on thread ${Thread .currentThread().name} " ) }
1025
1025
}
1026
1026
job.join() // wait for our coroutine to complete
1027
1027
}
@@ -1077,8 +1077,7 @@ coroutines for complex pipelines with fan-in and fan-out between multiple worker
1077
1077
<!-- - MODULE kotlinx-coroutines-reactive -->
1078
1078
<!-- - INDEX kotlinx.coroutines.reactive -->
1079
1079
[ publish ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/kotlinx.coroutines.-coroutine-scope/publish.html
1080
- [ org.reactivestreams.Publisher.consumeEach ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/consume-each.html
1081
- [ org.reactivestreams.Publisher.openSubscription ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/open-subscription.html
1080
+ [ org.reactivestreams.Publisher.collect ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-reactive/kotlinx.coroutines.reactive/org.reactivestreams.-publisher/collect.html
1082
1081
<!-- - MODULE kotlinx-coroutines-rx2 -->
1083
1082
<!-- - INDEX kotlinx.coroutines.rx2 -->
1084
1083
[ rxFlowable ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-rx2/kotlinx.coroutines.rx2/kotlinx.coroutines.-coroutine-scope/rx-flowable.html
0 commit comments