Observable,如果下一個是平等的,如何忽略最後一個?Observable,如果下一個是平等的,如何忽略最後一個?
@Injectable()
export class LoadingService {
private _observer: Observer<boolean>;
loading: Observable<boolean>;
constructor() {
this.loading = new Observable<boolean>(
observer => this._observer = observer);
}
toggleLoadingIndicator(isLoading: boolean) {
if (this._observer) {
this._observer.next(isLoading);
}
}
}
和訂閱:
loadingService.loading
.subscribe(isLoading => this.changeLoading(isLoading)
);
所以,當我打電話,這樣在異步:
loadingService.toggleLoadingIndicator(true);
setTimeout(() => {
loadingService.toggleLoadingIndicator(false);
console.log('finish 5000');
}, 5000);
loadingService.toggleLoadingIndicator(true);
setTimeout(() => {
loadingService.toggleLoadingIndicator(false); //here turn the indicator false, but it should be true once there is a other call async waiting (the 5000)
console.log('finish 3000');
}, 3000);
在3000結束,指示燈變爲假。我如何檢測是否調用了第二個true來取消下一個錯誤並等待最後一個錯誤?
您正在尋找不同的:https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/distinctuntilchanged.md –