diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java b/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java index dd40f1e6..9b821736 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/ChannelOwner.java @@ -20,6 +20,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.microsoft.playwright.Deferred; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -49,11 +50,22 @@ class ChannelOwner { this.initializer = initializer; connection.registerObject(guid, this); - if (parent != null) + if (parent != null) { parent.objects.put(guid, this); + } } - public void dispose() { + void disconnect() { + // Clean up from parent and connection. + if (parent != null) { + parent.objects.remove(guid); + } + connection.unregisterObject(guid); + // Dispose all children. + for (ChannelOwner child : new ArrayList<>(objects.values())) { + child.disconnect(); + } + objects.clear(); } WaitableResult sendMessageAsync(String method, JsonObject params) { diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java b/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java index 2b7bc13e..e441338e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Connection.java @@ -93,7 +93,6 @@ public class Connection { return result; } - public ChannelOwner waitForObjectWithKnownName(String guid) { while (!objects.containsKey(guid)) { processOneMessage(); @@ -112,7 +111,7 @@ public class Connection { objects.put(guid, object); } - void unregisterObject(String guid, ChannelOwner object) { + void unregisterObject(String guid) { objects.remove(guid); } @@ -156,7 +155,7 @@ public class Connection { if (object == null) { throw new PlaywrightException("Cannot find object to dispose: " + message.guid); } - object.dispose(); + object.disconnect(); return; } ChannelOwner object = objects.get(message.guid); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java b/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java index b13a60a0..5ab39f9e 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/JSHandleImpl.java @@ -38,6 +38,11 @@ public class JSHandleImpl extends ChannelOwner implements JSHandle { return null; } + @Override + public void dispose() { + sendMessage("dispose"); + } + @Override public Object evaluate(String pageFunction, Object arg) { JsonObject params = new JsonObject(); diff --git a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java index 29bc3ca2..ce6de2bd 100644 --- a/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java +++ b/playwright/src/main/java/com/microsoft/playwright/impl/Serialization.java @@ -90,15 +90,15 @@ class Serialization { result.s = (String) value; } else if (value instanceof List) { List list = new ArrayList<>(); - for (Object o : (List) value) { + for (Object o : (List) value) { list.add(serializeValue(o, handles, depth + 1)); } result.a = list.toArray(new SerializedValue[0]); } else if (value instanceof Map) { List list = new ArrayList<>(); @SuppressWarnings("unchecked") - Map map = (Map) value; - for (Map.Entry e : map.entrySet()) { + Map map = (Map) value; + for (Map.Entry e : map.entrySet()) { SerializedValue.O o = new SerializedValue.O(); o.k = e.getKey(); o.v = serializeValue(e.getValue(), handles, depth + 1);