RxJS Does Observable empty the data array after onCompleted is called?

时间:2016-04-21 22:21:55

标签: reactive-programming rxjs

I am struggling with RxJS. Some questions about Observable

  1. Does Observable empty the data array after onCompleted is called?
  2. when I chain two subscribe() method together I got error "subscribe does not exist in type Subscription". Why is that?

    someObs.map(...).subscribe(...).subscribe(...)

  3. Is there a way to check an observable data array count without subscribing?

  4. If Observable clears the data items after emitting them is there a way to refill new data items into the same observable instance without creating a new one? if yes, how?

1 个答案:

答案 0 :(得分:1)

  1. 否。 RxJS是功能编程的近亲,这意味着突变通常是一个很大的禁忌。流数据的重点是避免状态突变,这在很多方面都是当今应用中许多问题的根源。
  2. 所有Observable都会包装各种数据源类型,以便它们通过公共接口发出事件。因此Rx.Observable.fromArrayRx.Observable.fromPromise都生成具有相同语义的Observable,唯一的区别是事件的数量以及生成这些事件的时间。

    1. Observables是懒惰的,所有花哨的方法链接都没有做任何事情,直到最终订阅了Observable。所以subscribe真的很像说"执行"或者" go",它将允许事件开始在Observable中移动。

    2. 不确定你的意思&#34; Observable data array&#34;,请参见第1点。如果将数组传递给Observable,那么你应该能够检查的大小< / EM>

    3. 它没有,但是从某种意义上说,你可以&#34;重新填充&#34;通过简单地重新订阅基于数组的Observable,将开始再次发送事件的过程。正如dvlsg所提到的,你也可以使用Subject将事件显式地推送到观察者,但在99%的情况下,它的使用可以而且应该避免,因为它被用作拐杖以避免实际上< EM>反应性

    4. 即。

      var source = Rx.Observable.fromArray([1, 2, 3, 4]);
      
      //First subscription, prints all of the events from array
      source.subscribe(x => console.log(x));
      
      //Second subscription, prints all the events *again* because
      //this is a new subscription occurrence
      source.subscribe(y => console.log(y));
      
相关问题