为什么大多数 类 的 HttpClient API 定义不可变对象?
Why do most classes of the HttpClient API define immutable objects?
documentation for HttpClient 关于不变性的声明如下:
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.
我很难理解这个解释。有人可以解释一下吗?
如果不了解the source code. It's explained in depth in the article Insider’s guide into interceptors and HttpClient mechanics in Angular,这个解释很难理解the source code. It's explained in depth in the article Insider’s guide into interceptors and HttpClient mechanics in Angular。
当您调用 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;
}
来自消息来源的评论如下:
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 请求方法返回的,可以 重试 。拦截器应始终从原始请求开始。如果请求是可变的,并且可以在之前的 运行 拦截器期间进行修改,则无法满足此条件。所以请求及其所有组成部分应该是不可变的。
documentation for HttpClient 关于不变性的声明如下:
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.
我很难理解这个解释。有人可以解释一下吗?
如果不了解the source code. It's explained in depth in the article Insider’s guide into interceptors and HttpClient mechanics in Angular,这个解释很难理解the source code. It's explained in depth in the article Insider’s guide into interceptors and HttpClient mechanics in Angular。
当您调用 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;
}
来自消息来源的评论如下:
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 请求方法返回的,可以 重试 。拦截器应始终从原始请求开始。如果请求是可变的,并且可以在之前的 运行 拦截器期间进行修改,则无法满足此条件。所以请求及其所有组成部分应该是不可变的。