2016-08-05 6 views
0

오류 메시지를 표시하는 레이블이 있습니다. 두 번 클릭하면 전체 스택 추적을 보여주는 큰 대화 상자가 나타납니다. 오류에 대한 하나와 클릭 이벤트 하나 :스윙 이벤트 관찰 가능 항목을 다른 관측 가능 항목과 결합

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()))); 

내가 만 표시하는 오류에 대한 관찰 통지를 필터링하고 내가 관찰 내 클릭 내부에서에서 가입 한 경우 마지막 오류를 재생 나는이 명 관찰 가능한 있습니다.

내 질문은, 내가 더 할 수있는 RxJava에 연산자가 있습니까? 깔끔하게? 나는 combineLatest()을 사용하려고 시도했으나 효과가 있었기 때문에 대화가 열릴 때마다 오류가 발생했습니다.

두 가지 관측 가능 항목이 있습니다. 하나는 "마스터"와 같습니다. 마스터 관찰 가능 (클릭 관찰 가능) 항목을 방출하면 다른 관찰 가능 (내 오류 알림) 최신 항목을 방출해야합니다.

답변

2

서브 스크립 션에서 다른 Observable을 사용하는 것은 종종 설계상의 결함입니다.

response에서 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가 작동하는 것 같다,하지만 난 기본 기술을 이해하고 있는지 확실하지 않습니다 : 당신은 당신의 코드 flatMap 연산자를 사용하려는 경우

는 예를 들어,이 같은 업데이트 할 수 있도록 ReplaySubject는 구독에서 마지막 항목을 방출합니다. 이는 flatMap()이 전달 된 매핑 함수에서 결과로 나오는 관찰 가능 객체에 가입한다는 것을 의미합니다 ...? – morpheus05

+0

flatMap은 람다가 반환 한 관찰 결과의 모든 결과를 병합합니다. 따라서 Replaysubject를 사용하면 마지막 이벤트와 이후 이벤트에 대한 알림을 받게됩니다. 테이크 연산자를 사용하면 매번 마지막 이벤트 만 통보 받게됩니다. – dwursteisen

관련 문제