Skip to content

Commit

Permalink
introduce HostContextBase
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVollmers committed Oct 5, 2023
1 parent 0a22203 commit fd7a6c0
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
12 changes: 7 additions & 5 deletions packages/Ignis.Components.Server/ServerHostContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
using Microsoft.AspNetCore.Http;
using Ignis.Components.Extensions;
using Microsoft.AspNetCore.Http;

namespace Ignis.Components.Server;

internal class ServerHostContext : IHostContext
internal class ServerHostContext : HostContextBase
{
private readonly IHttpContextAccessor _httpContextAccessor;

public bool IsPrerendering => !_httpContextAccessor.HttpContext?.Response.HasStarted ?? false;
public override bool IsPrerendering => !_httpContextAccessor.HttpContext?.Response.HasStarted ?? false;

public bool IsServerSide => true;
public override bool IsServerSide => true;

public ServerHostContext(IHttpContextAccessor httpContextAccessor)
public ServerHostContext(IEnumerable<IComponentExtension> componentExtensions,
IHttpContextAccessor httpContextAccessor) : base(componentExtensions)
{
_httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
}
Expand Down
14 changes: 10 additions & 4 deletions packages/Ignis.Components.WebAssembly/WebAssemblyHostContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
namespace Ignis.Components.WebAssembly;
using Ignis.Components.Extensions;

internal class WebAssemblyHostContext : IHostContext
namespace Ignis.Components.WebAssembly;

internal class WebAssemblyHostContext : HostContextBase
{
public bool IsPrerendering => false;
public override bool IsPrerendering => false;

public override bool IsServerSide => false;

public bool IsServerSide => false;
public WebAssemblyHostContext(IEnumerable<IComponentExtension> componentExtensions) : base(componentExtensions)
{
}
}
27 changes: 27 additions & 0 deletions packages/Ignis.Components/HostContextBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Ignis.Components.Extensions;

namespace Ignis.Components;

public abstract class HostContextBase : IHostContext
{
public abstract bool IsPrerendering { get; }

public abstract bool IsServerSide { get; }

public IEnumerable<IComponentExtension> ComponentExtensions { get; }

protected HostContextBase(IEnumerable<IComponentExtension> componentExtensions)
{
ComponentExtensions = componentExtensions ?? throw new ArgumentNullException(nameof(componentExtensions));
}

public void OnComponentUpdate(IgnisComponentBase component)
{
if (component == null) throw new ArgumentNullException(nameof(component));

foreach (var extension in ComponentExtensions)
{
extension.OnUpdate(component);
}
}
}
2 changes: 1 addition & 1 deletion packages/Ignis.Components/Ignis.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net7.0'">
<PackageReference Include="Microsoft.AspNetCore.Components" Version="7.0.9"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.85"/>
</ItemGroup>
Expand Down
11 changes: 8 additions & 3 deletions tests/Ignis.Tests.Common/TestHostContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
using Ignis.Components;
using Ignis.Components.Extensions;

namespace Ignis.Tests.Common;

internal class TestHostContext : IHostContext
internal class TestHostContext : HostContextBase
{
public bool IsPrerendering => false;
public override bool IsPrerendering => false;

public bool IsServerSide => true;
public override bool IsServerSide => true;

public TestHostContext(IEnumerable<IComponentExtension> componentExtensions) : base(componentExtensions)
{
}
}

0 comments on commit fd7a6c0

Please sign in to comment.