RxJs:经过一段时间后才有新值

RxJs: New value only after a period of time has passed

我是 ReactiveExtensions 的新手,我还没有开始工作 s.th。我认为应该是一个非常常见的用例。我只想在没有新的下一个值的特定时间段过去后才收到一个新值。在下面的示例中,这段时间是 1 秒。 debounce-operator 似乎完全符合我的要求。我没有让它工作。

const observable$ = new Rx.Observable(observer => {

 observer.next('start');

 setTimeout(() => {
  observer.next(1);
 }, 100);
 setTimeout(() => {
  observer.next(2);
 }, 200);
 setTimeout(() => {
  observer.next(3);
 }, 300);
 setTimeout(() => {
  observer.next(4);
 }, 400);

 setTimeout(() => {
  observer.next('end');
 }, 1500);
});

let sub = observable$
      .debounce(1000) //debounce(1000, null) does not work either
      .take(100)
      .subscribe(data => {
          console.log(data);
         }, 
         err => console.log(err.message), 
         complete => console.log('Observable completed')
       )

我想要得到的只是一个控制台输出:

"start"
"end"

在我的 IDE (Webstorm) 中,尽管文档指出第二个参数是可选的,但上述代码甚至无法编译。在 jsbin.com i get the following error: "this.durationSelector.call is not a function" (I admit, I dont know yet how to apply schedulers in rxjs). In the documentation they use only a number, too. Most examples of debounce i found on google use only a number, i.e. this 上。为什么这对我不起作用?

感谢您的帮助!

PS: 我使用 rxjs 5.0.0-beta.6.

编辑:借助这里的答案,我确实找到了我想要的实际解决方案:

const observable$ = new Rx.Observable(observer => {

observer.next('start');

 setTimeout(() => {
  observer.next(1);
 }, 1100); //<-- If you change 1100 to i.e. 900 you just get "end" in the output, because there is no 1s periode during which no new value arrives. 
 setTimeout(() => {
  observer.next(2);
 }, 1200);
 setTimeout(() => {
  observer.next(3);
 }, 1300);
 setTimeout(() => {
  observer.next(4);
 }, 1400);
 setTimeout(() => {
  observer.next(5);
 }, 1500);

 setTimeout(() => {
  observer.next('end');
 }, 1501);


});

let sub = observable$
      .debounceTime(1000)
      .take(10)
      .subscribe(data => {
         console.log(data);
       }, 
       err => console.log(err.message), 
       complete => console.log('Observable completed')
);

你可以这样做

const observable$ = new Rx.Observable(observer => {

    observer.next('start');

    setTimeout(() => {
        observer.next(1);
    }, 100);
    setTimeout(() => {
        observer.next(2);
    }, 200);
    setTimeout(() => {
        observer.next(3);
    }, 300);
    setTimeout(() => {
        observer.next(4);
    }, 400);

    setTimeout(() => {
        observer.next('end');
    }, 1500);


});

let sub = observable$
    .map(function(x, i) {
        return {
            val: x,
            index: i
        };
    })
    .debounce(function(obj) {
        let interval = obj.index === 0 ? 0 : 1500;
        return Rx.Observable.timer(interval);
    })
    .take(100)
    .subscribe(data => {
            console.log(data.val);
        },
        err => console.log(err.message),
        complete => console.log('Observable completed')
    )

这里的关键是使用map函数获取元素的索引,然后决定等待的时间间隔。

这只是旧版 RxJS 文档的问题。 RxJS 4 overloads debounce here

但是,RxJS 5 splits the operator so that there are actually two, one that uses a selector function to determine the debounce length and the second debounceTime接受时间。

因此您的信息流变为:

let sub = observable$
      .debounceTime(1000) //debounce(1000, null) does not work either
      .take(100)
      .subscribe(data => {
          console.log(data);
         }, 
         err => console.log(err.message), 
         complete => console.log('Observable completed')
       )