How to prevent AsyncSubject from completing when the last observer unsubscribes

Viewed 2660

The AsyncSubject becomes observable when the last subject observer unsubscribes from the subject. Here is the quote:

When it’s done, it’s done. Subjects cannot be reused after they’re unsubscribed, completed or errored.

Here is the demo:

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

ofObservable.subscribe(subject);

subject.subscribe((v) => {
    console.log(v);
});

subject.unsubscribe((v) => {
    console.log(v);
});

// here I'll get the error "object unsubscribed"
subject.subscribe((v) => {
    console.log(v);
});

How to prevent the subject from completing?

There's a share operator:

In RxJS 5, the operator share() makes a hot, refCounted observable that can be retried on failure, or repeated on success. Because subjects cannot be reused once they’ve errored, completed or otherwise unsubscribed, the share() operator will recycle dead subjects to enable resubscription to the resulting observable.

That is what I'm looking for. But share creates a subject and I need AsyncSubject.

1 Answers
Related