I'm currently developing a project that involves receiving humidity data from a raspberry pi sensor connected to a backend. Within my Angular Component, I am aiming to display this data in a functional and pure manner. However, I have some doubts regarding my implementation and would greatly appreciate any feedback.
The getAllHumidities()
function serves two purposes:
- It subscribes to an Observable and returns the data in a functional way.
- It also saves the Subscription as "humiditySubscription", albeit as a side effect.
I can't help but feel that the implementation of getAllHumidities()
may not be following best practices. Is it possible to avoid creating the two component properties altogether?
export class HumidityListComponent implements OnInit, OnDestroy {
humidities: Humidity[];
humiditySubscription: Subscription;
constructor(private humidityService: HumidityService) {
}
ngOnInit() {
this.humidities = this.getAllHumidities();
}
getAllHumidities(): Humidity[] {
let humidities: Humidity[];
this.humiditySubscription = this.humidityService.getAll().subscribe(data => {
humidities = data;
});
return humidities;
}
ngOnDestroy() {
this.humiditySubscription.unsubscribe();
}
}