Skip to content

Commit

Permalink
AwaitableResult 的结果可见性修改
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Oct 21, 2024
1 parent d62df03 commit eae4382
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 165 deletions.
16 changes: 16 additions & 0 deletions example/src/main/java/com/ruiyun/example/A_LaunchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,22 @@ public void test0() throws IOException {
}
}

@Test
public void test17() throws IOException {
//指定chrome浏览器的缓存目录
launchOptions.setCacheDir("C:\\Users\\fanyong\\Desktop\\.local-browser");
Browser browser = getBrowser();
//打开一个页面
Page page = browser.newPage();
Target target1 = page.target();
System.out.println("one type=" + target1.type() + ", url=" + target1.url() + ",id=" + target1.getTargetId());
List<Target> targets = browser.targets();
//看看targets里面都有什么,包含browser,page,等类型,其中还包含了上面newPage得到page
for (Target target : targets) {
System.out.println("two type=" + target.type() + ", url=" + target.url() + ",id=" + target.getTargetId());
}
}

public Browser getBrowser() throws IOException {
return Puppeteer.launch(launchOptions);
}
Expand Down
111 changes: 0 additions & 111 deletions example/src/main/java/com/ruiyun/example/Z_KillExample.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public class AwaitableResult<T> {

private final CountDownLatch latch = new CountDownLatch(1);
private T response;
private volatile T response;

public static <T> AwaitableResult<T> create() {
return new AwaitableResult<>();
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/com/ruiyun/jvppeteer/core/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -50,7 +49,6 @@
import java.util.function.Supplier;

import static com.ruiyun.jvppeteer.common.Constant.CDP_BINDING_PREFIX;
import static com.ruiyun.jvppeteer.common.Constant.DEFAULT_BATCH_SIZE;
import static com.ruiyun.jvppeteer.common.Constant.MAIN_WORLD;
import static com.ruiyun.jvppeteer.common.Constant.PUPPETEER_WORLD;
import static com.ruiyun.jvppeteer.util.Helper.withSourcePuppeteerURLIfNone;
Expand Down Expand Up @@ -440,57 +438,6 @@ public ElementHandle frameElement() throws JsonProcessingException, EvaluateExce
return parentFrame.mainRealm().adoptBackendNode(response.get("backendNodeId").asInt()).asElement();
}

private List<JSHandle> transposeIterableHandle(JSHandle list) throws JsonProcessingException {
JSHandle generatorHandle = null;
try {
generatorHandle = list.evaluateHandle("iterable => {\n" +
" return (async function* () {\n" +
" yield* iterable;\n" +
" })();\n" +
" }");
return transposeIteratorHandle(generatorHandle);
} finally {
Optional.ofNullable(generatorHandle).ifPresent(JSHandle::dispose);
}
}

private List<JSHandle> transposeIteratorHandle(JSHandle iterator) throws JsonProcessingException, EvaluateException {
int size = DEFAULT_BATCH_SIZE;
List<JSHandle> results = new ArrayList<>();
List<JSHandle> result;
while ((result = fastTransposeIteratorHandle(iterator, size)) != null) {
results.addAll(result);
size <<= 1;
}
return results;
}

private List<JSHandle> fastTransposeIteratorHandle(JSHandle iterator, int size) throws JsonProcessingException, EvaluateException {
JSHandle array = null;
Collection<JSHandle> handles;
try {
array = iterator.evaluateHandle("async (iterator, size) => {\n" +
" const results = [];\n" +
" while (results.length < size) {\n" +
" const result = await iterator.next();\n" +
" if (result.done) {\n" +
" break;\n" +
" }\n" +
" results.push(result.value);\n" +
" }\n" +
" return results;\n" +
" }", Collections.singletonList(size));
Map<String, JSHandle> properties = array.getProperties();
handles = properties.values();
if (properties.isEmpty()) {
return null;
}
return new ArrayList<>(handles);
} finally {
Optional.ofNullable(array).ifPresent(JSHandle::dispose);
}
}

public ElementHandle $(String selector) throws JsonProcessingException, EvaluateException {
ElementHandle document = this.document();
return document.$(selector);
Expand Down

0 comments on commit eae4382

Please sign in to comment.