Q:

RxJava: Watch custom class

I have a custom class, call it Builder.  It has a method act(), that performs an async operation.  When the operation is done, I want to emit an event.
How can I accomplish this in RxJava?
Seems like a straightforward task, but I have yet to see the right RxJava way to do it.
For more detail, a typical act() method looks like so:
    class Builder
       public void act() {
            processRunner.runAsync(new ProcessRunner.ProcessResultHandler(){
                @Override
                public void onComplete(int result) {
                    // Emit event
                }
            });
        }

And what I want is in the consuming class, to call:
builder.act()

And somehow be alerted to when onComplete() is called.

I also control ProcessRunner, so I'd like to convert that to RxJava and chain results, but for now I'll take the simple usecase!

A:

That can possible using PublishSubject in RxJava.  Let me explain to you how you can use it into your builder. Create the class as shown below:
public class Communicator {
  private static Communicator  mCommunicator = new Communicator();
  private PublishSubject<Void> mGrabEvent = new PublishSubjetct.create();
  private Communicator(){
  }
  public static getCommunicator(){
   return mCommunicator
  }
  public static emitEventItem(){
     // you can pass any object that you want to listen
     mGrabEvent.onNext(null);
  }
 public PublishSubject<Void> subscribeEvent(){
   return mGrabEvent;
 }
}

This class would help you to emit & subscribe it for listening to that particular event anywhere into your application. Below I try to show you how to emit and hook that listener into you activity or fragment.
A. For emit the message from your builder class
class Builder{
  public void act() {
    processRunner.runAsync(new ProcessRunner.ProcessResultHandler(){
      @Override
      public void onComplete(int result) {
        // Emit event
        Communicatort.getCommunicator().emitEventItem();
      }
    });
  }
  }

B. Another end point where you would listen to it by subscribing it.
Communicatort.getCommunicator().subscribeEvent().subscribe(new Action<Void>(){
  @overrider
  public void call(Void void){
  }
});

C. In last, Make sure you should unsubscribe it onDestroy of the activity.
I hope that could help you. Let me know if you could understand in any point.
Thanks,
Bhavdip

