1

I have a parent component in Angular which creates a ReplaySubject which is sourced from an API call. This changes over time as filters get changed and new calls are made for new data. This Subject is passed to child components so they can work on the data and stay updated if the filters change and new data is emitted.

// ParentComponent
public tasksList = new ReplaySubject(1);
queryParams.switchMap(params => apiCall(params)).subscribe(tasks => tasksList.next(tasks));

// ParentComponent template
<task-list [tasks$]="tasksList"></task-list>

// TaskListComponent
tasksSub = tasks$.subscribe();
ngOnDestroy() { tasksSub.unsubscribe(); }

The problem I have is that when one of the children components is destroyed and unsubscribes from the Subject it completes the Subject and new components cannot subscribe. How can I share the Subject and maintain replayability while also being able to destroy child components?

BeaverusIV
  • 988
  • 1
  • 11
  • 26
  • 2
    What you've described is weird, I've just created a plnkr to test it out and `Subject` won't complete on `unsubscribe` http://plnkr.co/edit/v4PLUKDPwY7ZqksMLko4?p=preview Maybe there's another line somewhere that breaks it. – Harry Ninh Aug 09 '17 at 23:43
  • Hmm, you're right. I even put it through a child component and it behaved correctly. I have solved this another way in the mean time using the approach discussed in https://stackoverflow.com/a/41177163/218312 I will close this question – BeaverusIV Aug 10 '17 at 00:36
  • Possible duplicate of [Angular/RxJs When should I unsubscribe from \`Subscription\`](https://stackoverflow.com/questions/38008334/angular-rxjs-when-should-i-unsubscribe-from-subscription) – BeaverusIV Aug 10 '17 at 00:36
  • I looks like you completed the Subject somewhere and then tried to subscribe to it later: https://stackoverflow.com/documentation/rxjs/9518/subject/29424/subject-and-its-internal-state – martin Aug 10 '17 at 07:09

0 Answers0