This happens because handleObs
observable is cold, toPromise
creates a subscription, then httpClient(...).subscribe
creates another subscription. This results in several requests. And yes, handleObs.do()
should be used instead, it doesn't result in subscription and just provides side effect.
Generally it is desirable to have request counter for a spinner, because it should handle concurrent requests properly:
function spinnerCallback() {
if (globalSpinnerService.requestCount > 0)
globalSpinnerService.requestCount--;
}
if(spinnerParam) {
globalSpinnerService.requestCount++;
handleObs.do(spinnerCallback, spinnerCallback);
}
And globalSpinnerService.spinner
is actually a getter:
get spinner() {
this.requestCount > 0;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…