2016-08-05 1 views
0

エラーメッセージを表示するラベルがあります。ダブルクリックするとスタックトレース全体を表示する大きなダイアログが表示されます。私は唯一のショーerrosに観察可能な通知をフィルタリングし、私が観測私のクリックの内側からそれから加入した場合、最後のエラーを再生他のオブザーバとのスイングイベント観測値の結合

final ConnectableObservable<Notification> errorNotifications = pm 
    .getNotificationObservable() 
    .filter(notification -> notification.getType().isError() && !notification.getLongMessage().isEmpty()) 
    .replay(1); 

errorNotifications.connect(); 

SwingObservable.fromMouseEvents(dialog.getMessagePanel().getMessageLabel()) 
       .map(MouseEvent::getClickCount) 
       .filter(number -> number >= 2) 
       .subscribe(integer -> errorNotifications 
        .take(1) 
        .subscribe(notification -> ErrorDialog.showError(dialog.getFrame(), "Error", notification.getLongMessage()))); 

:エラーの一つとクリックイベントのために1:私は2つの観測を持っています。

私の質問は、RxJavaにこれ以上のことをすることができる演算子がありますか?私はcombineLatest()を使用しようとしましたが、これは効果がありました。エラーが発生するたびにダイアログが開きます。

私は2つのオブザーバブルを持っています:1つは "マスター"のようなものです:マスターが観測可能(クリック観測可能)でアイテムを放出する場合、他の観測可能(エラー通知)は最新のアイテムを放出する必要があります。

答えて

2

サブスクリプションで別のObservableを使用することは、設計上の欠陥であることがよくあります。

このresponseflatMapオペレータを確認することができます。別のイベントを発生させたときにエラー通知を出すのに役立ちます。

あなたのコードでflatMap演算子を使用したい場合たとえば、それは次のように更新することができます。

final ConnectableObservable<Notification> errorNotifications = 
                pm.getNotificationObservable() 
                .filter(notification -> notification.getType().isError() && !notification.getLongMessage().isEmpty()) 
                .replay(1); 

errorNotifications.connect(); 

SwingObservable.fromMouseEvents(dialog.getMessagePanel().getMessageLabel()) 
      .map(MouseEvent::getClickCount) 
      .filter(number -> number >= 2) 
      .flatMap(integer -> errorNotifications.take(1)) 
      .subscribe(notification -> ErrorDialog.showError(dialog.getFrame(), "Error", notification.getLongMessage()))); 
+0

flatMapが動作しているようですが、私は基本的な技術を理解している場合はわからない。だから、 ReplaySubjectは、渡されたマッピング関数から結果として得られるobservableにflatMap()がサブスクライブすることを意味するサブスクリプションの最後の項目を放出します...? – morpheus05

+0

flatMapはラムダが返す観測値からすべての結果をマージします。 Replaysubjectを使用すると、最後のイベントとそれ以降のイベントが通知されます。 takeオペレーターでは、最後のイベントのたびに通知されます。 – dwursteisen

関連する問題