1

I have a ConnectableObservable which is part of a BehaviourSubject.

question$: ConnectableObservable<Result>;
private questionSubject: BehaviorSubject<Result>; 

this.questionSubject = new BehaviorSubject<Result>(new Result('initial',{}));
this.question$ = (this.questionSubject.asObservable()).pipe(publish()) as ConnectableObservable<Result>;

I use next method to emit values when response from the server is received.

this.questionSubject.next(new Result(response.result,getQuestionResponse));

It seems that the Subject is emitting past values as well.

    jsonQuestion response:  {"question-id":"78830909-b23e-4345-bc69-63f3ce039b20",...} //SUBJECT SENDS THIS
question-details.component.ts:369 got stream value  Result {result: "success", additionalInfo: "{"question-id":"78830909-b23e-4345-bc69-63f3ce039b…2-ae23-f284213ff80a"},"is-question-creator":true}"}// OBSERVABLE RECEIVED THIS
... AND THE SAME TRACE COMES AGAIN!!
question-details.component.ts:369 got stream value  Result {result: "success", additionalInfo: "{"question-id":"78830909-b23e-4345-bc69-63f3ce039b…2-ae23-f284213ff80a"},"is-question-creator":true}"}

Why is the Observable sending multiple values. How can i make it send only the latest value?

4

1 回答 1

0

When you see multiple identical messages emitted from Subject or BehaviorSubject, the problem may be that you have multiple subscriptions.

You can have multiple subscriptions by literally subscribing multiple times, or by not appropriately disposing of subscriptions.

I'd have to see your subscriptions to be more specific. :-)

于 2019-11-05T18:34:07.210 回答