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

  • 相关阅读:
    nginx rewrite 伪静态重写学习笔记
    正则表达式相关知识
    rpm的含义
    find命令的使用
    chmod的运用方式
    [GO]数组的比较和赋值
    [GO]二维数组的介绍
    [GO]变量内存和变量地址
    [GO]给导入包起别名
    阿里云负载均衡SLB 七层https协议 nginx 获取真实IP
  • 原文地址:https://www.cnblogs.com/Answer1215/p/11738664.html
Copyright © 2011-2022 走看看