简体   繁体   English

Rxjs - 重新订阅未订阅的Observable

[英]Rxjs - resubscribe to unsubscribed Observable

I'm using a Service to "ping" my server every 2.5s, returning the response time from my server. 我每隔2.5秒使用一次服务“ping”我的服务器,从我的服务器返回响应时间。 Therefore I am using observables. 因此我使用的是可观测量。

I am also using angular 2 and typescript. 我也使用角度2和打字稿。

I now want to stop the service (unsubscribe) on button click. 我现在想要在点击按钮时停止服务(取消订阅)。 This works just fine! 这很好用! The button should be a togglebutton, so if not subscribed, subscribe and other way around. 按钮应该是一个togglebutton,所以如果没有订阅,订阅和其他方式。 But resubscribing doesn't work! 但重新订阅不起作用!

Here is my service: 这是我的服务:

export class PingService {
  pingStream: Subject<number> = new Subject<number>();
  ping: number = 0;
  url: string = url.href;

  constructor(private _http: Http) {
    Observable.interval(2500)
      .subscribe((data) => {
        let timeStart: number = performance.now();

        this._http.get(this.url)
          .subscribe((data) => {
            let timeEnd: number = performance.now();

            let ping: number = timeEnd - timeStart;
            this.ping = ping;
            this.pingStream.next(ping);
          });
      });
  }
}

And here is my function on click: 这是我的点击功能:

toggleSubscription() {   
      if (this.pingService.pingStream.isUnsubscribed) {
         this.pingService.pingStream.subscribe(ping => {
         this.ping = ping;
         NTWDATA.datasets[0].data.pop();
         NTWDATA.datasets[0].data.splice(0, 0, this.ping);
      })
      }
      else {
         this.pingService.pingStream.unsubscribe();
      }
   }

I am subscribing to the PingService within the cunstructor of my appcomponent. 我在appcomponent的cunstructor中订阅了PingService。 The data gets displayed in a chart. 数据显示在图表中。 When I click the button for the first time, it stops the service, no data updates anymore. 当我第一次单击该按钮时,它会停止服务,不再有数据更新。 When I click the next time, nothing happens, although the "this.pingService.pingStream.isUnsubscribed" returns true. 当我下次点击时,没有任何反应,尽管“this.pingService.pingStream.isUnsubscribed”返回true。

my constructor: 我的构造函数:

    constructor(private location: Location,
       private pingService: PingService) {

          this.pingService.pingStream.subscribe(ping => {
             this.ping = ping;
             NTWDATA.datasets[0].data.pop();
             NTWDATA.datasets[0].data.splice(0, 0, this.ping);
          })
   }

I am also getting an error "ObjectUnsubscribedError" when I click the button for the first time. 我第一次点击按钮时也收到错误“ObjectUnsubscribedError”。

Any help is appreciated! 任何帮助表示赞赏! Thanks! 谢谢!

Since you are using RxJS you don't have to subscribe/unsubscribe. 由于您使用的是RxJS,因此您无需订阅/取消订阅。 Just consider another approach using Rx streams. 只考虑使用Rx流的另一种方法。 The idea is to have 2 streams main and toggle stream, so combined they fire events only when your toggle stream is on. 我们的想法是拥有2个maintoggle流,因此只有当您的toggle流打开时,它们才能组合起来。

var mainStream = Rx.Observable.interval(100).map(() => '.');

var toggleStream = Rx.Observable
                .fromEvent(toggle, 'change')
                .map(e => e.target.checked);

var resultStream = toggleStream
                    .filter(x => x === true)
                    .startWith(true)
                    .flatMap(() => mainStream.takeUntil(toggleStream));

resultStream.subscribe(x => display.innerText += x);

See sample fiddle 样品小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM