Skip to content

Commit b44dba0

Browse files
committed
Added explanation for wait until available #57
1 parent 22c44ed commit b44dba0

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

README.md

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ var config = Configuration.Default
3333

3434
in the previous example `MyConsoleLogger` refers to a class implementing the `IConsoleLogger` interface. Examples of classes implementing this interface are available in our [samples repository](https://github.com/AngleSharp/AngleSharp.Samples).
3535

36+
## Extension Methods
37+
38+
This plugin also delivers some extension methods to be used together with elements from AngleSharp, e.g., `IDocument` or `IElement`.
39+
40+
For instance, the following waits until a stable point has been reached (and the document is fully available):
41+
42+
```cs
43+
var context = BrowsingContext.New(config);
44+
var document = await context.OpenAsync(address)
45+
.WaitUntilAvailable();
46+
```
47+
48+
Scripts can also be run in the context of the document, where the result of the last expression is returned:
49+
50+
```cs
51+
var numEntries = document.ExecuteScript("document.querySelectorAll('div').length");
52+
```
53+
3654
## Vision and Status
3755

3856
The repository contains DOM bindings for the *Jint* JavaScript engine. *Jint* is fully ECMAScript 5 compatible and provides the basis for evaluating JavaScripts in the context of the AngleSharp DOM representation.

src/AngleSharp.Js/JsDocumentExtensions.cs

+35
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ namespace AngleSharp.Dom
33
using AngleSharp.Browser;
44
using AngleSharp.Js;
55
using System;
6+
using System.Threading;
67
using System.Threading.Tasks;
78

89
/// <summary>
@@ -92,5 +93,39 @@ public static Task<IDocument> WhenStable(this IDocument document) =>
9293
/// <returns>A task that is finished when the enqueued tasks completed.</returns>
9394
public static Task<IDocument> WhenStable(this Task<IDocument> documentTask) =>
9495
documentTask.Then(_ => { });
96+
97+
/// <summary>
98+
/// Waits until the document from the task is fully available.
99+
/// This includes that the document is completed and that a stable
100+
/// point has been reached.
101+
/// </summary>
102+
/// <param name="documentTask">The document task to await.</param>
103+
/// <param name="cancellation">The timeout cancellation, if any.</param>
104+
/// <returns>A task that is finished when the document is available.</returns>
105+
public static async Task WaitUntilAvailable(this Task<IDocument> documentTask, CancellationToken cancellation = default)
106+
{
107+
var document = await documentTask.ConfigureAwait(false);
108+
await document.WaitUntilAvailable(cancellation).ConfigureAwait(false);
109+
}
110+
111+
/// <summary>
112+
/// Waits until the document is fully available. This includes
113+
/// that the document is completed and that a stable point has
114+
/// been reached.
115+
/// </summary>
116+
/// <param name="document">The document to await.</param>
117+
/// <param name="cancellation">The timeout cancellation, if any.</param>
118+
/// <returns>A task that is finished when the document is available.</returns>
119+
public static async Task WaitUntilAvailable(this IDocument document, CancellationToken cancellation = default)
120+
{
121+
if (document.ReadyState != DocumentReadyState.Complete)
122+
{
123+
var abort = new TaskCompletionSource<object>();
124+
cancellation.Register(() => abort.TrySetCanceled());
125+
await Task.WhenAny(document.AwaitEventAsync("load"), abort.Task).ConfigureAwait(false);
126+
}
127+
128+
await document.WhenStable().ConfigureAwait(false);
129+
}
95130
}
96131
}

0 commit comments

Comments
 (0)