Reputation: 5788
How to really pause an rxjs observable?
I have the following code:
var pauser = new Rx.Subject();
var animation = new Rx.Subject();
var source = animation.pausableBuffered(pauser);
source
.subscribe(function(frame) {
console.log('subscribe', frame);
setTimeout(function() {
source.resume();
}, frame.duration);
source.pause();
});
pauser.onNext(true);
console.log('start');
animation.onNext({ duration: 1000 });
animation.onNext({ duration: 2000 });
animation.onNext({ duration: 2000 });
http://jsfiddle.net/bbvarga/8yvLhjhe/
I expect a start message in the console, right after a subscribe, than 1s break, than one subscribe message, than 2s break, and the last subscribe
but after the one second break I got the two last subscribe message immediately. Seems like I can pause the observable only once.
For those who are interested in what do I want to achieve: I want to have a queue of events, and I want to receive the next event, if some callback is called for the previous one (the event finished. now it's just a simple setTimeout)
Upvotes: 4
Views: 3044
Reputation: 1319
pausableBuffered keeps a buffer when paused and drains the buffer when resume is called. What you want looks more like a controlled observable, where you say source.request(1)
.
See the rxjs docs on backpressure for more info.
var animation = new Rx.Subject();
var source = animation.controlled();
source
.subscribe(function(frame) {
console.log('new event', frame);
setTimeout(function() {
console.log('after timeout', frame);
source.request(1);
}, frame.duration);
});
source.request(1);
animation.onNext({ duration: 1000 });
console.log('animation.onNext 1');
animation.onNext({ duration: 2000 });
console.log('animation.onNext 2');
animation.onNext({ duration: 3000 });
console.log('animation.onNext 3')
Upvotes: 2