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 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> 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) {
@ -348,13 +351,10 @@ public final class ChatMessage extends SupportMessage {
}
private void notifyChangeListener() {
if (listener != null) {
Listener listener = this.listener.get();
if (listener != null) {
listener.onMessageStateChanged();
}
}
}
@Override
public String toString() {

View file

@ -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)