组合多个条件可观察对象以准备数据 [英] Combining multiple conditional Observables for preparing the data

查看:52
本文介绍了组合多个条件可观察对象以准备数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 stores ,我需要准备合并的数据

I have two stores which I need to prepare the combined data

  1. 获取所有部门(例如前50个部门)
  2. 获取所有用户(例如: 1000 个用户)

我想合并两个数据并准备最终数据.注意:该部门将要求加载所有用户(这可能需要一些时间),显然,用户不能根据自己的需要执行任何操作部门数据以准备最终结果.

I want to merge both the data and prepare final data. Note: the department would require all users to be loaded (which might take some time), and obliviously, users can't do anything on their own as they need department data to prepare the final result.

这里是部门的地方:

      this.store.select(getUserInfo).subscribe((info) => {
        this.store.select(getAllDepts).subscribe((depts) => {
          if (depts.length < 1) {
            this.store.dispatch(loadDeptStore({ accountId: info.acntId}));
          } else {
            console.log(depts);  // get Dept data
          }
        });
      })

我拥有的 Users 数据:

      this.store
        .select(getUserInfo)
        .pipe(
          flatMap((info) => {
            this.acntName = info.acntId;
            return this.store.select(getAllUsers, { accountId: info.acntId });
          })
        )
        .subscribe((res) => {
          if (res.length < 1 ) {
            this.store.dispatch(loadUsersInAccountStore({ billingCrmAccountId: this.acntName }));
          } else {
            console.log(res); // get users data
          }
        })

我应该使用 combineLates 还是其他方法,如果是这样,我应该如何使用RxJS功能以反应方式进行操作.

Shall I use combineLates or something else, if so, how should I do it in reactive way using RxJS features.

推荐答案

首先,不要在订阅中进行订阅,这是一种反模式.始终使用较高的观测值.我不知道代码的确切工作方式,因为我不使用ngrx,但是您可以像这样构建可观察对象:

First, don't make a subscription in a subscription, it's an anti-pattern. Always use higher observables. I don't know exactly how the code work since I don't use ngrx but you could build your observables like that:

    const userInfo$ = this.store.select(getUserInfo);
    userInfo$.pipe(
        mergeMap(info => {
            this.acntName = info.acntId;
            const departments$ = this.store.select(getAllDepts)
                .pipe(
                    map(depts => {
                        if (depts.length < 1) {
                            return this.store.dispatch(loadDeptStore({ accountId: info.acntId}));
                        } else {
                            return depts;  // get Dept data
                        }
                    })
                );
            const users$ = this.store.select(getAllUsers, { accountId: info.acntId })
                .pipe(
                    map(res => {
                        if (res.length < 1 ) {                          
                            return this.store.dispatch(loadUsersInAccountStore({billingCrmAccountId: this.acntName }));
                        } else {
                            return res; // get users data
                        }
                    })
                );
            return combineLatest([departments$, users$]); 
        }),
        map(([depts, users]) => {
            // do stuffs and return something
        })
    ).subscribe(res => console.log('results: ', res));
        

当两个可观察对象都发射一次时,CombineLatest就会发射.每次观察到的一个发射时,它都会再次发射.
它可能不适用于您当前的实现,但是您可以从一个来源了解如何合并两个可观察对象.

CombineLatest will emit when both your observables have emitted once. It will emit again each time one of the observables emits.
It may not work with your current implementation but you get the idea on how to combine two observables from one source.

这篇关于组合多个条件可观察对象以准备数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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