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

  • 相关阅读:
    VS2005 GridView操作大全(转载)
    架构与模式
    JS全选与取消
    C#查找指定文件夹下指定后缀名的所有文件
    select poll epoll Hello
    scanf() gets() fgets()使用注意事项 Hello
    gtk_init() Hello
    用C实现FFT算法 Hello
    时间相关函数 Hello
    gcc的编译属性和选项 Hello
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11738664.html
Copyright © 2011-2022 走看看