How to emulate a BehaviorSubject with a connectable Observable in RX-Scala

StackOverflow https://stackoverflow.com/questions/21461990

  •  05-10-2022
  •  | 
  •  

سؤال

Is there a way to make an Observable emulate a BehaviorSubject (but without the Observer interface) in rx-scala? I.e. make it an Observable with memory, so that it can have multiple subscriptions, and on each new subscription, it produces the last emitted value?

Observable.publish() does half the job, but it doesn't emit the last value. Observable.cache.publish() on the other hand replays all values - I would need something like that, but which only replays the last emitted value, to handle infinite streams.

Rx-Java solutions also accepted, although the native Scala form is preferred!

هل كانت مفيدة؟

المحلول 2

With the scala bindings, use observable replay 1 refCount.

نصائح أخرى

How about simply using the existing BehaviorSubject Scala implementation? As you can see, it's certainly available in 0.16.0, and I'm certain 0.15.0 includes it as well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top