zoukankan      html  css  js  c++  java
  • [RxJS] Subject asObservable() method

    You can create your own state store, not using any state management libraray.

    You might have seen the partten: People create a Subject, then use abObservable() method.

    export class State {
      private prevState: INEO[];
      private neoStoreSubject: BehaviorSubject<INEO[]>;
      neoStore$: Observable<INEO[]>;
    
      protected constructor() {
        this.neoStoreSubject = new BehaviorSubject([]);
        this.neoStore$ = this.neoStoreSubject.asObservable();
      }
    
      ...
    }

    Main reason for that is, we want to keep:

    this.neoStoreSubject = new BehaviorSubject([]);

    as private, we don't want any component can call .next() method to update store. The job for updating store should only happen in 'State' class.

    // State class
    setNeoStore(neoList: INEO[]) {
    this.setPrevState(); this.neoStoreSubject.next(neoList); this.dismissError(); }

    For component, we can subscribe this.neoStore$. it can only receive the data, but not update the store directly.

    // From Component
    
      biggerFasterNeo$ = this.data.neoStore$.pipe(
        filter(neoList => !!neoList === true),
        map(neoList => neoList.filter(neo => {
          if (neo.estimated_diameter > 0.5 || neo.relative_velocity > 50000) {
            return neo;
          }
        }))
      );

    For component

  • 相关阅读:
    个人博客12
    《梦断代码》阅读笔记03
    个人博客11
    个人博客10
    【Codeforces 404C】Restore Graph
    【Codeforces 476C】Dreamoon and Sums
    【Codeforces 242C】King's Path
    【Codeforces 382C】Arithmetic Progression
    【Codeforces 1096D】Easy Problem
    【Codeforces 494A】Treasure
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11738664.html
Copyright © 2011-2022 走看看