improve reliability of chat messages ack by not using weak listeners

This commit is contained in:
woodser 2023-04-30 09:31:08 -04:00
parent 6030f1dd57
commit a2c0abcbc1
2 changed files with 15 additions and 12 deletions

View File

@ -36,7 +36,6 @@ import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -85,7 +84,7 @@ public final class ChatMessage extends SupportMessage {
private final StringProperty sendMessageErrorProperty; private final StringProperty sendMessageErrorProperty;
private final StringProperty ackErrorProperty; private final StringProperty ackErrorProperty;
transient private WeakReference<Listener> listener; transient private Listener listener;
public ChatMessage(SupportType supportType, public ChatMessage(SupportType supportType,
String tradeId, String tradeId,
@ -328,8 +327,12 @@ public final class ChatMessage extends SupportMessage {
return Utilities.getShortId(tradeId); return Utilities.getShortId(tradeId);
} }
public void addWeakMessageStateListener(Listener listener) { public void addChangeListener(Listener listener) {
this.listener = new WeakReference<>(listener); this.listener = listener;
}
public void removeChangeListener() {
this.listener = null;
} }
public boolean isResultMessage(Dispute dispute) { public boolean isResultMessage(Dispute dispute) {
@ -349,10 +352,7 @@ public final class ChatMessage extends SupportMessage {
private void notifyChangeListener() { private void notifyChangeListener() {
if (listener != null) { if (listener != null) {
Listener listener = this.listener.get(); listener.onMessageStateChanged();
if (listener != null) {
listener.onMessageStateChanged();
}
} }
} }

View File

@ -329,7 +329,7 @@ public class ChatView extends AnchorPane {
bg.setId("message-bubble-green"); bg.setId("message-bubble-green");
messageLabel.getStyleClass().add("my-message"); messageLabel.getStyleClass().add("my-message");
copyIcon.getStyleClass().add("my-message"); copyIcon.getStyleClass().add("my-message");
message.addWeakMessageStateListener(() -> updateMsgState(message)); message.addChangeListener(() -> updateMsgState(message));
updateMsgState(message); updateMsgState(message);
} else if (isMyMsg) { } else if (isMyMsg) {
headerLabel.getStyleClass().add("my-message-header"); headerLabel.getStyleClass().add("my-message-header");
@ -350,7 +350,7 @@ public class ChatView extends AnchorPane {
}; };
sendMsgBusyAnimation.isRunningProperty().addListener(sendMsgBusyAnimationListener); sendMsgBusyAnimation.isRunningProperty().addListener(sendMsgBusyAnimationListener);
message.addWeakMessageStateListener(() -> updateMsgState(message)); message.addChangeListener(() -> updateMsgState(message));
updateMsgState(message); updateMsgState(message);
} else { } else {
headerLabel.getStyleClass().add("message-header"); headerLabel.getStyleClass().add("message-header");
@ -693,10 +693,13 @@ public class ChatView extends AnchorPane {
} }
private void removeListenersOnSessionChange() { private void removeListenersOnSessionChange() {
if (chatMessages != null && disputeDirectMessageListListener != null) if (chatMessages != null) {
chatMessages.removeListener(disputeDirectMessageListListener); if (disputeDirectMessageListListener != null) chatMessages.removeListener(disputeDirectMessageListListener);
chatMessages.forEach(msg -> msg.removeChangeListener());
}
if (chatMessage != null) { if (chatMessage != null) {
chatMessage.removeChangeListener();
if (arrivedPropertyListener != null) if (arrivedPropertyListener != null)
chatMessage.arrivedProperty().removeListener(arrivedPropertyListener); chatMessage.arrivedProperty().removeListener(arrivedPropertyListener);
if (storedInMailboxPropertyListener != null) if (storedInMailboxPropertyListener != null)