From 06e7b7a4d7a2b28ffb67f1a3dcfa1f45274627de Mon Sep 17 00:00:00 2001 From: Puja Jagani Date: Wed, 3 Jul 2024 16:29:53 +0530 Subject: [PATCH] [bidi][java] Ensure empty values are removed from the call back map --- java/src/org/openqa/selenium/bidi/Connection.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/java/src/org/openqa/selenium/bidi/Connection.java b/java/src/org/openqa/selenium/bidi/Connection.java index 643872ed1f0e3..9b667eee627dc 100644 --- a/java/src/org/openqa/selenium/bidi/Connection.java +++ b/java/src/org/openqa/selenium/bidi/Connection.java @@ -25,6 +25,7 @@ import java.io.Closeable; import java.io.StringReader; import java.time.Duration; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -210,7 +211,16 @@ public void removeListener(long id) { Lock lock = callbacksLock.writeLock(); lock.lock(); try { - eventCallbacks.forEach((k, v) -> v.remove(id)); + List> list = new ArrayList<>(); + eventCallbacks.forEach( + (k, v) -> { + v.remove(id); + if (v.isEmpty()) { + list.add(k); + } + }); + + list.forEach(eventCallbacks::remove); } finally { lock.unlock(); }