我知道标题很奇怪,但是就到了。
我订阅了一个可观察的filteredBooks$
,它由可观察的this._bookFilter$
发出的值触发。但是,该转换有些昂贵,因此,如果从switchMap
发出新值,我想取消this._bookFilter$
的所有正在进行的执行。
我该如何实现?
filteredBooks$: Observable<Book[]> = this._bookFilter$.pipe(
debounceTime(50),
distinctUntilChanged((f1, f2) => this.filtersAreDistinct(f1, f2)),
// IF A NEW MESSAGE (bookFilter emission) COMES I WANT TO INTERRUPT THE EXPENSIVE EXECUTION BELOW
switchMap(
bookFilter => findBooksByCategories$(bookFilter.categories),
scan<Book[], Book[]>((filteredAndSortedResult, chunkOfBooksInCategories) => {
const filteredAndScoredChunk = [];
chunkOfBooksInCategory.forEach(bookToScore => {
const score = bookToScore.getSearchScore().calculateBookScoreByFilter(bookFilter);
if (score !== 0) {
filteredAndScoredChunk.push(bookToScore);
}
});
return filteredAndSortedResult.concat(filteredAndScoredChunk).sort(this.bookSortCompare);
} , []));
}
),
shareReplay(1)
);