У меня проблема с оператором forkJoin в RxJS и запросами http, отмененными chrome.
У меня есть массив наблюдаемых (то есть http-запросов) с именем validationSupportBatch$
.
Я использую массив следующим образом:
this.subscription.add(
forkJoin<T>(validationSupportBatch$)
.pipe(mergeMap(() => this.getByStandardActivityCode()))
.subscribe((svs: T[]) => {
this.validationSupports = svs;
this.notificationService.success('SVS.SAVE.success');
},
error => this.notificationService.error('SVS.SAVE.failure')
)
);
К сожалению, запросы отменяются Chrome (см. Скриншот ниже для пакета из 3 http запросов).
Может кто-нибудь помочь, пожалуйста?
редактировать
Вот заголовки запроса:
Provisional headers are shown
Accept: application/json, text/plain, */*
Authorization: Bearer XXX
Content-Type: application/json
Origin: https://localhost:4200
Referer: https://localhost:4200/validation/applied/activity/HHN-KADJAR-A0000020/standard-validation-support?planId=HHN-KADJAR-I001
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36
x-validation-context: APPLIED
x-validation-project-family: HHN
x-validation-project-name: Kadjar
x-validation-project-scope: 39416543-6b07-4e92-afd5-afacb1f18975
и тело запроса (только json):
{"id":null,"nature":"PHYSICAL_POWERTRAIN","natureLabel":"Physical - Powertrain","numberOfDays":0,"requiredQty":2,"appliedActivity":{"code":"HHN-KADJAR-A0000020"}}
edit 2 : Как ни странно, когда я не добавляю forkJoin
к subscription
, запросы работают нормально.
Мой код сейчас:
forkJoin<T>(validationSupportBatch$)
.pipe(mergeMap(() => this.getByStandardActivityCode()))
.subscribe((svs: T[]) => {
this.validationSupports = svs;
this.notificationService.success('SVS.SAVE.success');
},
error => this.notificationService.error('SVS.SAVE.failure')
);
Обратите внимание, что this.subscription.add(
часть была удалена.
Есть идеи, почему subscription
препятствует выполнению запросов?
edit 3 : Вот как я могу управлять своими подписками в моем компоненте:
export abstract class ValidationSupportListComponent<T, U> implements OnInit, OnDestroy {
subscription: Subscription = new Subscription();
...
ngOnDestroy() {
this.subscription.unsubscribe();
}
saveValidationSupports(supports: T[]) {
const validationSupportBatch$ = _(supports)
.filter(support => support.requiredQty > 0)
.flatMap(support => _.times(support.requiredQty, () => this.saveValidationSupport(support)))
.value();
this.subscription.add(
forkJoin<T>(validationSupportBatch$)
.pipe(mergeMap(() => this.getByStandardActivityCode()))
.subscribe((svs: T[]) => {
this.validationSupports = svs;
this.notificationService.success('SVS.SAVE.success');
},
error => this.notificationService.error('SVS.SAVE.failure')
)
);
}
edit 4 : Я понимаю, что другие варианты использования this.subscription.add(
внутри компонента также не работают ...
См. Например. Следующий код приводит к отмене запроса :
this.subscription.add(
this.deleteValidationSupport(entity)
.subscribe(() => this.removeFromModel(entity))
);