为什么大多数HttpClient API类都定义不可变对象? [英] Why do most classes of the HttpClient API define immutable objects?

查看:114
本文介绍了为什么大多数HttpClient API类都定义不可变对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpClient的文档说明了有关不变性的以下内容:

拦截器可以检查和更改传出的请求,并且 收到的回复.但是,得知 HttpRequest和HttpResponse类在很大程度上是不可变的.

这是有原因的:由于该应用程序可能会重试请求,因此 拦截器链可能会多次处理单个请求.如果 请求是可变的,重试的请求将不同于 原始请求.不变性确保拦截器看到的相同 要求每次尝试.

我很难理解这个解释.有人可以提供解释吗?

解决方案

如果不了解 Insider's的文章中对此进行了详细说明. .

当您调用http的任何方法时,例如get,Angular会创建一个请求.然后,此请求用于启动可观察的序列,订阅后,此请求将通过拦截器链传递.这是序列处理的一部分(非常简化的代码):

function get() {
    let req: HttpRequest<any> = new HttpRequest<any>();
    const events$ = of(req).pipe(concatMap((req: HttpRequest<any>) => {
        // running request through interceptors chain
        this.handler.handle(req);
    }));
    return $events;
}

以下是来源的评论:

从初始请求Observable.of()开始,然后运行 处理程序( 包括concatMap()中的所有拦截器.这样,处理程序运行 在可观察"链中,会导致拦截器在每个 订阅(这也会使重试重新运行处理程序,包括拦截器)

.

$events流是从http请求方法返回的流,可以重试.拦截器应始终以原始请求开头.如果请求是可变的,并且可以在上一次拦截器运行期间进行修改,则无法满足此条件.因此,请求及其所有组成部分应该是不变的.

The documentation for HttpClient states the following about immutability:

Interceptors exist to examine and mutate outgoing requests and incoming responses. However, it may be surprising to learn that the HttpRequest and HttpResponse classes are largely immutable.

This is for a reason: because the app may retry requests, the interceptor chain may process an individual request multiple times. If requests were mutable, a retried request would be different than the original request. Immutability ensures the interceptors see the same request for each try.

I find it hard to understand this explanation. Can someone please provide an explanation?

解决方案

This explanation is very difficult to understand without knowing the source code. It's explained in depth in the article Insider’s guide into interceptors and HttpClient mechanics in Angular.

When you call any method of http, like get, Angular creates a request. Then this request is used to start an observable sequence and when subscribed this request is passed through interceptors chain. This is done as part of sequence processing (very simplified code):

function get() {
    let req: HttpRequest<any> = new HttpRequest<any>();
    const events$ = of(req).pipe(concatMap((req: HttpRequest<any>) => {
        // running request through interceptors chain
        this.handler.handle(req);
    }));
    return $events;
}

Here is the comment from the sources:

Start with an Observable.of() the initial request, and run the handler (which includes all interceptors) inside a concatMap(). This way, the handler runs inside an Observable chain, which causes interceptors to be re-run on every subscription (this also makes retries re-run the handler, including interceptors).

So the $events stream is what is returned from http request methods and can be retried. The interceptors should always start with the original request. If the request is mutable and can be modified during the previous run of interceptors, this condition can't be met. So the request and all its constituent parts should be immutable.

这篇关于为什么大多数HttpClient API类都定义不可变对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆