When using the async pipe on a observable that is using the .share() operator (due to expensive calculations in the backend), I stumbled upon this behaviour:
data$ = (new Observable(observer => {
let counter=0;
observer.next(counter)
window.setInterval(() => {
observer.next(counter);
counter++;
}, 2000);
}))
.share();
Template:
{{ (data$|async) !== null }}
{{ (data$|async) !== null }}
The output of the initial value is:
true false
The following outputs (after more than 2 seconds) are:
true true
which is the behavior I would expect for the first value, too.
If I omit the .share(), the output for the first value is "true true", as I would expect. I suppose the behavior above is due to the fact that the first expression in the template triggers observable execution, and once the second async pipe subscribes to the observable, the data is already gone. Is this explanation correct? And how can I avoid this behavior?
observer.next(...); observer.complete(). - Estus FlaskshareReplay(1)was used (as the name says, it shares the replay of last 1 value). For A2+RxJS5 a direct counterpart can be used, see the answer. - Estus Flask