I have this little bit complex pipe structure in my Angular + RxJS app:
remote-data.service.ts:
getAll(url): Observable<any[]> {
return this.http.get(url, this.options)
.pipe(map((result: any[]) => result));
}
proxy.service.ts:
// localDataService use my LocalDataService
// remoteDataService use my RemoteDataService
getAll(model): Observable<any[]> {
if (model.use_localstorage) {
return this.localDataService.getAll(model)
.pipe(map((result: any[]) => result));
} else {
return this.remoteDataService.getAll(model.url)
.pipe(map((result: any[]) => result));
}
}
helper.service.ts:
getAll(model): Observable<any[]> {
// do some general fancy things...
return this.proxyService.getAll(model)
.pipe(map((result: any) => result));
}
}
Then in my component:
export class MyComponent {
helperService = new HelperService();
model = new MyModel();
getAll() {
this.helperService.getAll().subscribe((result: any[]) => {
// parse result
});
}
}
As you see I build a pipeline from remote data service, proxy service, helper servcice and the component. Of course the reason is to separate each functions from eachother and make my services more reuseable.
My goal is to avoid the memory leaks.
And the question is: if I want to put a take(1)
RxJS operator into my pipeline it's enough to put it into the end of pipeline, before the .subscribe()
or need to put it every services and component too?
What is the best practice in this case to avoid memory leaks?