@@ -16,13 +16,7 @@ public sealed class DocumentBuildContext : IDocumentBuildContext
1616 private readonly ConcurrentDictionary < string , TocInfo > _tableOfContents = new ( FilePathComparer . OSPlatformSensitiveStringComparer ) ;
1717 private readonly Task < IXRefContainerReader > _reader ;
1818
19- public DocumentBuildContext ( string buildOutputFolder )
20- : this ( buildOutputFolder , Enumerable . Empty < FileAndType > ( ) , ImmutableArray < string > . Empty , ImmutableArray < string > . Empty , 1 , Directory . GetCurrentDirectory ( ) , string . Empty , null , null ) { }
21-
22- public DocumentBuildContext ( string buildOutputFolder , IEnumerable < FileAndType > allSourceFiles , ImmutableArray < string > externalReferencePackages , ImmutableArray < string > xrefMaps , int maxParallelism , string baseFolder , string versionName , ApplyTemplateSettings applyTemplateSetting , string rootTocPath )
23- : this ( buildOutputFolder , allSourceFiles , externalReferencePackages , xrefMaps , maxParallelism , baseFolder , versionName , applyTemplateSetting , rootTocPath , null , null ) { }
24-
25- public DocumentBuildContext ( DocumentBuildParameters parameters )
19+ public DocumentBuildContext ( DocumentBuildParameters parameters , CancellationToken cancellationToken )
2620 {
2721 BuildOutputFolder = Path . Combine ( Path . GetFullPath ( EnvironmentContext . BaseDirectory ) , parameters . OutputBaseDir ) ;
2822 VersionName = parameters . VersionName ;
@@ -34,9 +28,10 @@ public DocumentBuildContext(DocumentBuildParameters parameters)
3428
3529 if ( parameters . XRefMaps . Length > 0 )
3630 {
31+ // Note: `_reader` task is processed asyncronously and await is called later. So OperationCancellationException is not thrown by this lines.
3732 _reader = new XRefCollection (
3833 from u in parameters . XRefMaps
39- select new Uri ( u , UriKind . RelativeOrAbsolute ) ) . GetReaderAsync ( parameters . Files . DefaultBaseDir , parameters . MarkdownEngineParameters ? . FallbackFolders ) ;
34+ select new Uri ( u , UriKind . RelativeOrAbsolute ) ) . GetReaderAsync ( parameters . Files . DefaultBaseDir , parameters . MarkdownEngineParameters ? . FallbackFolders , cancellationToken ) ;
4035 }
4136 RootTocPath = parameters . RootTocPath ;
4237
@@ -54,9 +49,18 @@ from u in parameters.XRefMaps
5449 }
5550 }
5651 VersionFolder = versionDir ;
52+ CancellationToken = cancellationToken ;
5753 }
5854
59- public DocumentBuildContext (
55+ #region Constructors that used by test code.
56+
57+ internal DocumentBuildContext ( string buildOutputFolder )
58+ : this ( buildOutputFolder , Enumerable . Empty < FileAndType > ( ) , ImmutableArray < string > . Empty , ImmutableArray < string > . Empty , 1 , Directory . GetCurrentDirectory ( ) , string . Empty , null , null ) { }
59+
60+ private DocumentBuildContext ( string buildOutputFolder , IEnumerable < FileAndType > allSourceFiles , ImmutableArray < string > externalReferencePackages , ImmutableArray < string > xrefMaps , int maxParallelism , string baseFolder , string versionName , ApplyTemplateSettings applyTemplateSetting , string rootTocPath )
61+ : this ( buildOutputFolder , allSourceFiles , externalReferencePackages , xrefMaps , maxParallelism , baseFolder , versionName , applyTemplateSetting , rootTocPath , null , null ) { }
62+
63+ private DocumentBuildContext (
6064 string buildOutputFolder ,
6165 IEnumerable < FileAndType > allSourceFiles ,
6266 ImmutableArray < string > externalReferencePackages ,
@@ -98,6 +102,7 @@ from u in xrefMaps
98102 }
99103 VersionFolder = versionFolder ;
100104 }
105+ #endregion
101106
102107 public string BuildOutputFolder { get ; }
103108
@@ -129,6 +134,8 @@ from u in xrefMaps
129134
130135 public ICustomHrefGenerator HrefGenerator { get ; }
131136
137+ public CancellationToken CancellationToken { get ; } = CancellationToken . None ;
138+
132139 internal ConcurrentBag < ManifestItem > ManifestItems { get ; } = new ( ) ;
133140
134141 private ConcurrentDictionary < string , XRefSpec > ExternalXRefSpec { get ; } = new ( ) ;
@@ -150,9 +157,10 @@ public void ReportExternalXRefSpec(XRefSpec spec)
150157
151158 public void ResolveExternalXRefSpec ( )
152159 {
153- Task . WaitAll (
160+ Task . WaitAll ( [
154161 Task . Run ( ResolveExternalXRefSpecForSpecs ) ,
155- Task . Run ( ResolveExternalXRefSpecForNoneSpecsAsync ) ) ;
162+ Task . Run ( ResolveExternalXRefSpecForNoneSpecsAsync )
163+ ] , CancellationToken ) ;
156164 }
157165
158166 private void ResolveExternalXRefSpecForSpecs ( )
@@ -205,7 +213,7 @@ private List<string> ResolveByExternalReferencePackages(List<string> uidList, Co
205213
206214 var oldSpecCount = externalXRefSpec . Count ;
207215 var list = new List < string > ( ) ;
208- using ( var externalReferences = new ExternalReferencePackageCollection ( ExternalReferencePackages , MaxParallelism ) )
216+ using ( var externalReferences = new ExternalReferencePackageCollection ( ExternalReferencePackages , MaxParallelism , CancellationToken ) )
209217 {
210218 foreach ( var uid in uidList )
211219 {
@@ -401,7 +409,7 @@ public XRefSpec GetXrefSpec(string uid)
401409
402410 if ( ExternalReferencePackages . Length > 0 )
403411 {
404- using ( var externalReferences = new ExternalReferencePackageCollection ( ExternalReferencePackages , MaxParallelism ) )
412+ using ( var externalReferences = new ExternalReferencePackageCollection ( ExternalReferencePackages , MaxParallelism , CancellationToken ) )
405413 {
406414 xref = GetExternalReference ( externalReferences , uid ) ;
407415 }
0 commit comments