fix: properly dispose scope objects (#60)

This commit is contained in:
Yury Semikhatsky 2020-10-30 11:38:26 -07:00 committed by GitHub
parent 92ae6cae49
commit 686bd28670
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 8 deletions

View File

@ -20,6 +20,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.microsoft.playwright.Deferred; import com.microsoft.playwright.Deferred;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -49,11 +50,22 @@ class ChannelOwner {
this.initializer = initializer; this.initializer = initializer;
connection.registerObject(guid, this); connection.registerObject(guid, this);
if (parent != null) if (parent != null) {
parent.objects.put(guid, this); 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<JsonElement> sendMessageAsync(String method, JsonObject params) { WaitableResult<JsonElement> sendMessageAsync(String method, JsonObject params) {

View File

@ -93,7 +93,6 @@ public class Connection {
return result; return result;
} }
public ChannelOwner waitForObjectWithKnownName(String guid) { public ChannelOwner waitForObjectWithKnownName(String guid) {
while (!objects.containsKey(guid)) { while (!objects.containsKey(guid)) {
processOneMessage(); processOneMessage();
@ -112,7 +111,7 @@ public class Connection {
objects.put(guid, object); objects.put(guid, object);
} }
void unregisterObject(String guid, ChannelOwner object) { void unregisterObject(String guid) {
objects.remove(guid); objects.remove(guid);
} }
@ -156,7 +155,7 @@ public class Connection {
if (object == null) { if (object == null) {
throw new PlaywrightException("Cannot find object to dispose: " + message.guid); throw new PlaywrightException("Cannot find object to dispose: " + message.guid);
} }
object.dispose(); object.disconnect();
return; return;
} }
ChannelOwner object = objects.get(message.guid); ChannelOwner object = objects.get(message.guid);

View File

@ -38,6 +38,11 @@ public class JSHandleImpl extends ChannelOwner implements JSHandle {
return null; return null;
} }
@Override
public void dispose() {
sendMessage("dispose");
}
@Override @Override
public Object evaluate(String pageFunction, Object arg) { public Object evaluate(String pageFunction, Object arg) {
JsonObject params = new JsonObject(); JsonObject params = new JsonObject();

View File

@ -90,15 +90,15 @@ class Serialization {
result.s = (String) value; result.s = (String) value;
} else if (value instanceof List) { } else if (value instanceof List) {
List<SerializedValue> list = new ArrayList<>(); List<SerializedValue> list = new ArrayList<>();
for (Object o : (List<Object>) value) { for (Object o : (List<?>) value) {
list.add(serializeValue(o, handles, depth + 1)); list.add(serializeValue(o, handles, depth + 1));
} }
result.a = list.toArray(new SerializedValue[0]); result.a = list.toArray(new SerializedValue[0]);
} else if (value instanceof Map) { } else if (value instanceof Map) {
List<SerializedValue.O> list = new ArrayList<>(); List<SerializedValue.O> list = new ArrayList<>();
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) value; Map<String, ?> map = (Map<String, ?>) value;
for (Map.Entry<String, Object> e : map.entrySet()) { for (Map.Entry<String, ?> e : map.entrySet()) {
SerializedValue.O o = new SerializedValue.O(); SerializedValue.O o = new SerializedValue.O();
o.k = e.getKey(); o.k = e.getKey();
o.v = serializeValue(e.getValue(), handles, depth + 1); o.v = serializeValue(e.getValue(), handles, depth + 1);