Angular 2+使用RxJS-关于take(1)和first()的完整与退订

问题描述 投票:1回答:1

通过阅读一些文档和问题,我发现对于first()或take(1)在完成时实际上要退订的天气还有些不清楚。我想我的困惑是围绕“完成”还是“退订”。说一个可观察的完成,这是否也意味着订阅已取消订阅?我正在考虑将其用于垃圾回收,而我需要知道,在first()或take(1)完成之后,可观察对象没有保留任何引用。

如果这些功能没有取消订阅,我需要知道完成后最简单的取消订阅方法。还是有必要吗?

angular rxjs angular2-observables
1个回答
8
投票

源代码中的几件事,

[(do)first()和take(1)完成时实际上会退订

看起来会这样。

take.ts

protected _next(value: T): void {
  const total = this.total;
  const count = ++this.count;
  if (count <= total) {
    this.destination.next(value);
    if (count === total) {
      this.destination.complete();
      this.unsubscribe();
    }
  }
}

说一个可观察的对象完成了,这是否也意味着订阅已取消订阅?

Subscription.ts中,遇到了此问题(在文档中未看到)

/**
 * Adds a tear down to be called during the unsubscribe() of this
 * Subscription.
 *
   ...
 */
add(teardown: TeardownLogic): Subscription {

所以我认为可以使用拆解来验证是否已取消订阅。

const source1 = Observable.range(1, 10).take(6)

const subscription1 = source1.subscribe(x => console.log('subscription1'))
  .add(() => console.log('teardown1'))
// Emits 6x then 'teardown1'

const subscription2 = source1.take(4).subscribe(x => console.log('subscription2'))
  .add(() => console.log('teardown2'))
// Emits 4x then 'teardown2'

但请注意,take()仅在其下游取消订阅,而不是all可观察者的订阅者

const source2 = Observable.range(1, 10)

const subscription3 = source2.subscribe(x => console.log('subscription3'))
  .add(() => console.log('teardown3'))
// Emits 10x then 'teardown3'

const subscription4 = source2.take(5).subscribe(x => console.log('subscription4'))
  .add(() => console.log('teardown4'))
// Emits 5x then 'teardown4'

我需要知道在first()或take(1)完成之后,可观察对象没有保留任何引用。

这有点棘手,这里是Observable.subscribe()方法。似乎观察者没有保留对这三个参数中任何一个的引用,而是订阅保留了对可观察对象的引用。

subscribe(observerOrNext?: PartialObserver<T> | ((value: T) => void),
          error?: (error: any) => void,
          complete?: () => void): Subscription {

  const { operator } = this;
  const sink = toSubscriber(observerOrNext, error, complete);

  if (operator) {
    operator.call(sink, this.source);
  } else {
    sink.add(this.source ? this._subscribe(sink) : this._trySubscribe(sink));
  }

  if (sink.syncErrorThrowable) {
    sink.syncErrorThrowable = false;
    if (sink.syncErrorThrown) {
      throw sink.syncErrorValue;
    }
  }
  return sink;
}

这可以在下面的测试代码中看到。订阅处于活动状态(关闭:false)时,订阅者在_subscriptions中引用了可观察的内容>

const source3 = Observable.interval(1000)
const subscription5 = source3.subscribe(x => {})
console.log(source3)
console.log(subscription5)

控制台输出:

IntervalObservable 
  period: 1000
  scheduler: AsyncScheduler {...}
  _isScalar: false
  __proto__: Observable

Subscriber 
  closed: false
  destination: SafeSubscriber {...}
  isStopped: false
  syncErrorThrowable: false
  syncErrorThrown: false
  syncErrorValue: null
  _parent: null
  _parents: null
  _subscriptions: [AsyncAction]
  __proto__: Subscription

但是当我们用take(1)关闭订阅时,

const source3 = Observable.interval(1000).take(1)
const subscription5 = source3.subscribe(x => {})
console.log(source3)
console.log(subscription5)

Subscriber _subscriptions设置为null,释放引用。

Subscriber 
  closed: true
  destination: SafeSubscriber {...}
  isStopped: true
  syncErrorThrowable: false
  syncErrorThrown: false
  syncErrorValue: null
  _parent: null
  _parents: null
  _subscriptions: null
  __proto__: Subscription

我不确定这是否可以视为所有可观察/运营商/订户链的确定证明,但至少表明可以验证您的特定用例。

© www.soinside.com 2019 - 2024. All rights reserved.