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

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

问题描述

HttpClient 文档 陈述了以下关于不变性的内容:

The documentation for HttpClient states the following about immutability:

拦截器的存在是为了检查和改变传出的请求和传入的响应.然而,得知这一点可能会令人惊讶HttpRequest 和 HttpResponse 类在很大程度上是不可变的.

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?

推荐答案

这个解释在不知道的情况下很难理解 源代码.它在文章 Insider's 中进行了深入解释Angular 拦截器和 HttpClient 机制指南.

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.

当您调用 http 的任何方法时,例如 get,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:

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

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).

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

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天全站免登陆