Skip to content

Commit 276887b

Browse files
authored
fix: optimize Reactive.flush() for large listener queues (#23080)
Fixes #23075
1 parent 1bd8623 commit 276887b

1 file changed

Lines changed: 24 additions & 16 deletions

File tree

  • flow-client/src/main/java/com/vaadin/client/flow/reactive

‎flow-client/src/main/java/com/vaadin/client/flow/reactive/Reactive.java‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,42 @@ public static void flush() {
100100
if (flushing) {
101101
return;
102102
}
103+
104+
if (flushListeners == null) {
105+
flushListeners = JsCollections.array();
106+
}
107+
108+
if (postFlushListeners == null) {
109+
postFlushListeners = JsCollections.array();
110+
}
111+
112+
int flushListenerIndex = 0;
113+
int postFlushListenerIndex = 0;
114+
103115
try {
104116
flushing = true;
105-
while (hasFlushListeners() || hasPostFlushListeners()) {
117+
118+
while (flushListenerIndex < flushListeners.length()
119+
|| postFlushListenerIndex < postFlushListeners.length()) {
106120
// Purge all flush listeners
107-
while (hasFlushListeners()) {
108-
FlushListener oldestListener = flushListeners.remove(0);
109-
oldestListener.flush();
121+
while (flushListenerIndex < flushListeners.length()) {
122+
flushListeners.get(flushListenerIndex).flush();
123+
flushListenerIndex++;
110124
}
111125

112126
// Purge one post flush listener, then look if there are new
113-
// flush
114127
// listeners to purge
115-
if (hasPostFlushListeners()) {
116-
FlushListener oldestListener = postFlushListeners.remove(0);
117-
oldestListener.flush();
128+
if (postFlushListenerIndex < postFlushListeners.length()) {
129+
postFlushListeners.get(postFlushListenerIndex).flush();
130+
postFlushListenerIndex++;
118131
}
119132
}
120133
} finally {
121134
flushing = false;
122-
}
123-
}
124-
125-
private static boolean hasPostFlushListeners() {
126-
return postFlushListeners != null && !postFlushListeners.isEmpty();
127-
}
128135

129-
private static boolean hasFlushListeners() {
130-
return flushListeners != null && !flushListeners.isEmpty();
136+
flushListeners.splice(0, flushListenerIndex);
137+
postFlushListeners.splice(0, postFlushListenerIndex);
138+
}
131139
}
132140

133141
/**

0 commit comments

Comments
 (0)