1
votes

I want to communicate from children to the parent as described in the Angular cookbook. A service creates Subject through which to communicate:

progressSubject = new Subject<ProgressMessage>

The parent subscribes to the Subject and reacts on messages:

this.service.progressSubject.subscribe(msg => showProgress(msg))

Clients can use progressSubject.next() to send messages, which works fine. However, if I want to subscribe the progressSubject to another Observable, all subscriptions terminate if the source observable completes. The parent will not react to input anymore.

let first = new Subject();
first.subscribe(this.service.progressSubject);
first.next(3); // value is processed by subscribers to progressSubject
first.complete();

let second= new Subject();
second.subscribe(this.service.progressSubject);
second.next(3); // original subcription to progressSubject is ended, 
                // value is not processed anymore.

How can I ignore the "complete" event on a Subject, so that multiple sources can connect to that Subject?

1
Why are you doing the complete in the first place? - Narm
Because the source could be an observable, e.g. from AJAX calls that already does the complete. I'd like to transform that source and then subscribe my Subject to the source, but filter out the complete events from the source - Alex R

1 Answers

1
votes

I

const first = new Subject();
const progressSubject = new Subject();
first.subscribe(progressSubject);

// is equal to

first.subscribe({
  next: val => progressSubject.next(val),
  error: err => progressSubject.error(err),
  complete: () => progressSubject.complete,
});

// if you don't need `complete` - just don't pass it )

first.subscribe({
  next: val => progressSubject.next(val),
  error: err => progressSubject.error(err),
});

Also we can create helper for that:

function skipComplete(subject){
  return {
    next: val => subject.next(val),
    error: err => subject.error(err),
  }
}

first.subscribe(skipComplete(progressSubject))

II

As @IngoBürk mentioned we can use concat(NEVER):

first
  .pipe(concat(NEVER))
  .subscribe(progressSubject);