简体   繁体   English

如何在 RXJS 中启动和停止可观察的间隔?

[英]how can start and stop an interval observable in RXJS?

I have a very simple timeInterval observable and I want to start/stop transmission without disconnecting subscribers (which should sit and wait regardless of observable status).我有一个非常简单的 timeInterval observable,我想在不断开订阅者的情况下开始/停止传输(无论可观察状态如何,都应该坐等)。 Is possible, and if so how?有没有可能,如果有怎么办?

var source = Rx.Observable
  .interval(500)
  .timeInterval()
  .map(function (x) { return x.value + ':' + x.interval; })
  .take(10);

  var subscription = source.subscribe(
  function (x) {
     $("#result").append('Next: ' + x + ' ');
  },
  function (err) {
    $("#result").append('Error: ' + err);
  },
  function () {
    $("#result").append('Completed');
  });

general comment: most of the examples ive seen show how to define observables and subscribers.一般评论:我看到的大多数示例都展示了如何定义可观察对象和订阅者。 how do i affect the behavior of existing objects?我如何影响现有对象的行为?

Depends on what is the source of the stop/resume signal.取决于停止/恢复信号的来源。 The simplest way I can think about is with the pausable operator , which as the documentation says works better with hot observables.我能想到的最简单的方法是使用pausable operator ,正如文档所说,它更适合热观察。 So in the following sample code, I removed the take(10) (your pausable signal now comes through the pauser subject), and added share to turn your observable into a hot one.因此,在下面的示例代码中,我删除了take(10) (您的可暂停信号现在通过pauser主题pauser ),并添加了share以将您的 observable 变成热门的。

var pauser = new Rx.Subject(); var source = Rx.Observable .interval(500) .timeInterval() .map(function (x) { return x.value + ':' + x.interval; }) .share() .pausable(pauser); var subscription = source.subscribe( function (x) { $("#result").append('Next: ' + x + ' '); }, function (err) { $("#result").append('Error: ' + err); }, function () { $("#result").append('Completed'); }); // To begin the flow pauser.onNext(true); // or source.resume(); // To pause the flow at any point pauser.onNext(false); // or source.pause();

Here is a more sophisticated example which will pause your source every 10 items:这是一个更复杂的示例,它将每 10 个项目暂停您的源:

// Helper functions
function emits ( who, who_ ) {return function ( x ) {
 who.innerHTML = [who.innerHTML, who_ + " emits " + JSON.stringify(x)].join("\n");
};}

var pauser = new Rx.Subject();
var source = Rx.Observable
  .interval(500)
  .timeInterval()
  .map(function (x) { return x.value + ':' + x.interval; })
  .share();
var pausableSource = source
  .pausable(pauser);

source
  .scan(function (acc, _){return acc+1}, 0)
  .map(function(counter){return !!(parseInt(counter/10) % 2)})
  .do(emits(ta_validation, 'scan'))
  .subscribe(pauser);

var subscription = pausableSource.subscribe(
  function (x) {
     $("#ta_result").append('Next: ' + x + ' ');
  },
  function (err) {
    $("#ta_result").append('Error: ' + err);
  },
  function () {
    $("#ta_result").append('Completed');
});

You should have by now your answer to the second question.您现在应该已经知道第二个问题的答案了。 Combine the observables you are given with the relevant RxJS operators to realize your use case.将给定的 observable 与相关的 RxJS 运算符结合起来,以实现您的用例。 This is what I did here.这就是我在这里所做的。

not the most elegant, but probably the simplest:不是最优雅的,但可能是最简单的:

  timeSubscription: Subscription
  timer: Observable<number>;
  time = 0;

toggle() {
if (!this.timer)
  this.timer = interval(500);

if (!this.timeSubscription || this.timeSubscription.closed)
  this.timeSubscription = this.timer.subscribe(tick => { // running
    console.log(this.time++);
  });
else
  this.timeSubscription.unsubscribe(); // not running 
}

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

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