Skip to content

Commit

Permalink
fix: Add XAML source generator cancellation support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 2, 2021
1 parent bde528b commit dd4b19e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ public KeyValuePair<string, string>[] Generate()
var lastBinaryUpdateTime = _forceGeneration ? DateTime.MaxValue : GetLastBinaryUpdateTime();

var resourceKeys = GetResourceKeys();
var filesFull = new XamlFileParser(_excludeXamlNamespaces, _includeXamlNamespaces, _metadataHelper).ParseFiles(_xamlSourceFiles);
var filesFull = new XamlFileParser(_excludeXamlNamespaces, _includeXamlNamespaces, _metadataHelper)
.ParseFiles(_xamlSourceFiles, _generatorContext.CancellationToken);
var files = filesFull
.Trim()
.OrderBy(f => f.UniqueID)
Expand All @@ -277,6 +278,11 @@ public KeyValuePair<string, string>[] Generate()

var filesToProcess = filesQuery.AsParallel();

#if NETSTANDARD2_0
filesToProcess = filesToProcess
.WithCancellation(_generatorContext.CancellationToken);
#endif

if (Debugger.IsAttached)
{
filesToProcess = filesToProcess
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ public XamlFileParser(string[] excludeXamlNamespaces, string[] includeXamlNamesp
this._metadataHelper = roslynMetadataHelper;
}

public XamlFileDefinition[] ParseFiles(string[] xamlSourceFiles)
public XamlFileDefinition[] ParseFiles(string[] xamlSourceFiles, System.Threading.CancellationToken cancellationToken)
{
var files = new List<XamlFileDefinition>();

return xamlSourceFiles
.AsParallel()
.Select(ParseFile)
.WithCancellation(cancellationToken)
.Select(f => ParseFile(f, cancellationToken))
.Where(f => f != null)
.ToArray()!;
}

private XamlFileDefinition? ParseFile(string file)
private XamlFileDefinition? ParseFile(string file, System.Threading.CancellationToken cancellationToken)
{
try
{
Expand All @@ -65,6 +66,8 @@ public XamlFileDefinition[] ParseFiles(string[] xamlSourceFiles)
{
if (reader.Read())
{
cancellationToken.ThrowIfCancellationRequested();

return Visit(reader, file);
}
}
Expand Down

0 comments on commit dd4b19e

Please sign in to comment.