In my Angular12 application, I am fetching data from a Firebase Realtime DB using AngularFire. To streamline my code and ensure consistency, I have implemented a DAO service to preprocess the retrieved data (e.g., converting string dates to Date objects). However, this preprocessing step has caused me to lose the subscription functionality, compromising the real-time data benefits of my app. I am now exploring options to return an Observable list after performing the preprocessing.
Current implementation:
FantaroiDAOService
private list = this.db.list<Fantaros>('fantaroi')
constructor(private db: AngularFireDatabase){}
public getIpir() {
return this.list.valueChanges();
}
AppComponent
ngOnInit(): void {
let fantaroi: Fantaros[] = [];
this.dao.getIpir().subscribe(f => {
fantaroi = f.map(f => {
const soc = f.soc.map(date => new Date(date))
const dishes = f.dishes? f.dishes.map(date => new Date(date)) : []
const off = f.off.map(date => new Date(date))
return new Fantaros(f.name, soc, dishes, off);
})
// other Component related code.
});
}
fantaroi
is the list that requires monitoring for changes in order to update the view. I am seeking a solution that allows me to achieve this without duplicating the date conversion logic throughout my codebase. Any suggestions on how to approach this challenge?