2

Does RxJS Observable remove an item after emitting it (to Observer)?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
rejkid
  • 65
  • 8
  • 1
    Your question is not clear, please elaborate with example – Gopi Feb 02 '22 at 04:59
  • When there are elements in Observable stream and I subscribe to it and process all elements from observable in next method of Observer - does Observable removes all the elements it had before emitting them to my Observer next handler? For example: of(1, 2, 3) .subscribe(someObserver); - after that does Observable contains still 1,2,3 elements or it is empty – rejkid Feb 02 '22 at 05:18
  • Observables are just functions. They do not hold any value. In the case of `of(1,2,3,)` the Observable just moves the values received as input (and therefore NOT stored) to the `next` function of the Subscriber. An Observable is a stream over time. One thing occurs at t0 and this thing gets notified to any subscriber. – Picci Feb 02 '22 at 07:40

1 Answers1

1

One of the tricky aspects of RxJS to keep in mind is factoring in time. By default, when an observable emits a value, an existing observer will receive that value. If an observer subscribes to an observable after it has emitted a value, it will not receive it because it was a "late" subscriber.

Also, by default an observable can only have one subscriber. If you want to have multiple subscribers to one observable, you should use the share() operator. Subjects (and their sub-classes) can share with multiple subscribers by default. Even then, you need to keep timing in mind. You must consider when the source observable emits a value, and when all observers are actively subscribed to receive that value.

Lastly, you can also cache emitted values, meaning any late subscribers will still receive those values upon subscription. With observables, you can use the shareReplay(n) where n is the number of values you want to replay to any new subscribers.

BehaviorSubjects work similarly in that by default it will emit its last single value to any observers the moment they subscribe. ReplaySubject also does this where you can pass in n number of emitted values.

Joshua McCarthy
  • 1,739
  • 1
  • 9
  • 6
  • Thanks for the reply Joshua. So are Observables really functions (as stated by Picci) or objects - I am confused by answer from Picci. I thought they store the items and when subscribed to (as you said, before anything is stored in them) they start emitting them to subscribers. – rejkid Feb 02 '22 at 08:23
  • Picci has it right. You should consider them as functions that pass values through at a given point of time. There is no storage of values (unless you use specific operators as I explained above). – Joshua McCarthy Feb 02 '22 at 08:29
  • I have to digest that. I am coming from Java environment and to me calling function of the function is hard to understand (e.g.Observable.subscribe()). – rejkid Feb 02 '22 at 08:42
  • Guys can you point me to some good and simple article to understand RxJS please. – rejkid Feb 02 '22 at 09:38
  • https://www.learnrxjs.io/#introduction is always a good place to start. As for how to setup a data flow, I would recommend searching videos from Deborah Kurata. https://youtu.be/HE-xh_RBIno Her talks typically involve Angular, but the RxJS principles she covers can be used with setup in JS. – Joshua McCarthy Feb 02 '22 at 10:16
  • `"Also, by default an observable can only have one observer." ` is this correct? – ABOS Feb 02 '22 at 19:31
  • I made a slight change to correct this. I changed `observer` to `subscriber`. You can find more details about this concept here: https://stackoverflow.com/questions/42635771/can-rxjs-observables-be-only-handled-by-one-subscriber – Joshua McCarthy Feb 03 '22 at 02:00