4

I think I don't understand difference between sample and throttle correctly.

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

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

They are both used to silence observable. Sample uses notifier to emit values and throttle uses function to determine how long it should ignore values?

Is that correct?

2 Answers 2

4

In below examples :

//emit value every 1 second const source = Rx.Observable.interval(1000); 

Throttle :

//throttle for 2 seconds, emit latest value const throttle = source.throttle(val => Rx.Observable.interval(2000)); //output: 0...3...6...9 throttle.subscribe(val => console.log(val)); 

Sample :

//sample last emitted value from source every 2s const sample = source.sample(Rx.Observable.interval(2000)); //output: 2..4..6..8.. sample.subscribe(val => console.log(val)); 

As you can see, Sample picked up the latest emitted event (0, 2,...), whereas Throttle turned off the stream for 2 seconds and waited for the next one to be emitted ( 0, 3, 6,...).

Sign up to request clarification or add additional context in comments.

2 Comments

This site uses an ascii diagramming technique that was very helpful. I would like to see how that looks on this example. See gist.github.com/staltz/868e7e9bc2a7b8c1f754. It basically shows a line for the data then a line for the transform then another line for the data after the transform. (in case the link stops working). Look for clickStream.map(f).scan(g).
think about it like this, sample looks backwards and always gets you something, where throttle looks fwd and gets the next event after the allowed time window.
3

Throttle ignores every events in the time interval. So, if your notifier emits an events, all the previous events from the source are ignored (and deleted).

Sample returns the last event since the last sample. So if the notifier emits an event, it will look from the source events the latest one from the last sampling.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.