-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: IConditionalOutputComponent used to determine if a component sh…
…ould produce markup
- Loading branch information
Showing
11 changed files
with
165 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
|
||
namespace Htmxor.Components; | ||
|
||
public class HtmxorLayoutComponentBase : LayoutComponentBase, IConditionalOutputComponent | ||
{ | ||
public bool ShouldOutput(int conditionalChildren) | ||
=> conditionalChildren == 0; | ||
|
||
protected override void BuildRenderTree([NotNull] RenderTreeBuilder builder) | ||
=> builder.AddContent(0, Body); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Htmxor.Components; | ||
|
||
/// <summary> | ||
/// Represents a component that can conditionally produce markup. | ||
/// </summary> | ||
public interface IConditionalOutputComponent | ||
{ | ||
/// <summary> | ||
/// Determine whether this component should produce any markup during a request. | ||
/// </summary> | ||
/// <param name="conditionalChildren">The number of children that implements <see cref="IConditionalOutputComponent"/>.</param> | ||
/// <returns><see langword="true"/> if the component should produce markup, <see langword="false"/> otherwise.</returns> | ||
bool ShouldOutput(int conditionalChildren); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Htmxor/Rendering/Buffering/ConditionalBufferedTextWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace Htmxor.Rendering.Buffering; | ||
|
||
/// <summary> | ||
/// A text writer that will only output when its <see cref="ShouldWrite"/> returns true. | ||
/// </summary> | ||
internal sealed class ConditionalBufferedTextWriter : BufferedTextWriter | ||
{ | ||
internal bool ShouldWrite { get; set; } = true; | ||
|
||
public ConditionalBufferedTextWriter(TextWriter underlying) : base(underlying) | ||
{ | ||
} | ||
|
||
public override void Write(char value) | ||
{ | ||
if (ShouldWrite) | ||
{ | ||
base.Write(value); | ||
} | ||
} | ||
|
||
public override void Write(char[] buffer, int index, int count) | ||
{ | ||
if (ShouldWrite) | ||
{ | ||
base.Write(buffer, index, count); | ||
} | ||
} | ||
|
||
public override void Write(string? value) | ||
{ | ||
if (ShouldWrite) | ||
{ | ||
base.Write(value); | ||
} | ||
} | ||
|
||
public override void Write(int value) | ||
{ | ||
if (ShouldWrite) | ||
{ | ||
base.Write(value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,56 @@ | ||
using Microsoft.AspNetCore.Components; | ||
using System.Diagnostics; | ||
using Htmxor.Components; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
using Microsoft.AspNetCore.Components.RenderTree; | ||
|
||
namespace Htmxor.Rendering; | ||
|
||
internal class HtmxorComponentState : ComponentState | ||
{ | ||
public HtmxorComponentState(Renderer renderer, int componentId, IComponent component, HtmxorComponentState? parentComponentState) | ||
private readonly HtmxorComponentState? parentComponentState; | ||
private int conditionalChildrenCount; | ||
private IConditionalOutputComponent? conditionalOutput; | ||
private bool isDisposed; | ||
|
||
public HtmxorComponentState(HtmxorRenderer renderer, int componentId, IComponent component, HtmxorComponentState? parentComponentState) | ||
: base(renderer, componentId, component, parentComponentState) | ||
{ | ||
if (component is IConditionalOutputComponent conditionalOutput) | ||
{ | ||
this.conditionalOutput = conditionalOutput; | ||
parentComponentState?.ConditionalChildAdded(); | ||
} | ||
|
||
this.parentComponentState = parentComponentState; | ||
} | ||
|
||
public override ValueTask DisposeAsync() | ||
{ | ||
if (parentComponentState is not null && conditionalOutput is not null && !isDisposed) | ||
{ | ||
parentComponentState.ConditionalChildDisposed(); | ||
} | ||
|
||
isDisposed = true; | ||
|
||
return base.DisposeAsync(); | ||
} | ||
|
||
internal bool HasPartialFragments() | ||
private void ConditionalChildAdded() | ||
{ | ||
throw new NotImplementedException(); | ||
conditionalChildrenCount++; | ||
parentComponentState?.ConditionalChildAdded(); | ||
} | ||
|
||
private void ConditionalChildDisposed() | ||
{ | ||
conditionalChildrenCount--; | ||
parentComponentState?.ConditionalChildDisposed(); | ||
Debug.Assert(conditionalChildrenCount >= 0, "conditionalChildrenCount should never be able to be less than zero"); | ||
} | ||
|
||
internal bool ShouldGenerateMarkup() | ||
=> conditionalOutput?.ShouldOutput(conditionalChildrenCount) | ||
?? parentComponentState?.ShouldGenerateMarkup() | ||
?? true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.