-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba35ca2
commit 82143fb
Showing
36 changed files
with
2,295 additions
and
120 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 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
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,44 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace StrawberryShake.Razor | ||
{ | ||
public abstract class DataComponent<TClientOrOperation> | ||
: ComponentBase | ||
, IDisposable | ||
{ | ||
private readonly List<IDisposable> _subscriptions = new(); | ||
private bool _disposed; | ||
|
||
[Inject] | ||
protected internal TClientOrOperation ClientOrOperation { get; internal set; } = default!; | ||
|
||
public void Subscribe(Func<TClientOrOperation, IDisposable> subscribe) | ||
{ | ||
_subscriptions.Add(subscribe(ClientOrOperation)); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected void Dispose(bool disposing) | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (disposing) | ||
{ | ||
foreach (var subscription in _subscriptions) | ||
{ | ||
subscription.Dispose(); | ||
} | ||
} | ||
|
||
_disposed = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Components; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
|
||
namespace StrawberryShake.Razor | ||
{ | ||
public abstract class QueryBase<TResult> | ||
: ComponentBase | ||
, IDisposable | ||
where TResult : class | ||
{ | ||
private IDisposable? _subscription; | ||
private bool _isLoading = true; | ||
private bool _isErrorResult; | ||
private bool _isSuccessResult; | ||
private TResult? _result; | ||
private IReadOnlyList<IClientError>? _errors; | ||
private bool _disposed; | ||
|
||
[Parameter] | ||
public RenderFragment<TResult> Content { get; set; } = default!; | ||
|
||
[Parameter] | ||
public RenderFragment<IReadOnlyList<IClientError>>? Error { get; set; } | ||
|
||
[Parameter] | ||
public RenderFragment? Loading { get; set; } | ||
|
||
[Parameter] | ||
public ExecutionStrategy Strategy { get; set; } | ||
|
||
protected void Subscribe(IObservable<IOperationResult<TResult>> observable) | ||
{ | ||
_subscription = observable.Subscribe(operationResult => | ||
{ | ||
_result = operationResult.Data; | ||
_errors = operationResult.Errors; | ||
_isErrorResult = operationResult.IsErrorResult(); | ||
_isSuccessResult = operationResult.IsSuccessResult(); | ||
_isLoading = false; | ||
StateHasChanged(); | ||
}); | ||
} | ||
|
||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
if (_isLoading && Loading is not null) | ||
{ | ||
builder.AddContent(0, Loading); | ||
} | ||
|
||
if (_isErrorResult) | ||
{ | ||
builder.AddContent(0, Error, _errors!); | ||
} | ||
|
||
if (_isSuccessResult) | ||
{ | ||
builder.AddContent(0, Content, _result!); | ||
} | ||
|
||
base.BuildRenderTree(builder); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
protected void Dispose(bool disposing) | ||
{ | ||
if (!_disposed) | ||
{ | ||
if (disposing) | ||
{ | ||
_subscription?.Dispose(); | ||
} | ||
|
||
_disposed = true; | ||
} | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/StrawberryShake/Client/src/Razor/StrawberryShake.Razor.csproj
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,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
||
<PropertyGroup> | ||
<PackageId>StrawberryShake.Razor</PackageId> | ||
<AssemblyName>StrawberryShake.Razor</AssemblyName> | ||
<RootNamespace>StrawberryShake.Razor</RootNamespace> | ||
<Nullable>enable</Nullable> | ||
<Description>Provides razor components for using StrawberryShake.</Description> | ||
<TargetFrameworks>net5.0</TargetFrameworks> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<SupportedPlatform Include="browser" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="5.0.4" /> | ||
<PackageReference Include="System.Reactive" Version="5.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Core\StrawberryShake.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,2 @@ | ||
@using Microsoft.AspNetCore.Components.Web | ||
@using StrawberryShake |
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
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.