代码之家  ›  专栏  ›  技术社区  ›  SeaFuzz

使用RXJS与管道连接防止泄漏,直到

  •  0
  • SeaFuzz  · 技术社区  · 7 年前

    当使用rxjs时,有一些困惑是 takeUntil 操作员的行为 combineLatest 使用时 pipe .

    有必要加上 取到 每一个内部可见的像这样包括一个管道 取到 :

    import {combineLatest} from 'rxjs';
    import {takeUntil} from 'rxjs/operators';
    

    combineLatest(
          this._store.pipe(select(stateOfLocation), takeUntil(this._destroy)),
          this._store.pipe(select(stateOfPlacesSelected), takeUntil(this._destroy))
        )
          .pipe(takeUntil(this._destroy$))
          .subscribe( ([location, places])  => {
            /* Business Logic */
          }); 
    

    …还是会 pipe(takeUntil(this._destroy$)) 处理来自 组合最 ,像这样:

    combineLatest(
          this._store.pipe(select(stateOfLocation)),
          this._store.pipe(select(stateOfPlacesSelected))
        )
          .pipe(takeUntil(this._destroy$))
          .subscribe( ([location, places])  => {
            /* Business Logic */
          });
    

    我的猜测是后者是正确的,但我想再次检查,以确保我是正确理解这一点。

    0 回复  |  直到 7 年前
        1
  •  0
  •   Zazaeil    7 年前

    根据经验:你是的 takeUntil 就一次。这就是模式的要点:把它放在一个地方。

    曾经的你 onNext(...) this._destroy$ , 取到 保证报告完成,这反过来会触发拆卸逻辑(其中包括取消订阅)。