Skip to content

Commit

Permalink
next try on postponedActions
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Feb 25, 2025
1 parent 29d91b1 commit 34ef05a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/htmlunit/html/DomElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ public <P extends Page> P click(final boolean shiftKey, final boolean ctrlKey, f
AbstractJavaScriptEngine.PostponedActionsBlocker blocker = null;
final AbstractJavaScriptEngine<?> jsEngine = webClient.getJavaScriptEngine();
if (webClient.isJavaScriptEnabled()) {
blocker = jsEngine.blockPostponedActions();
blocker = jsEngine.blockPostponedActions(page);
}
try {
if (handleFocus) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ public interface AbstractJavaScriptEngine<SCRIPT> {
* <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
* Indicates that no postponed action should be executed.
*
* @param page only block actions for this page
* @return {@link PostponedActionsBlocker}
*/
PostponedActionsBlocker blockPostponedActions();
PostponedActionsBlocker blockPostponedActions(Page page);

/**
* Compiles the specified JavaScript code in the context of a given scope.
Expand Down Expand Up @@ -159,6 +160,7 @@ Object execute(HtmlPage page,
* Helper for blocking the execution of postponed actions for some time.
*/
interface PostponedActionsBlocker {
boolean blocks(PostponedAction postponedAction);
void release();
}
}
36 changes: 27 additions & 9 deletions src/main/java/org/htmlunit/javascript/JavaScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,13 @@ private void doProcessPostponedActions() {

// verify that the page that registered this PostponedAction is still alive
if (action.isStillAlive()) {
action.execute();
if (postponedActionsBlocker_ == null
|| !postponedActionsBlocker_.blocks(action)) {
action.execute();
}
else {
addPostponedAction(action);
}
}
}
}
Expand Down Expand Up @@ -1033,13 +1039,13 @@ protected void handleJavaScriptTimeoutError(final HtmlPage page, final TimeoutEr
* {@inheritDoc}
*/
@Override
public PostponedActionsBlocker blockPostponedActions() {
public PostponedActionsBlocker blockPostponedActions(final Page page) {
if (postponedActionsBlocker_ == null) {
postponedActionsBlocker_ = new RootPostponedActionsBlocker(this);
postponedActionsBlocker_ = new RootPostponedActionsBlocker(this, page);
return postponedActionsBlocker_;
}

return new ChildPostponedActionsBlocker();
return new ChildPostponedActionsBlocker(postponedActionsBlocker_);
}

/**
Expand All @@ -1048,9 +1054,7 @@ public PostponedActionsBlocker blockPostponedActions() {
*/
@Override
public void processPostponedActions() {
if (postponedActionsBlocker_ == null) {
doProcessPostponedActions();
}
doProcessPostponedActions();
}

/**
Expand Down Expand Up @@ -1396,9 +1400,16 @@ public static String evaluateProxyAutoConfig(final BrowserVersion browserVersion
*/
private final class RootPostponedActionsBlocker implements PostponedActionsBlocker {
private final JavaScriptEngine jsEngine_;
private final Page owningPage_;

private RootPostponedActionsBlocker(final JavaScriptEngine jsEngine) {
private RootPostponedActionsBlocker(final JavaScriptEngine jsEngine, final Page owningPage) {
jsEngine_ = jsEngine;
owningPage_ = owningPage;
}

@Override
public boolean blocks(final PostponedAction postponedAction) {
return owningPage_ == postponedAction.getOwningPage();
}

@Override
Expand All @@ -1412,9 +1423,16 @@ public void release() {
* {@link PostponedActionsBlocker} - noop blocker.
*/
private final class ChildPostponedActionsBlocker implements PostponedActionsBlocker {
private final RootPostponedActionsBlocker root_;

private ChildPostponedActionsBlocker() {
private ChildPostponedActionsBlocker(final RootPostponedActionsBlocker root) {
super();
root_ = root;
}

@Override
public boolean blocks(final PostponedAction postponedAction) {
return root_.blocks(postponedAction);
}

@Override
Expand Down

0 comments on commit 34ef05a

Please sign in to comment.