简体   繁体   中英

Rxjs throttleTime works unexpected

(using angular 2 & NgRedux 2 with ngrx 5.0.3)

usersReducer$, articlesReducer$ are Anonymous Subjects I receive from an imported module (ngRedux).

let merge$ = Observable.merge(this.usersReducer$, this.articlesReducer$);
  .do((data) => {
    let time = performance.now();
    console.log('%c REGULAR UPDATE ' + time, 'background: #555; color: #bada55', data);
  });

// TODO : why it doesn't work as expected?!
// The merge$ emits values, and here it only emits once and happens again only
// when the reducers change much later.
this.throttleSub = merge$
  .throttleTime(0.0000000000001)
  .do((data) => {
    let time = performance.now();
    this.refreshData();
    console.log('%c THROTTLE UPDATE ' + time, 'background: #222; color: #bada55', data);
  })
  .subscribe(() => {})

Output : 在此输入图像描述

NOTE: If I there is any userReducer$/articleReducer$ emit later, We'll see throttle UPDATE log.

My best guess that things are happening much to fast (that's why I put 0.00000001 in the time between, because 1 ms will emit throttle one time at the start), but the ms difference between them just makes me feel wrong about it.

Would love some help, im so confused.

(with debounceTime(1) instead of throttle, we get only 1 debounce log at the end, it doesn't seems possible that everything is less then 1ms from each other ! there is a server request in the middle !)


EDIT: I've made .timestamp() operator as Mark van Straten stated in his comment. this is the output: 在此输入图像描述


UPDATE : I've chained the timestamp to the origin observable, and chained throttleTime into a different subscriber, I think now It's more clear. 在此输入图像描述

http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-throttleTime

Lets a value pass, then ignores source values for the next duration milliseconds.

So the behaviour is that it emits a value, then the timer runs and after the timer has ran to completion it will emit the next emitted value from upstream further downstream.

If you would make a marble diagram of this behaviour it would look something like this:

input stream:     --a-b-c---b----c--d----------a-|
throttleTime(50):   a-----| b-----| d-----|    a-|
output stream:    --a-------b-------d----------a-|

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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