use of com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJobManager in project htmlunit by HtmlUnit.
the class WebClient method waitForBackgroundJavaScript.
/**
* <p><span style="color:red">Experimental API: May be changed in next release
* and may not yet work perfectly!</span></p>
*
* <p>This method blocks until all background JavaScript tasks have finished executing. Background
* JavaScript tasks are JavaScript tasks scheduled for execution via <tt>window.setTimeout</tt>,
* <tt>window.setInterval</tt> or asynchronous <tt>XMLHttpRequest</tt>.</p>
*
* <p>If a job is scheduled to begin executing after <tt>(now + timeoutMillis)</tt>, this method will
* wait for <tt>timeoutMillis</tt> milliseconds and then return a value greater than <tt>0</tt>. This
* method will never block longer than <tt>timeoutMillis</tt> milliseconds.</p>
*
* <p>Use this method instead of {@link #waitForBackgroundJavaScriptStartingBefore(long)} if you
* don't know when your background JavaScript is supposed to start executing, but you're fairly sure
* that you know how long it should take to finish executing.</p>
*
* @param timeoutMillis the maximum amount of time to wait (in milliseconds)
* @return the number of background JavaScript jobs still executing or waiting to be executed when this
* method returns; will be <tt>0</tt> if there are no jobs left to execute
*/
public int waitForBackgroundJavaScript(final long timeoutMillis) {
int count = 0;
final long endTime = System.currentTimeMillis() + timeoutMillis;
for (Iterator<WeakReference<JavaScriptJobManager>> i = jobManagers_.iterator(); i.hasNext(); ) {
final JavaScriptJobManager jobManager;
final WeakReference<JavaScriptJobManager> reference;
try {
reference = i.next();
jobManager = reference.get();
if (jobManager == null) {
i.remove();
continue;
}
} catch (final ConcurrentModificationException e) {
i = jobManagers_.iterator();
count = 0;
continue;
}
final long newTimeout = endTime - System.currentTimeMillis();
count += jobManager.waitForJobs(newTimeout);
}
if (count != getAggregateJobCount()) {
final long newTimeout = endTime - System.currentTimeMillis();
return waitForBackgroundJavaScript(newTimeout);
}
return count;
}
Aggregations