Skip to content

Commit

Permalink
Add cancellation support to async methods
Browse files Browse the repository at this point in the history
Introduce calls to `cancellationToken.ThrowIfCancellationRequested()` in various methods across the `XperienceCommunity.DataContext` namespace to ensure operations respect cancellation requests. Updated methods include `FirstOrDefaultAsync` and `ToListAsync` in `ContentItemContext.cs`, `PageContentContext.cs`, and `ReusableSchemaContext.cs`, as well as `ExecuteQueryAsync` in `ContentQueryExecutor.cs`, `PageContentQueryExecutor.cs`, and `ReusableSchemaExecutor.cs`. These changes improve the responsiveness and resource management of the application.
  • Loading branch information
bluemodus-brandon committed Sep 22, 2024
1 parent 04b9d7d commit bb0b8ae
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/XperienceCommunity.DataContext/ContentItemContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public ContentItemContext(IWebsiteChannelContext websiteChannelContext,
public async Task<T?> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var queryBuilder = BuildQuery(predicate, topN: 1);

var queryOptions = CreateQueryOptions();
Expand Down Expand Up @@ -134,6 +136,8 @@ public IDataContext<T> Take(int count)
/// <returns>A task that represents the asynchronous operation. The task result contains the content items.</returns>
public async Task<IEnumerable<T>> ToListAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

ValidateQuery();

var queryBuilder = BuildQuery(_query?.Expression!);
Expand Down
4 changes: 4 additions & 0 deletions src/XperienceCommunity.DataContext/ContentQueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public ContentQueryExecutor(ILogger<ContentQueryExecutor<T>> logger, IContentQue
public async Task<IEnumerable<T>> ExecuteQueryAsync(ContentItemQueryBuilder queryBuilder,
ContentQueryExecutionOptions queryOptions, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

try
{
var results = await _queryExecutor.GetMappedResult<T>(queryBuilder, queryOptions,
Expand All @@ -40,6 +42,8 @@ public async Task<IEnumerable<T>> ExecuteQueryAsync(ContentItemQueryBuilder quer
{
foreach (var processor in _processors.OrderBy(x=> x.Order))
{
cancellationToken.ThrowIfCancellationRequested();

await processor.ProcessAsync(result, cancellationToken);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/XperienceCommunity.DataContext/PageContentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public PageContentContext(IProgressiveCache cache, PageContentQueryExecutor<T> p
public async Task<T?> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var queryBuilder = BuildQuery(predicate, topN: 1);

var queryOptions = CreateQueryOptions();
Expand Down Expand Up @@ -121,6 +123,8 @@ public IDataContext<T> Take(int count)
[return: NotNull]
public async Task<IEnumerable<T>> ToListAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

ValidateQuery();

var queryBuilder = BuildQuery(_query?.Expression!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public async Task<IEnumerable<T>> ExecuteQueryAsync(ContentItemQueryBuilder quer
{
try
{
cancellationToken.ThrowIfCancellationRequested();

var results = await _queryExecutor.GetMappedWebPageResult<T>(queryBuilder, queryOptions,
cancellationToken: cancellationToken);

Expand All @@ -41,6 +43,8 @@ public async Task<IEnumerable<T>> ExecuteQueryAsync(ContentItemQueryBuilder quer
{
foreach (var processor in _processors.OrderBy(x => x.Order))
{
cancellationToken.ThrowIfCancellationRequested();

await processor.ProcessAsync(result, cancellationToken);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/XperienceCommunity.DataContext/ReusableSchemaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public ReusableSchemaContext(IWebsiteChannelContext websiteChannelContext,
public async Task<T?> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate,
CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

var queryBuilder = BuildQuery(predicate, 1);

var queryOptions = CreateQueryOptions();
Expand Down Expand Up @@ -126,7 +128,10 @@ public IDataContext<T> Take(int count)
/// <returns>A task that represents the asynchronous operation. The task result contains the content items.</returns>
public async Task<IEnumerable<T>> ToListAsync(CancellationToken cancellationToken = default)
{
cancellationToken.ThrowIfCancellationRequested();

ValidateQuery();

var queryBuilder = BuildQuery(_query?.Expression!);

var queryOptions = CreateQueryOptions();
Expand Down
2 changes: 2 additions & 0 deletions src/XperienceCommunity.DataContext/ReusableSchemaExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public async Task<IEnumerable<T>> ExecuteQueryAsync(ContentItemQueryBuilder quer
{
try
{
cancellationToken.ThrowIfCancellationRequested();

var results = await _queryExecutor.GetMappedResult<T>(queryBuilder, queryOptions,
cancellationToken: cancellationToken);

Expand Down

0 comments on commit bb0b8ae

Please sign in to comment.