0

I am trying to figure out how to create an async observer and trigger/emit events at some time in the future without rebuilding the observable and the subscriber list.

Looking for something like:

MyAsyncObservable o = new MyAsyncObservable();

o.subscribe(s);
o.subscribe(s2);
while(scanner.hasNext()){
  o.emit(scanner.nextInt()); // emit T to subscribers.
}

Where MyAsyncObservable could just be a Observable.fromAsync(emitter,buffermode)

instead of

while(scanner.hasNext(){
  Observable<Integer> o = Observable.just(scanner.nextInt());
  o.subscribe(s);
  o.subscribe(s2);
}
trahane
  • 701
  • 6
  • 16
Jose Diaz
  • 13
  • 4

1 Answers1

0

If your observable is cold, just use .delay(); see f.e. this:

Observable.just("Hello!").delay(10, TimeUnit.SECONDS).subscribe(System.out::println);

If it's a hot observable, just add a buffer (assuming you don't expect more than 10 emissions in 10 seconds:

Observable
  .just("Hello!")
  .onBackpressureBuffer(10)
  .delay(10, TimeUnit.SECONDS)
  .subscribe(System.out::println);

Edit: Oh, I see - you want a Subject - Either PublishSubject or BehaviorSubject. Create them and feed them data via the usual onNext/onComplete/onError.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • Not really looking for a delayed send. More about figuring out how to make a hot observable that will emit events based on some other object( open connection, user input, etc). – Jose Diaz Aug 28 '16 at 13:14
  • and that is what I was looking for! No idea why they burry this as the last topic on the rxjava documentation. – Jose Diaz Aug 28 '16 at 22:40
  • @JoseDiaz because it's a tool best used sparingly; it breaks backpressure, it breaks unsubscription notification, and it's too easy to fall into a rut using it on every occasion that is slightly different than the documentation. that said, you don't *need* to use it for this case, but it makes life easier. – Tassos Bassoukos Aug 29 '16 at 07:22