From 46ebd66376a1affdab37eaf2f16bbaa026666cbb Mon Sep 17 00:00:00 2001 From: Pepijn Van Eeckhoudt Date: Thu, 12 May 2022 15:53:34 +0200 Subject: [PATCH] Adapt forceMessageHandling --- .../client/communication/MessageHandler.java | 22 +++++++------ .../client/communication/MessageSender.java | 31 ++++++++++++++++--- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/flow-client/src/main/java/com/vaadin/client/communication/MessageHandler.java b/flow-client/src/main/java/com/vaadin/client/communication/MessageHandler.java index b888a8fa536..09ab36df996 100644 --- a/flow-client/src/main/java/com/vaadin/client/communication/MessageHandler.java +++ b/flow-client/src/main/java/com/vaadin/client/communication/MessageHandler.java @@ -227,8 +227,7 @@ protected void handleJSON(final ValueMap valueMap) { return; } - registry.getMessageSender() - .setResynchronizationState(ResynchronizationState.NOT_ACTIVE); + registry.getMessageSender().clearResynchronizationState(); if (hasResynchronize && !isNextExpectedMessage(serverId)) { // Resynchronize request. We must remove any old pending @@ -579,13 +578,6 @@ private int getExpectedServerId() { } private void forceMessageHandling() { - // Clear previous request if it exists. Otherwise resyncrhonize can - // trigger - // "Trying to start a new request while another is active" exception and - // fail. - if (registry.getRequestResponseTracker().hasActiveRequest()) { - registry.getRequestResponseTracker().endRequest(); - } if (!responseHandlingLocks.isEmpty()) { // Lock which was never release -> bug in locker or things just // too slow @@ -606,6 +598,18 @@ private void forceMessageHandling() { // has been lost // Drop pending messages and resynchronize pendingUIDLMessages.clear(); + + // Inform the message sender that resynchronize is desired already + // since endRequest may already send out a next request + registry.getMessageSender().requestResynchronize(); + + // Clear previous request if it exists. + if (registry.getRequestResponseTracker().hasActiveRequest()) { + registry.getRequestResponseTracker().endRequest(); + } + + // Call resynchronize to make sure a resynchronize request is sent in + // case endRequest did not already do this. registry.getMessageSender().resynchronize(); } } diff --git a/flow-client/src/main/java/com/vaadin/client/communication/MessageSender.java b/flow-client/src/main/java/com/vaadin/client/communication/MessageSender.java index e05667541c0..4e89be4e6c0 100644 --- a/flow-client/src/main/java/com/vaadin/client/communication/MessageSender.java +++ b/flow-client/src/main/java/com/vaadin/client/communication/MessageSender.java @@ -231,9 +231,9 @@ public String getCommunicationMethodName() { * state from the server */ public void resynchronize() { - Console.log("Resynchronize from server requested"); - resynchronizationState = ResynchronizationState.SEND_TO_SERVER; - sendInvocationsToServer(); + if (requestResynchronize()) { + sendInvocationsToServer(); + } } /** @@ -276,8 +276,29 @@ public void setClientToServerMessageId(int nextExpectedId, boolean force) { } } - void setResynchronizationState(ResynchronizationState state) { - resynchronizationState = state; + /** + * Modifies the resynchronize state to indicate that resynchronization is desired + * + * @return true if the resynchronize request still needs to be sent; false otherwise + */ + boolean requestResynchronize() { + switch (resynchronizationState) { + case NOT_ACTIVE: + Console.log("Resynchronize from server requested"); + resynchronizationState = ResynchronizationState.SEND_TO_SERVER; + return true; + case SEND_TO_SERVER: + // Resynchronize has already been requested, but hasn't been sent yet + return true; + case WAITING_FOR_RESPONSE: + default: + // Resynchronize has already been requested, but response hasn't been received yet + return false; + } + } + + void clearResynchronizationState() { + resynchronizationState = ResynchronizationState.NOT_ACTIVE; } ResynchronizationState getResynchronizationState() {