I'm here thinking about observables ..
And I imagined the following situations:
Example 1)
In my case when I use NGRX
I create all the architecture correctly, and I create a selector service for that particular store.
In the service it is not possible to use ngOnDestroy because it is not a component and I have the following question, is there a memory leak in a service? Or does this service automatically destroy observables?
Example 2) Using the selectors in example one, is it necessary to subscribe to this selector and then destroy it?
PEOPLE-SELECTORS.SERVICE
@Injectable({ providedIn: 'root' })
export class PeopleSelectorsService {
constructor(private readonly store: Store<StoreState>) {}
get error(): Observable<IRequestError> {
return this.store.pipe(select(fromPeopleSelectors.getError));
}
get loading(): Observable<boolean> {
return this.store.pipe(select(fromPeopleSelectors.getLoading));
}
get main(): Observable<IPeople> {
return this.store.pipe(select(fromPeopleSelectors.getMain));
}
get total(): Observable<number> {
return this.store.pipe(select(fromPeopleSelectors.selectTotal));
}
get all(): Observable<Array<IPeople>> {
return this.store.pipe(select(fromPeopleSelectors.selectAll));
}
get allIds(): Observable<Array<string | number>> {
return this.store.pipe(select(fromPeopleSelectors.selectIds));
}
}
APP.COMPONENT
ngOnInit(): void {
this.peopleDispatchService.getAll();
this.isLoading$ = this.peopleSelectorsService.loading;
}
<main [attr.isLoading]="isLoading$ | async">
<ng-container></ng-container>
<app-loading-container *ngIf="isLoading$ | async; else isMainController"></app-loading-container>
<ng-template #isMainController>
<app-user-talk-controller-container></app-user-talk-controller-container>
<app-user-talk-container></app-user-talk-container>
</ng-template>
</main>