Skip to content

Commit 6496675

Browse files
committed
refactor: Use static empty NavigationRenderResult object
1 parent 3b4fe98 commit 6496675

File tree

6 files changed

+22
-43
lines changed

6 files changed

+22
-43
lines changed

src/Elastic.ApiExplorer/OpenApiGenerator.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,16 +288,11 @@ private async Task<IFileInfo> Render<T>(INavigationItem current, T page, ApiRend
288288
if (!outputFile.Directory!.Exists)
289289
outputFile.Directory.Create();
290290

291-
var navigationHtml = await navigationRenderer.RenderNavigation(current.NavigationRoot, new Uri("http://ignored.example"), INavigationHtmlWriter.AllLevels, ctx);
291+
var navigationRenderResult = await navigationRenderer.RenderNavigation(current.NavigationRoot, new Uri("http://ignored.example"), INavigationHtmlWriter.AllLevels, ctx);
292292
renderContext = renderContext with
293293
{
294294
CurrentNavigation = current,
295-
NavigationHtml = navigationHtml switch
296-
{
297-
EmptyNavigationRenderResult => string.Empty,
298-
OkNavigationRenderResult result => result.Html,
299-
_ => throw new Exception("Unexpected navigation render result")
300-
}
295+
NavigationHtml = navigationRenderResult.Html
301296
};
302297
await using var stream = _writeFileSystem.FileStream.New(outputFile.FullName, FileMode.OpenOrCreate);
303298
await page.RenderAsync(stream, renderContext, ctx);

src/Elastic.Documentation.Site/Navigation/INavigationHtmlWriter.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface INavigationHtmlWriter
1010
{
1111
const int AllLevels = -1;
1212

13-
Task<INavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, Uri navigationSource,
13+
Task<NavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation, Uri navigationSource,
1414
int maxLevel, Cancel ctx = default);
1515

1616
async Task<string> Render(NavigationViewModel model, Cancel ctx)
@@ -19,21 +19,14 @@ async Task<string> Render(NavigationViewModel model, Cancel ctx)
1919
return await slice.RenderAsync(cancellationToken: ctx);
2020
}
2121
}
22-
23-
public interface INavigationRenderResult
22+
public record NavigationRenderResult
2423
{
25-
string Html { get; init; }
26-
string Id { get; init; }
27-
}
24+
public static NavigationRenderResult Empty => new()
25+
{
26+
Html = string.Empty,
27+
Id = "empty-navigation" // random id
28+
};
2829

29-
public record OkNavigationRenderResult : INavigationRenderResult
30-
{
3130
public required string Html { get; init; }
3231
public required string Id { get; init; }
3332
}
34-
35-
public record EmptyNavigationRenderResult : INavigationRenderResult
36-
{
37-
public string Html { get; init; } = string.Empty;
38-
public string Id { get; init; } = string.Empty;
39-
}

src/Elastic.Documentation.Site/Navigation/IsolatedBuildNavigationHtmlWriter.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,25 @@ public class IsolatedBuildNavigationHtmlWriter(BuildContext context, IRootNaviga
1313
{
1414
private readonly ConcurrentDictionary<(string, int), string> _renderedNavigationCache = [];
1515

16-
public async Task<INavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation,
16+
public async Task<NavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation,
1717
Uri navigationSource, int maxLevel, Cancel ctx = default)
1818
{
1919
var navigation = context.Configuration.Features.PrimaryNavEnabled || currentRootNavigation.IsUsingNavigationDropdown
2020
? currentRootNavigation
2121
: siteRoot;
22-
2322
var id = ShortId.Create($"{(navigation.Id, maxLevel).GetHashCode()}");
24-
2523
if (_renderedNavigationCache.TryGetValue((navigation.Id, maxLevel), out var value))
26-
return new OkNavigationRenderResult
24+
{
25+
return new NavigationRenderResult
2726
{
2827
Html = value,
2928
Id = id
3029
};
31-
30+
}
3231
var model = CreateNavigationModel(navigation, maxLevel);
3332
value = await ((INavigationHtmlWriter)this).Render(model, ctx);
3433
_renderedNavigationCache[(navigation.Id, maxLevel)] = value;
35-
return new OkNavigationRenderResult
34+
return new NavigationRenderResult
3635
{
3736
Html = value,
3837
Id = id

src/Elastic.Markdown/HtmlWriter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ private async Task<RenderResult> RenderLayout(MarkdownFile markdown, MarkdownDoc
5757
await DocumentationSet.Tree.Resolve(ctx);
5858

5959
var fullNavigationRenderResult = await NavigationHtmlWriter.RenderNavigation(markdown.NavigationRoot, markdown.NavigationSource, INavigationHtmlWriter.AllLevels, ctx);
60-
var miniNavigationRenderResult = DocumentationSet.Context.Configuration.Features.LazyLoadNavigation
61-
? await NavigationHtmlWriter.RenderNavigation(markdown.NavigationRoot, markdown.NavigationSource, 1, ctx)
62-
: new EmptyNavigationRenderResult();
60+
var miniNavigationRenderResult = await NavigationHtmlWriter.RenderNavigation(markdown.NavigationRoot, markdown.NavigationSource, 1, ctx);
6361

6462
var navigationHtmlRenderResult = DocumentationSet.Context.Configuration.Features.LazyLoadNavigation
6563
? miniNavigationRenderResult

src/Elastic.Markdown/IO/DocumentationSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public class DocumentationSet : INavigationLookups, IPositionalNavigation
130130

131131
public IReadOnlyCollection<IDocsBuilderExtension> EnabledExtensions { get; }
132132

133-
public ConcurrentDictionary<string, INavigationRenderResult> NavigationRenderResults { get; } = [];
133+
public ConcurrentDictionary<string, NavigationRenderResult> NavigationRenderResults { get; } = [];
134134

135135
public DocumentationSet(
136136
BuildContext context,

src/tooling/docs-assembler/Navigation/GlobalNavigationHtmlWriter.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,38 +45,32 @@ private bool TryGetNavigationRoot(
4545
return true;
4646
}
4747

48-
public async Task<INavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation,
48+
public async Task<NavigationRenderResult> RenderNavigation(IRootNavigationItem<INavigationModel, INavigationItem> currentRootNavigation,
4949
Uri navigationSource, int maxLevel, Cancel ctx = default)
5050
{
51-
5251
if (Phantoms.Contains(navigationSource)
5352
|| !TryGetNavigationRoot(navigationSource, out var navigationRoot, out var navigationRootSource)
5453
|| Phantoms.Contains(navigationRootSource)
55-
)
56-
return new EmptyNavigationRenderResult();
54+
)
55+
return NavigationRenderResult.Empty;
5756

5857
var navigationId = ShortId.Create($"{(navigationRootSource, maxLevel).GetHashCode()}");
5958

6059
if (_renderedNavigationCache.TryGetValue((navigationRootSource, maxLevel), out var value))
61-
return new OkNavigationRenderResult
62-
{
63-
Html = value,
64-
Id = navigationId
65-
};
60+
return NavigationRenderResult.Empty;
6661

6762
if (navigationRootSource == new Uri("docs-content:///"))
6863
{
6964
_renderedNavigationCache[(navigationRootSource, maxLevel)] = string.Empty;
70-
return new EmptyNavigationRenderResult();
65+
return NavigationRenderResult.Empty;
7166
}
7267

7368
Console.WriteLine($"Rendering navigation for {navigationRootSource}");
7469

7570
var model = CreateNavigationModel(navigationRoot, maxLevel);
7671
value = await ((INavigationHtmlWriter)this).Render(model, ctx);
7772
_renderedNavigationCache[(navigationRootSource, maxLevel)] = value;
78-
79-
return new OkNavigationRenderResult
73+
return new NavigationRenderResult
8074
{
8175
Html = value,
8276
Id = navigationId

0 commit comments

Comments
 (0)