3

Is it possible to have a kind of BehaviorSubject with pause and resume switches? Something like PausableBehaviorSubject.pause() and PausableBehaviorSubject.resume()? How could that be done?

The idea is that, when paused, the subject would not forward the events received via PausableBehaviorSubject.onNext(), but instead only store the last one. Upon resuming (or upon subscription, if not paused), the PausableBehaviorSubject would forward the stored last event (if any) and forward the subsequent events to its subscriber(s).

Eduardo Bezerra
  • 1,923
  • 1
  • 17
  • 32
  • something like this : `subject.filter(i -> !isPaused).replay(1).autoconnect()` ? (not tested..) – dwursteisen May 31 '16 at 12:13
  • can't you just do `subscribe` and `unsubscribe` instead of `resume` and `pause`? – Samuel Gruetter Jun 01 '16 at 08:26
  • @SamuelGruetter the problem is that you'd be allowing the observer to control when it gets updates and when not. With a pausable observable, this could be encapsulated, and thus pausing/resuming would stay out of reach of the observer. – Eduardo Bezerra Jun 01 '16 at 10:00
  • What you want sounds like applying backpressure to the observable. You can introduce a `Subject` that applies backpressure based on its state. Any upstream observables are responsible for buffering results until the backpressure is lifted. – Bob Dalgleish Jul 11 '16 at 21:08

1 Answers1

0

There is an operator that allows you to do this. It is called pausableBuffered. You can see an animation at: RxMarbles.

Your observer or subject would simply package the original observable, add a new Boolean observable, and flip the switch by providing the value of true or false to your new observable.

Bob Dalgleish
  • 8,167
  • 4
  • 32
  • 42