Processor

Processors are the counterparts for Subjects in Flowable. Every type of Subject has its counterpart as processor with backpressure support.

In the previous chapter (Chapter 3Observables, Observers, and Subjects), we started exploring Subject, with the PublishSubject; so, let's do the same here. Let's get started with PublishProcessor.

The following is an example of PublishProcessor:

    fun main(args: Array<String>) { 
      val flowable = listOf("String 1","String 2","String 3",
"String 4","String 5").toFlowable()//(1) val processor = PublishProcessor.create<String>()//(2) processor.//(3) subscribe({ println("Subscription 1: $it") runBlocking { delay(1000) } println("Subscription 1 delay") }) processor//(4) .subscribe({ println("Subscription 2 $it")}) flowable.subscribe(processor)//(5) }

So, in this example, on comment (1), we created a Flowable with the Iterable<T>.toFlowable() method. On comment (2), we created a processor instance with the PublishProcessor.create() method. On comment (3) and (4), we subscribed to the processor instance, and, on comment (5). we subscribed to the Flowable with the processor instance.

The following is the output:

The processor is waiting for all its Subscribers to complete before pushing the next emission.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.178