- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于那里的 Angular 专业人士来说,这应该是一个简单的问题。我有这个简单的方法:
private addMultiple(products: Product[]): void {
let observables: Observable<Product>[] = [];
products.forEach((product: Product) => observables.push(this.productService.create(product)));
forkJoin(observables).subscribe();
}
如果我向该方法发送一个产品,它工作正常,但如果我尝试多个 (100),我只会收到大量已取消的请求,我不确定为什么。
有人知道我是否应该使用不同的方法吗?
这是我的productService:
import { Inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { finalize, map } from 'rxjs/operators';
import { BehaviorSubject, Observable } from 'rxjs';
import { Product, Attempt, RequestOptions } from '../models';
import { HttpServiceConfig, HTTP_SERVICE_CONFIG } from '../configs';
@Injectable({
providedIn: 'root',
})
export class ProductService {
private endpoint: string = 'products';
public items: BehaviorSubject<Product[]>;
public loading: BehaviorSubject<boolean>;
constructor(
@Inject(HTTP_SERVICE_CONFIG) public config: HttpServiceConfig,
private httpClient: HttpClient
) {
this.items = new BehaviorSubject<Product[]>([]);
this.loading = new BehaviorSubject<boolean>(false);
}
list(categoryId: number, options?: RequestOptions): Observable<Product[]> {
this.loading.next(true);
return this.httpClient
.get<Attempt<Product[]>>(
`${this.config.apiUrl}/categories/${categoryId}/${this.endpoint}/master`,
options?.getRequestOptions()
)
.pipe(
map((response: Attempt<Product[]>) => {
if (response.failure) return response.result;
this.items.next(response.result);
return response.result;
}),
finalize(() => this.loading.next(false))
);
}
get(id: number, slug: string, options?: RequestOptions): Observable<Product> {
return this.httpClient
.get<Attempt<Product>>(
`${this.config.apiUrl}/${this.endpoint}/${id}?slug=${slug}`,
options?.getRequestOptions()
)
.pipe(
map((response: Attempt<Product>) => {
return response.result;
})
);
}
public create(item: Product, options?: RequestOptions): Observable<Product> {
return this.httpClient
.post<Attempt<Product>>(
`${this.config.apiUrl}/${this.endpoint}`,
item,
options?.getRequestOptions()
)
.pipe(
map((response: Attempt<Product>) => {
if (response.failure) return response.result;
const newItem = response.result;
const items = this.items.value;
items.push(newItem);
this.items.next(items);
return response.result;
})
);
}
public updateSpecification(
item: Product,
options?: RequestOptions
): Observable<Product> {
return this.httpClient
.put<Attempt<Product>>(
`${this.config.apiUrl}/${this.endpoint}/specification`,
item,
options?.getRequestOptions()
)
.pipe(
map((response: Attempt<Product>) => {
if (response.failure) return response.result;
const newItem = response.result;
const items = this.items.value;
this.remove(items, newItem.id);
items.push(newItem);
this.items.next(items);
return response.result;
})
);
}
public approve(item: Product, options?: RequestOptions): Observable<Product> {
return this.httpClient
.put<Attempt<Product>>(
`${this.config.apiUrl}/${this.endpoint}/approve`,
item,
options?.getRequestOptions()
)
.pipe(
map((response: Attempt<Product>) => {
if (response.failure) return response.result;
const newItem = response.result;
const items = this.items.value;
this.remove(items, newItem.id);
items.push(newItem);
this.items.next(items);
return response.result;
})
);
}
public reject(item: Product, options?: RequestOptions): Observable<Product> {
return this.httpClient
.put<Attempt<Product>>(
`${this.config.apiUrl}/${this.endpoint}/reject`,
item,
options?.getRequestOptions()
)
.pipe(
map((response: Attempt<Product>) => {
if (response.failure) return response.result;
const newItem = response.result;
const items = this.items.value;
this.remove(items, newItem.id);
items.push(newItem);
this.items.next(items);
return response.result;
})
);
}
private remove(items: Product[], id: number | string) {
items.forEach((item, i) => {
if (item.id !== id) {
return;
}
items.splice(i, 1);
});
}
}
最佳答案
如果服务器几乎同时受到太多请求的攻击,它可能会取消请求,这就是使用带有一百个 Observable 的 forkJoin
时发生的情况。
forkJoin
开始并行“执行”所有 Observable。
如果你想控制并行执行的数量,你应该尝试使用 concurrency
参数集的 mergeMap
。如果您想将请求与响应相匹配,这种方法可能会稍微复杂一些。
代码可能是这样的
// set the number of requests you want to fly concurrently
const concurrency = 10;
// use from to generate an Observable from an array of Products
from(products).pipe(
// use mergeMap to execute the request
mergeMap(product => this.productService.create(product).pipe(
// return an object which contains the result and the product which started the request
map(result => ({result, product}))
), concurrency), // set the concurrency in mergeMap
// toArray transform the stream into an Array if this is what you want
toArray()
).subscribe(
res => {// do something with res, which is an array of objects containing objects with result and product}
)
关于Angular 10 forkJoin 取消请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66459546/
调用 forkJoin 时: forkJoin(observableA, observableB) .pipe(takeUntil(onDestroy$)) .pipe(takeUntil(onD
我有以下代码 const mySource:Observable[]> (...) mySource.pipe( switchMap(arr=>forkJoin(arr)); ) 按预期工作,但
TL; 博士 是否可以在 observable 的管道内使用 forkJoin? 完整故事: 我有一个返回对象数组的 Observable 的服务。对于这些对象中的每一个,我都需要再次调用返回一个可观
今天,我打开我的 Angular 项目,发现一个警告说: forkJoin is deprecated: resultSelector is deprecated, pipe to map inste
我正在尝试 Java ForkJoin 框架并编写了一个简单的测试程序,将图像的像素设置为随机颜色。例如。它会产生伪噪声。 但是在测试性能时,我发现运行单线程实际上比运行多线程更快。我通过通过一个高阈
我有一个方法。 private getData() { console.log('Begin'); const sub1 = this.http['url1'].get();
我有几种形式的方法 getFirstDataBunch() { return this.repository.getData(this.parameter).pipe(
我正在使用 forkJoin 合并两个 Firebase 请求的结果 两个请求都完成并记录在控制台中,但 forkJoin 本身的映射函数不会触发,因此没有结果返回到应用程序 public initG
stackblitz 在下面的代码中,this.form.valueChanges 和 this.form.get('name').valueChanges 发射正常。但是 forkJoin 这些都不
我不明白为什么我可以这样做 ForkJoin(service.Obs1(), service.Obs2()).subscribe 但是如果我重构我的代码,以便我向我的服务类添加一个返回 ForkJoi
我的应用程序将进行多个并行 http 调用。它基于 switch-fall though 逻辑,因此查询的数量会有所不同(1、2 或 3 次调用)。有没有办法让我知道哪个电话首先返回?即要么分配一个
我正在尝试使用 forkJoin 运算符来组合两个可观察值并输出一个新值。其中一个调用返回一组对象,其中包含与保单上的多个人相关的数据(该部分不能更改)。另一个 observable 告诉我们谁登录了
对于那里的 Angular 专业人士来说,这应该是一个简单的问题。我有这个简单的方法: private addMultiple(products: Product[]): void { let
我刚刚下载了 Reactive Extensions for .NET 的稳定版本,正在查看 101 Rx examples .并行执行示例使用 ForkJoin方法。这种方法不是稳定版本(或不稳定版
我正在尝试使用 forkJoin 来运行一个带有键和值的对象数组,该值是可观察的。我需要将它设置为对象数组将导致键相同且值是 observable api 调用的结果的位置。这个对象可以有任意数量的键
什么会导致单个 subscribe 触发,但 forkJoin 不触发? 我已经发送了一个触发 subject.subscribe 的请求,但是当我将其添加到与 forkJoin 一起使用的数组时,f
我尝试通过 rx-http-request 使用 RxJS 从 NodeJS 服务器查询第三方 API。从长远来看,我想使用 RxJS 来处理一些更有趣的错误情况。但目前我在运行一个相当微不足道的案例
我目前正在使用 combineLatest() 方法来获取我所有的 friend 并将其作为列表。但是,当我尝试从头开始删除某个项目时,它会产生排序问题。 ( View 无法正确渲染)因此,我想从 c
我有一个使用 Firebase Storage 发送一些图像数据的应用程序,因此我使用此方法: startUpload() { if (typeof this.fileList !== 'unde
我有一个非常大的有序可观察对象列表,需要并行运行。当每个可观察对象返回时,它们将结果附加到行为主题,这就是结果传递的方式。但是,我需要在它们全部完成时调用一个特定的函数。 每个 observables
我是一名优秀的程序员,十分优秀!