使用自定义 HTTP Interceptor 记录 SAP Sp
2021-11-30 本文已影响0人
华山令狐冲
最后的效果:

请求的方法类型和 url 都被记录了下来,同响应一起。

设置断点来观察自定义 HTTP interceptor 运行时原理:

我们自定义的 intercept 也是通过依赖注入的方式被注入到 httpInterceptor 的调度器里。

首先把 method 和 urlWithParams 从 request 里提取出来:

返回的 Observable pipe 方法里的 switchMap 里的处理函数,什么时候会被触发?

在响应式编程的世界里,不要太在意 switchMap 里指定的函数体是何时被调用的——充斥的全是 rxjs 的回调。

只需要注意 switchMap 里必须返回一个 Observable wrapper 对象。
再看这个 custom interceptor 什么时候被实例化的?

首先把系统所有的 HTTP_INTERCEPTORS 注入的实现都取出来,放到数组里,然后使用数组的 reduceRight 方法,逐一调用之。
我们自定义的 Custom HTTP Interceptor 赫然在数组里,所以可以被调用到:

更多Jerry的原创文章,尽在:"汪子熙":
