0

I have this class. I’d like, the client of this class gets the instance of the field chatHub as soon as chatHub assigned in the callback connection.connected(() -> chatHub = connection.createHubProxy("ChatHub")); chatHub might be null. We have to push chatHub through subscriber as soon as it has been initialized; any ideas?

public class SignalRManager {
    private HubProxy chatHub;

    public SignalRManager() {
        Platform.loadPlatformComponent(new AndroidPlatformComponent());

        HubConnection connection = new HubConnection("https://test.chatlasapp.com/signalr/hubs");
        connection.stateChanged((connectionState, connectionState2) -> Log.i("SignalR", connectionState.name() + "->" + connectionState2.name()));
        connection.closed(() -> {
            Log.i("SignalR", "Closed");
            chatHub = null;
            connection.start();
        });
        //As soon as HubConnection connected this callback invokes.
        connection.connected(() -> chatHub = connection.createHubProxy("ChatHub"));

        connection.start();
    }

    Observable<HubProxy> getHubProxy(){

    }
}

I'm wondering how to implement getHubProxy method properly? Thanks in advance!

beka
  • 11
  • 3

1 Answers1

0

You can emit chatHub instance as soon it's inialized thought a PublishSubject. Then getHubProxy just return this subject, that will be configured with a replay of one, for futher subscription.

public class SignalRManager {
     private HubProxy chatHub;
     private PublishSubject<HubProxy> subject = PublishSubject.create();

     public SignalRManager() {
          connection = ... // code removed for clarity
          connection.connected(() -> {
                chatHub = connection.createHubProxy("ChatHub")
                subject.onNext(chatHub);
                subject.onCompleted();  
         });

         connection.start();
    }

    Observable<HubProxy> getHubProxy(){
         return subject.replay(1).autoconnect();
    }
 }

Please note that keeping hubProxy as a parameter is not more relevant then.

dwursteisen
  • 11,435
  • 2
  • 39
  • 36