3

(Working in RxKotlin and RxJava, but using metacode for simplicity)

Many Reactive Extensions guides begin by creating an Observable from already available data. From The introduction to Reactive Programming you've been missing, it's created from a single string

var soureStream= Rx.Observable.just('https://api.github.com/users');

Similarly, from the frontpage of RxKotlin, from a populated list

val list = listOf(1,2,3,4,5)
list.toObservable()     

Now consider a simple filter that yields an outStream,

var outStream = sourceStream.filter({x > 3})

In both guides the source events are declared apriori. Which means the timeline of events has some form

source: ----1,2,3,4,5-------
out:    --------------4,5---

How can I modify sourceStream to become more of a pipeline? In other words, no input data is available during sourceStream creation? When a source event becomes available, it is immediately processed by out:

source: ---1--2--3-4---5-------
out:    ------------4---5-------

I expected to find an Observable.add() for dynamic updates

var sourceStream = Observable.empty()
var outStream = sourceStream.filter({x>3})

//print each element as its added 
sourceStream .subscribe({println(it)})
outStream.subscribe({println(it)})

for i in range(5):
    sourceStream.add(i)

Is this possible?

Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
  • 1
    I think that's called a PublishSubject. – EpicPandaForce May 12 '17 at 19:24
  • Ah ok, yes this does look like what I need. Thanks,, would never have found that :) – Adam Hughes May 12 '17 at 19:29
  • 1
    You *might* want to look at `PublishRelay` from https://github.com/JakeWharton/RxRelay/ because that does not stop emitting events after an error event. – EpicPandaForce May 12 '17 at 19:37
  • Wonder why the tutoirals don't start with such publishers. I mean you can't be reactive without having something to react to – Adam Hughes May 12 '17 at 19:43
  • Because `Subject`s are generally to be avoided/rarely needed. http://davesexton.com/blog/post/To-Use-Subject-Or-Not-To-Use-Subject.aspx – Daniel T. May 13 '17 at 02:15
  • @AdamHughes Subjects are difficult to control and could be easily abused. But indeed, lost of build-in operator use subject for convenience aswell. Just follow the observable contract when you use subjects. And try not use RxBus as much as possible. – Phoenix Wang May 13 '17 at 13:43
  • I'm new, but how could I solve my problem without a subject? If I'm testing an application, and I want it to "pop" an update every 5 seconds, how else can I do it other than this Publish subscribe business? Can someone post an answer to this question that doesn't involve a Subscriber? – Adam Hughes May 13 '17 at 17:15
  • @AdamHughes It depends on how the events are created. Do you want some kind of [event bus](https://lorentzos.com/rxjava-as-event-bus-the-right-way-10a36bdd49ba)? – Tassos Bassoukos May 13 '17 at 17:58
  • Nor sure what I need (am just starting out). Maybe I'll just figure it out as I go and get more invested into real projects. My goal for this little project was simply to build an event processing pipeline, and wanted to push source updates through it every few seconds (intead of reading from a stored list for example) – Adam Hughes May 13 '17 at 18:31

1 Answers1

3

I'm new, but how could I solve my problem without a subject? If I'm testing an application, and I want it to "pop" an update every 5 seconds, how else can I do it other than this Publish subscribe business? Can someone post an answer to this question that doesn't involve a Subscriber?

If you want to pop an update every five seconds, then create an Observable with the interval operator, don't use a Subject. There are some dozen different operators for constructing Observables so you rarely need a subject.

That said, sometimes you do need one, and they come in very handy when testing code. I use them extensively in unit tests.

To Use Subject Or Not To Use Subject? is and excellent article on the subject of Subjects.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72