gpt4 book ai didi

javascript - 当使用 rxjs 6 以 Angular 6 发出新请求时如何取消正在进行的 HTTP 请求

转载 作者:行者123 更新时间:2023-11-29 16:35:18 29 4
gpt4 key购买 nike

我的应用程序中有两个 HTTP 服务 RxnsSearchServiceRxnsSearchHitCountService

使用 forkJoin 处理两个请求,如下面的代码。

constructor(
private rxnsSearchService: RxnsSearchService,
private rxnsSearchHitCountService: RxnsSearchHitCountService
) { }
const rxnsObservable: Observable<Array<any>> = this.rxnsSearchService.getReactions(this.searchParams, filters);
const headCountObservable: Observable<number> = this.rxnsSearchHitCountService.getHitCount(this.searchParams, filters);
forkJoin([rxnsObservable, headCountObservable]).pipe().subscribe((results) => { //handling results
},
error => {
console.log(error);
});

每当有新请求到来时,我想取消正在进行的旧请求。谁能帮我解决这个问题?

export class RxnsSearchService {
sub: Subject<any> = new Subject();
constructor(private httpClient: HttpClient) {}

getReactions(params: Params, offset: number, perPage: number, filters: any) {
const body = {
filters: filters,
query: params.query
};
return this.httpClient.post(environment.rxnsSearch, body).pipe(
map((response: Array<any>) => {
return response;
}),
catchError(error => {
console.log(error);
return throwError(error);
})
);
}
}

export class RxnsSearchHitCountService {
constructor(private httpClient: HttpClient) {}

getHitCount(params: Params, filters: any) {
const body = {
filters: filters,
query: params.query,
};
return this.httpClient.post(environment.rxnsSearchHitCount, body).pipe(
map((response: number) => {
return response;
}),
catchError(error => {
console.log(error);
return throwError(error);
})
);
}
}

最佳答案

我将通过一个简化的示例来介绍如何执行此操作的一般方法。假设我们目前有这个:

public getReactions() {
this.http.get(…)
.subscribe(reactions => this.reactions = reactions);
}

确保取消旧请求的方法是改为在某些主题上发出:

private reactionsTrigger$ = new Subject<void>();

public getReactions() {
this.reactionsTrigger$.next();
}

现在我们有一个表示触发新请求的事件流的可观察对象。您现在可以将 OnInit 实现为如下所示:

public ngOnInit() {
this.reactionsTrigger$.pipe(
// Use this line if you want to load reactions once initially
// Otherwise, just remove it
startWith(undefined),

// We switchMap the trigger stream to the request
// Due to how switchMap works, if a previous request is still
// running, it will be cancelled.
switchMap(() => this.http.get(…)),

// We have to remember to ensure that we'll unsubscribe from
// this when the component is destroyed
takeUntil(this.destroy$),
).subscribe(reactions => this.reactions = reactions);
}

// Just in case you're unfamiliar with it, this is how you create
// an observable for when the component is destroyed. This helps
// us to unsubscribe properly in the code above
private destroy$ = new Subject<void>();
public ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}

线

    switchMap(() => this.http.get(…)),

在您的情况下,实际上可能会将事件切换到 forkJoin:

    switchMap(() => forkJoin([rxnsObservable, headCountObservable])),

如果您希望单个事件流重新触发两个请求。

关于javascript - 当使用 rxjs 6 以 Angular 6 发出新请求时如何取消正在进行的 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51620217/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com