Angular 5 RxJs concatMap,switchMap,mergeMap哪个? [英] Angular 5 RxJs concatMap,switchMap,mergeMap which?

查看:90
本文介绍了Angular 5 RxJs concatMap,switchMap,mergeMap哪个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此方法可通过localstorage获取令牌,如果令牌不存在或已过期,我将调用API以获取另一个令牌并将其存储到localstorage.

I have this method to get token via localstorage, if token is not exist or is expired, I will call API to get another token and store to localstorage.

在这种情况下,我应该使用哪个地图,当前是否使用mergeMap或其他方式?

In this case, which map should I use, currently using mergeMap, or other way to do this?

public doGetToken():Observable<Token> {
    return this.loadToken().pipe( //get via localstorage
      map(token=>{
        let valid = this.validateTokenIsValid(token);
        let data = {
          token: token,
          valid: valid
        };
        return data;
      }),
      mergeMap(data=>{
        if (!data.valid) {
          return this.doApiGetToken(data.token).pipe(
            map(
              token=>{
                this.saveToken(token); //save to localstorage
                return token;
              }
            )
          );
        } else {
          return of(data.token);
        }
      })
    );

版本:Angular 5,rxjs5

version: Angular 5, rxjs5

谢谢.

推荐答案

如果您仅提出一个请求,则没关系使用哪种地图.

If you only make one request, then it doesn't matter which map you use.

mergeMap(也称为flatMap),concatMap,exhaustMap或switchMap的行为相同.

mergeMap (also called flatMap), concatMap , exhaustMap or switchMap will behave the same.

当您发出多个值时,这些运算符的行为会有所不同:

These operators behave differently when you emit more than 1 value:

switchMap

会将映射应用于收到的最新输入

will apply the mapping to the latest input received:

Src : -----A----B----C--D-E-------

switchMap (x => x--x) // emit x twice when received

Out:  ------A--A-B--B-C-D-E--E----

concatMap

将在完成另一个输入之前完成映射:

will finish the mapping before taking another input:

Src : -----A----B----C--D-E-----------

concatMap (x => x--x) // emit x twice when received

Out:  ------A--A-B--B-C--C--D--D-E--E

合并地图

类似于concatMap,但是它不等待映射完成.结果可能会重叠:

is like concatMap, but it doesn't wait for mapping to complete. The results can overlap though:

Src : -----A----B----C-D---E-----------

mergeMap (x => x--x) // emit x twice when received

Out:  ------A--A-B--B-C-D-C-D-E--E-----

排气图

就像一个反向的switchMap,它为输出赋予优先级:

is like a reversed switchMap, it gives priority to the output:

Src : -----A--------B----C-D---E-----------

exhaustMap (x => x--x--x) // emit x thrice when received

Out:  ------A--A--A--B--B--B-D--D--D-------

有关更多信息:

https://medium.com/@ vdsabev/the-simple-difference-between-rxjs-switchmap-and-mergemap-397c311552a5

大理石图:

http://rxmarbles.com/#mergeMap

我将代码的简化移到底部,以使一般信息一目了然.

Edit : I moved the simplification of your code to the bottom to make the general information visible at first sight.

public doGetToken(): Observable<Token> {
  return this.loadToken()
    .pipe( //get via localstorage
      mergeMap(token => {
        if(!this.validateTokenIsValid(token))
          return of(token)
        return this.doApiGetToken(token)
          .pipe(
            tap( token => this.saveToken(token)) //save to localstorage
          );
      })
    )
};

这篇关于Angular 5 RxJs concatMap,switchMap,mergeMap哪个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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