From 253c11850586844a26bf76b012a7b42a806ad3ca Mon Sep 17 00:00:00 2001
From: Denis Anisimov <denis@vaadin.com>
Date: Tue, 31 Jul 2018 15:37:48 +0300
Subject: [PATCH 1/2] Execute JS in the very end of the message processing
 event loop

Fixes #4444
---
 .../vaadin/client/communication/MessageHandler.java    | 10 ++++++----
 1 file changed, 6 insertions(+), 4 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 1cc3d4e3bc0..aebe747b803 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
@@ -378,10 +378,12 @@ assert getServerId(valueMap) == -1
 
             if (json.hasKey(JsonConstants.UIDL_KEY_EXECUTE)) {
                 // Invoke JS only after all tree changes have been
-                // propagated
-                Reactive.addPostFlushListener(
-                        () -> registry.getExecuteJavaScriptProcessor().execute(
-                                json.getArray(JsonConstants.UIDL_KEY_EXECUTE)));
+                // propagated and in the very end of the browser event loop
+                Reactive.addPostFlushListener(() -> Scheduler.get()
+                        .scheduleFinally(() -> registry
+                                .getExecuteJavaScriptProcessor()
+                                .execute(json.getArray(
+                                        JsonConstants.UIDL_KEY_EXECUTE))));
             }
 
             Console.log("handleUIDLMessage: "

From 9941dddd0e82dd08a0aca6b6880467f99bb438c2 Mon Sep 17 00:00:00 2001
From: Denis Anisimov <denis@vaadin.com>
Date: Tue, 31 Jul 2018 16:08:57 +0300
Subject: [PATCH 2/2] Move JS execution via post flush listener after all
 listeners

---
 .../com/vaadin/client/communication/MessageHandler.java   | 8 +++++---
 1 file changed, 5 insertions(+), 3 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 aebe747b803..be0e0491102 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
@@ -378,9 +378,11 @@ assert getServerId(valueMap) == -1
 
             if (json.hasKey(JsonConstants.UIDL_KEY_EXECUTE)) {
                 // Invoke JS only after all tree changes have been
-                // propagated and in the very end of the browser event loop
-                Reactive.addPostFlushListener(() -> Scheduler.get()
-                        .scheduleFinally(() -> registry
+                // propagated and after post flush listeners added during
+                // message processing (so add one more post flush listener which
+                // is called after all added post listeners).
+                Reactive.addPostFlushListener(
+                        () -> Reactive.addPostFlushListener(() -> registry
                                 .getExecuteJavaScriptProcessor()
                                 .execute(json.getArray(
                                         JsonConstants.UIDL_KEY_EXECUTE))));