使用Angular中的RxJs,我只需要完成在组件中创建的Observable吗?

时间:2018-05-03 09:21:42

标签: angular typescript rxjs

基本上我还不太清楚Angular组件中反应代码的清理。以下规则是否正确且充分?

我对反应组件的规则:

    手册 x.subscribe()上的
    • 如果使用 x.takeUntil(...)或类似构造,则无需手动清理
    • 如果在组件范围内调用 x.complete(),则无需手动清理
    • 其他手动 .unsubscribe()按需或 ngOnDestroy()
  • 在模板' observable |上异步
    • 不需要手动清理加上onPush兼容
  • 手动直接创建Observable,如 new Subject()
    • 手动 .complete() ngOnDestroy()中,否则永远不会关闭
  • 使用其他Observables(例如 Observable.merge(...)进行手动创建:
    • 只要清理订阅,就不需要手动清理

我不确定最后一个要点。

以下是根据这些规则重构的实际组件的完整示例,有人可以看到是否存在内存泄漏的危险吗?所有公共$ observable都通过模板中的 async 管道进行订阅,因此我确信订阅已得到处理。使用 new 创建的主题会被手动清理,因此应该没问题。我担心的部分是 Observable.combineLatest(...) observables。

import { ChangeDetectionStrategy, Component, Input, OnDestroy } from '@angular/core';
import { Router } from '@angular/router';
import { DeliveryPointId } from 'app/bso';
import { BookmarkService } from 'app/general';
import { EAccess, EPermission, ReduxGetters, ReduxService } from 'app/redux';
import * as routing from 'app/routing';
import { BehaviorSubject, Observable } from 'app/rx';

@Component({
  selector: 'app-last-opened-workitems-widget',
  templateUrl: './last-opened-workitems-widget.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class LastOpenedWorkitemsWidgetComponent implements OnDestroy {

  private readonly showPartners$ = new BehaviorSubject(true);
  private readonly showServices$ = new BehaviorSubject(true);
  private readonly showTens$ = new BehaviorSubject(true);
  private readonly showWorklist$ = new BehaviorSubject(true);

  @Input() set showItems(val: boolean) { this.showWorklist$.next(!!val); }
  @Input() set showPartners(val: boolean) { this.showPartners$.next(!!val); }
  @Input() set showProcesses(val: boolean) { this.showServices$.next(!!val); }
  @Input() set showTens(val: boolean) { this.showTens$.next(!!val); }

  constructor(
    private readonly redux: ReduxService,
    private readonly bookmarks: BookmarkService,
    private readonly router: Router,
  ) { }

  canPartners$ = this.redux.watch(ReduxGetters.userAccess).map(access => access[EPermission.Partner] >= EAccess.Read);
  canServices$ = this.redux.watch(ReduxGetters.userAccess).map(access => access[EPermission.Services] >= EAccess.Read);
  canTens$ = this.redux.watch(ReduxGetters.userAccess).map(access => access[EPermission.Tens] >= EAccess.Read);
  canWorklist$ = this.redux.watch(ReduxGetters.userAccess).map(access => access[EPermission.Worklist] >= EAccess.Read);

  lastItems$ = this.bookmarks.onLastOpenedWorkitems.map(ii => [...ii].reverse());
  lastPartners$ = this.bookmarks.onLastOpenedPartners.map(ii => [...ii].reverse());
  lastProcesses$ = this.bookmarks.onLastOpenedProcesses.map(ii => [...ii].reverse());
  lastTens$ = this.bookmarks.onLastOpenedTens.map(ii => [...ii].reverse());

  hasContentPartners$ = Observable
    .combineLatest(
      this.showPartners$.distinctUntilChanged(),
      this.canPartners$,
      this.lastPartners$.map(ii => ii.length > 0))
    .map(oks => oks.every(ii => ii));

  hasContentServices$ = Observable
    .combineLatest(
      this.showServices$.distinctUntilChanged(),
      this.canServices$,
      this.lastProcesses$.map(ii => ii.length > 0))
    .map(oks => oks.every(ii => ii));

  hasContentTens$ = Observable
    .combineLatest(
      this.showTens$.distinctUntilChanged(),
      this.canTens$,
      this.lastTens$.map(ii => ii.length > 0))
    .map(oks => oks.every(ii => ii));

  hasContentWorklist$ = Observable
    .combineLatest(
      this.showWorklist$.distinctUntilChanged(),
      this.canWorklist$,
      this.lastItems$.map(ii => ii.length > 0))
    .map(oks => oks.every(ii => ii));

  hasContent$ = Observable
    .combineLatest(this.hasContentPartners$, this.hasContentServices$, this.hasContentTens$, this.hasContentWorklist$)
    .map(oks => oks.some(ii => ii));

  ngOnDestroy() {
    [this.showPartners$, this.showServices$, this.showTens$, this.showWorklist$].forEach(ii => ii.complete());
  }

  gotoPartner = (id: string) => routing.gotoPartnerItem(this.router, id);
  gotoProcess = (id: number) => routing.gotoProcess(this.router, id);
  gotoTensItem = (id: DeliveryPointId) => routing.gotoTensItem(this.router, id);
  gotoWorkitem = (id: number) => routing.gotoWorkitem(this.router, id);

}

2 个答案:

答案 0 :(得分:0)

您需要取消订阅每个订阅,例如ngOnDestroy()方法。

或者您可以使用takeUntil()take()first()等操作符'限制'可观察量。

以上规则也适用于combineLatest

不要担心AsyncPipe创建的订阅,因为AsyncPipe本身负责取消订阅自己的订阅。

关于这个主题的一个很好的读物是Ben Lesh的article,他是RxJS的领导者。

答案 1 :(得分:0)

好的,我实际上只是用 .finally(...)测试了它,加上所有中间Observable的记录,并在组件被销毁时被正确触发。

因此,我宣布上述规则是正确的。