3
votes

The documentation on BehaviorSubject states that it should return the last emitted value regardless of when I subscribe but it doesn't return it for me:

const ofObservable = Rx.Observable.of(1, 2, 3);
const subject = new Rx.BehaviorSubject();
ofObservable.subscribe(subject);

subject.subscribe((v) => {
    console.log(v);
}, null, () => {
    console.log('completed');
});

The code logs completed only.

The ReplaySubject works as expected with the above code and log 1, 2, 3, completed.

1

1 Answers

5
votes

The problem is somewhere else.

When you use ofObservable.subscribe(subject) the source Observable emits also the complete notification which marks the Subject as stopped and it will never ever emit anything.

So a solution in this use-case could passing only the next signals:

ofObservable.subscribe(val => subject.next(val)); 

See demo: http://jsbin.com/limurip/3/edit?js,console