How to pause an observable

How do I actually pause the observable rxjs?

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'm expecting a start message on the console, right after subscribe than 1s break, than one subscribe message, than 2s break, and the last one to subscribe

but after a one-time break I got the last two subscriptions at once . It seems I can only pause observables once.

For anyone wondering what I want to achieve: I want to have an event queue and I want to receive the next event if some callback is called for the previous one (event completed. Just a simple setTimeout)

+3


source to share


1 answer


pausableBuffered retains the buffer when suspended and depletes the buffer when calling resume. What you want is more like a controlled observable where you speak source.request(1)

.

See the rxjs docs on backpressure for more information .



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')
      

Run codeHide result


+2


source







All Articles