Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#104] Implement StyledGroup component to handle style loading state. #105

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/BlazorStyled/Styled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ public class Styled : ComponentBase
[Parameter] public EventCallback<string> ClassnameChanged { get; set; }
[Parameter] public string GlobalStyle { get; set; }
[Parameter(CaptureUnmatchedValues = true)] public IReadOnlyDictionary<string, object> ComposeAttributes { get; set; }
[CascadingParameter] public StyledGroupContext StyleGroupContext { get; set; }

[Inject] private IStyled StyledService { get; set; }

protected override async Task OnAfterRenderAsync(bool firstRender)
{
await ProcessParameters();
if (StyleGroupContext == null)
{
await ProcessParameters();
}
else
{
StyleGroupContext.RegisterLoadTask(ProcessParameters());
}
}

private async Task ProcessParameters()
Expand Down
3 changes: 3 additions & 0 deletions src/BlazorStyled/StyledGroup.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<CascadingValue Value="StyleGroupContext">
@ChildContent
</CascadingValue>
45 changes: 45 additions & 0 deletions src/BlazorStyled/StyledGroup.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using Microsoft.AspNetCore.Components;

namespace BlazorStyled
{
public partial class StyledGroup : IDisposable
{
[Parameter] public RenderFragment ChildContent { get; set; }
[Parameter] public bool Loading { get; set; } = true;
[Parameter] public EventCallback<bool> LoadingChanged { get; set; }
[Parameter] public TimeSpan DebouncerTimeout { get; set; } = TimeSpan.Zero;

public StyledGroupContext StyleGroupContext { get; set; } = new StyledGroupContext();

protected override void OnInitialized()
{
StyleGroupContext.OnLoadingChanged += OnLoadingChanged;
StyleGroupContext.SetRendering(true);
}

protected override void OnParametersSet()
{
StyleGroupContext.DebouncerTimeout = DebouncerTimeout;
}

protected override void OnAfterRender(bool firstRender)
{
StyleGroupContext.SetRendering(false);
}

private async void OnLoadingChanged(bool loading)
{
Loading = loading;
if (LoadingChanged.HasDelegate)
{
await InvokeAsync(() => LoadingChanged.InvokeAsync(loading));
}
}

public void Dispose()
{
StyleGroupContext.OnLoadingChanged -= OnLoadingChanged;
}
}
}
102 changes: 102 additions & 0 deletions src/BlazorStyled/StyledGroupContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using System.Threading;
using System.Threading.Tasks;

namespace BlazorStyled
{
public class StyledGroupContext
{
private int mTaskCounter;
public bool Loading { get; private set; } = true;
public event Action<bool> OnLoadingChanged;
private bool mRendering = true;
private CancellationTokenSource mDebouncerCancellationTokenSource;
public TimeSpan DebouncerTimeout { get; set; } = TimeSpan.Zero;

public void SetRendering(bool value)
{
if (DebouncerTimeout > TimeSpan.Zero)
{
DebounceSetRendering(value);
}
else
{
SetRendering(value);
}
}

private void SetRenderingInternal(bool value)
{
if (value == mRendering)
{
return;
}

mRendering = value;

// Rendering is finished, there are no running tasks and Loading is still true
// This can happen when all registered tasks are completed synchronously and none of them are actually registered
if (!mRendering && mTaskCounter == 0)
{
SetLoading(false);
}
}

public void RegisterLoadTask(Task task)
{
if (task.IsCompleted)
{
return;
}

SetLoading(true);
Interlocked.Increment(ref mTaskCounter);
task.ContinueWith(OnLoadTaskCompleted);
}

private void OnLoadTaskCompleted(Task _)
{
Interlocked.Decrement(ref mTaskCounter);
if (mTaskCounter == 0)
{
SetLoading(false);
}
}

private void SetLoading(bool value)
{
if (Loading == value)
{
return;
}

// Trying to finish loading before OnAfterRender call - handled in SetRendering
if (!value && mRendering)
{
return;
}

Loading = value;
OnLoadingChanged?.Invoke(value);
}

private async Task DebouncerTask(bool value, CancellationToken token)
{
await Task.Delay(DebouncerTimeout);

if (token.IsCancellationRequested)
{
return;
}

SetRenderingInternal(value);
}

private void DebounceSetRendering(bool value)
{
mDebouncerCancellationTokenSource?.Cancel();
mDebouncerCancellationTokenSource = new CancellationTokenSource();
DebouncerTask(value, mDebouncerCancellationTokenSource.Token).ConfigureAwait(false);
}
}
}