07-31-2023, 09:53 AM
I have the following code in a component in my Angular2/Firebase app:
ngOnInit() {
this.allItems = this.angularFireDatabase.object(`projects/${id}/results`).map(
results => {
const x = itemMapper(results);
console.log(x); // <-- executes three times
return x;
});
const groupedData = this.allItems.map(groupMapper);
this.towers = groupedData.map(towerMapper);
this.units = groupedData.map(unitMapper);
}
I am using `this.allItems`, `this.towers` and `this.units` in the template with the `async` pipe:
<sanddance [items]="allItems | async">
<p-dataTable [value]="towers | async">
<p-dataTable [value]="units | async">
The code runs fine, but **the problem is that the log executes three times**--in other words, the mapping to create `this.allitems` is running three times. If I remove the `this.towers` and `this.units` assignments, then the log executes only once as expected. In other words, the `this.towers = groupedData.map(towerMapper); ` line seems to be causing an extra call to the mapper creating `this.allItems`, although it shouldn't need to and I don't want to it. Could it even be re-accessing the data from the Firebase database, which would obviously be a problem, especially since this is a lot of data?
Based on my limited understanding on "hot" and "cold" observables, it seems that the AngularFire observable is being treated as "hot", and re-triggered when someone downstream tries to map it (or subscribe to it, which is essentially what the `async` pipe does).
What am I missing?
ngOnInit() {
this.allItems = this.angularFireDatabase.object(`projects/${id}/results`).map(
results => {
const x = itemMapper(results);
console.log(x); // <-- executes three times
return x;
});
const groupedData = this.allItems.map(groupMapper);
this.towers = groupedData.map(towerMapper);
this.units = groupedData.map(unitMapper);
}
I am using `this.allItems`, `this.towers` and `this.units` in the template with the `async` pipe:
<sanddance [items]="allItems | async">
<p-dataTable [value]="towers | async">
<p-dataTable [value]="units | async">
The code runs fine, but **the problem is that the log executes three times**--in other words, the mapping to create `this.allitems` is running three times. If I remove the `this.towers` and `this.units` assignments, then the log executes only once as expected. In other words, the `this.towers = groupedData.map(towerMapper); ` line seems to be causing an extra call to the mapper creating `this.allItems`, although it shouldn't need to and I don't want to it. Could it even be re-accessing the data from the Firebase database, which would obviously be a problem, especially since this is a lot of data?
Based on my limited understanding on "hot" and "cold" observables, it seems that the AngularFire observable is being treated as "hot", and re-triggered when someone downstream tries to map it (or subscribe to it, which is essentially what the `async` pipe does).
What am I missing?