From a2c0abcbc1959fd5e9c7806c59c7fdb7b9628d5b Mon Sep 17 00:00:00 2001 From: woodser Date: Sun, 30 Apr 2023 09:31:08 -0400 Subject: [PATCH] improve reliability of chat messages ack by not using weak listeners --- .../core/support/messages/ChatMessage.java | 16 ++++++++-------- .../haveno/desktop/main/shared/ChatView.java | 11 +++++++---- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/haveno/core/support/messages/ChatMessage.java b/core/src/main/java/haveno/core/support/messages/ChatMessage.java index ff91cd7f17..eedf4bd190 100644 --- a/core/src/main/java/haveno/core/support/messages/ChatMessage.java +++ b/core/src/main/java/haveno/core/support/messages/ChatMessage.java @@ -36,7 +36,6 @@ import lombok.Setter; import lombok.extern.slf4j.Slf4j; import javax.annotation.Nullable; -import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -85,7 +84,7 @@ public final class ChatMessage extends SupportMessage { private final StringProperty sendMessageErrorProperty; private final StringProperty ackErrorProperty; - transient private WeakReference listener; + transient private Listener listener; public ChatMessage(SupportType supportType, String tradeId, @@ -328,8 +327,12 @@ public final class ChatMessage extends SupportMessage { return Utilities.getShortId(tradeId); } - public void addWeakMessageStateListener(Listener listener) { - this.listener = new WeakReference<>(listener); + public void addChangeListener(Listener listener) { + this.listener = listener; + } + + public void removeChangeListener() { + this.listener = null; } public boolean isResultMessage(Dispute dispute) { @@ -349,10 +352,7 @@ public final class ChatMessage extends SupportMessage { private void notifyChangeListener() { if (listener != null) { - Listener listener = this.listener.get(); - if (listener != null) { - listener.onMessageStateChanged(); - } + listener.onMessageStateChanged(); } } diff --git a/desktop/src/main/java/haveno/desktop/main/shared/ChatView.java b/desktop/src/main/java/haveno/desktop/main/shared/ChatView.java index cc159baeed..fcb0d3d745 100644 --- a/desktop/src/main/java/haveno/desktop/main/shared/ChatView.java +++ b/desktop/src/main/java/haveno/desktop/main/shared/ChatView.java @@ -329,7 +329,7 @@ public class ChatView extends AnchorPane { bg.setId("message-bubble-green"); messageLabel.getStyleClass().add("my-message"); copyIcon.getStyleClass().add("my-message"); - message.addWeakMessageStateListener(() -> updateMsgState(message)); + message.addChangeListener(() -> updateMsgState(message)); updateMsgState(message); } else if (isMyMsg) { headerLabel.getStyleClass().add("my-message-header"); @@ -350,7 +350,7 @@ public class ChatView extends AnchorPane { }; sendMsgBusyAnimation.isRunningProperty().addListener(sendMsgBusyAnimationListener); - message.addWeakMessageStateListener(() -> updateMsgState(message)); + message.addChangeListener(() -> updateMsgState(message)); updateMsgState(message); } else { headerLabel.getStyleClass().add("message-header"); @@ -693,10 +693,13 @@ public class ChatView extends AnchorPane { } private void removeListenersOnSessionChange() { - if (chatMessages != null && disputeDirectMessageListListener != null) - chatMessages.removeListener(disputeDirectMessageListListener); + if (chatMessages != null) { + if (disputeDirectMessageListListener != null) chatMessages.removeListener(disputeDirectMessageListListener); + chatMessages.forEach(msg -> msg.removeChangeListener()); + } if (chatMessage != null) { + chatMessage.removeChangeListener(); if (arrivedPropertyListener != null) chatMessage.arrivedProperty().removeListener(arrivedPropertyListener); if (storedInMailboxPropertyListener != null)