Skip to content Skip to sidebar Skip to footer

Does Startwith() Operator Turns Observable Into Replaysubject(1)?

If I want subscribers to initially get at least X, can I use startWith( X ) for an existing Observable: streamFromLibrary.startWith( X ).subscribe( myHandler ); //I want myHandler(

Solution 1:

You don't need to use ReplaySubject, however you should know that these two aren't the same:

  • The startWith() operator just emits a preset value to every observer when they subscribe.

  • The ReplaySubject(1) class re-emits the one last item that went through it. So the first value it emits to every observer might not be the same depending on what you pushed into this Subject.

Note, that there's also BehaviorSubject that takes its initial value as a parameter and then overrides it with every emission so it works very similarly to ReplaySubject(1).

However there's one important distinction. When BehaviorSubject receives complete notification it never ever emits anything. On the other hand ReplaySubject always replays its buffer to every observer even though it has already received the complete notification.

Post a Comment for "Does Startwith() Operator Turns Observable Into Replaysubject(1)?"