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.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<JsonElement> sendMessageAsync(String method, JsonObject params) {

View File

@ -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);

View File

@ -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();

View File

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