创建一个从订阅计算中获取其值的 Observable

Creating an Observable that gets its value from a subscriptions calculation

您好,我正在尝试创建一个 Observable,它会从另一个订阅中向其发送值,在本例中是一个 ngrx Store Reducer.

export class IsolatedAgentService {
missionList$: Observable<any>; // I need this observables subscription to emit to calculatedValue$
calculatedValue$:Observable<any>; // I need this observable to get its values from the subscription of  missionList$ subscription
missionList:any;
constructor(
    private _store:Store<any>
){
    this.missionList$ = this._store.select(root_reducers.getMissionList).pipe(skip(1));
    this.missionList$.subscribe((val:any)=> {
        let mostIsolatedCountry:any; //will hold value of calculation
        this.missionList = val;
        mostIsolatedCountry = this.getMostIsolatedCountry(this.missionList);
        // I want tot emit mostIsolatedCountry to another subscription
    });
}

我想做什么:

export class IsolatedAgentService {
  missionList$: Observable<any>;
  calculatedValue$:Observable<any> = Observable.create((observer)=>{
    // moved this line here from the previous missionList$ subscription
    let calculated:any = this.getMostIsolatedCountry(this.missionList);
    observer.next(calculated)
  });
  missionList:any;
  calculatedValue:any;
  constructor(
    private _store:Store<any>
  ){
    this.missionList$ = this._store.select(root_reducers.getMissionList).pipe(skip(1));
    this.missionList$.subscribe((val:any)=> {
        let mostIsolatedCountry:any; 
        this.missionList = val;
        this.calculatedValue$.subscribe((value)=>{
            this.calculatedValue = value;
        });
    });
}

目前,我基本上是在一个订阅中设置 class 属性,然后在同一个订阅中设置 class 属性 之后,我调用第二个订阅计算该集合的值 class 属性.

这感觉不对,我确定这不是做事的方法,但我目前缺乏 rxjs/observable 知识。

注意!我对通过 Store Action 发出计算值不感兴趣,我想要一个特定于 class 实例的 Observable。

这是您问题的答案:

export class IsolatedAgentService {
  missionList$: Observable<Mission[]>;
  calculatedValue$:Observable<any>;
  constructor(
    private _store:Store<any>
  ){
    this.missionList$ = this._store.select(root_reducers.getMissionList).pipe(skip(1));
    this.calculatedValue$ = this.missionList$.pipe( 
      map( missions => this.getMostIsolatedCountry(missions) )
    );
  }
}

甚至

this.calculatedValue$ = this.missionList$.pipe(
    map( this.getMostIsolatedCountry )
);

查看更多关于 NGRX 外观的信息:https://medium.com/@thomasburleson_11450/ngrx-facades-better-state-management-82a04b9a1e39

为什么你不公开和使用 observables 为什么还要订阅服务?

你应该拥有什么

export class IsolatedAgentService {
  missionList$: Observable<Mission[]>;
  calculatedValue$:Observable<any>;
  constructor(
    private _store:Store<any>
  ){
    this.missionList$ = this._store.select(root_reducers.getMissionList).pipe(skip(1));
    this.calculatedValue$ = this._store.select(root_reducers.getMissionCalculatedValue).pipe(skip(1));
  }
}

还有一个用于进行您需要的计算的选择器。

export const getMissionCalculatedValue= createSelector(
  getMissionList,
  (missionList) => {
    // do the calculations here
    return calculationResult;
  }
);