Skip to content

Enable inline documentation #44

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

Merged
merged 5 commits into from
May 4, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
- fix small bug of incorrect cache of manifest paths
- small improvements to lazy component (loading, error and required)
  • Loading branch information
isc30 committed May 4, 2020
commit bcc62efc0229a688fb7836aa57bde04ca15658a6
13 changes: 10 additions & 3 deletions demo/WasmHost/Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -9,11 +9,18 @@
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<Lazy Name="SimpleComponent" OnBeforeLoadAsync="Delay(500, 2500)">
<LazyPlaceholder />
<Loading>
<LazyPlaceholder />
</Loading>
<Error>
<h2>Component loading errored...</h2>
</Error>
</Lazy>

<Lazy Name="NamedComponent" OnBeforeLoadAsync="Delay(500, 2500)">
<LazyPlaceholder />
<Lazy Name="NamedComponent" Required="true" OnBeforeLoadAsync="Delay(500, 2500)">
<Loading>
<LazyPlaceholder />
</Loading>
</Lazy>

@code {
4 changes: 1 addition & 3 deletions nuget/BlazorLazyLoading.Server/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -76,9 +76,7 @@ public static void UseLazyLoading(
public sealed class LazyLoadingOptions
{
/// <summary>
/// <br>Specifies a list of Module Names (hints) to:</br>
/// <br> - Download DLLs from them</br>
/// <br> - Use their manifest to locate lazy resources</br>
/// Specifies a list of Module Names (hints) to download DLLs from them and use their manifest to locate lazy resources
/// </summary>
public IEnumerable<string> ModuleHints { get; set; } = Array.Empty<string>();

4 changes: 1 addition & 3 deletions nuget/BlazorLazyLoading.Wasm/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -41,9 +41,7 @@ public static IServiceCollection AddLazyLoading(
public sealed class LazyLoadingOptions
{
/// <summary>
/// <br>Specifies a list of Module Names (hints) to:</br>
/// <br> - Download DLLs from them</br>
/// <br> - Use their manifest to locate lazy resources</br>
/// Specifies a list of Module Names (hints) to download DLLs from them and use their manifest to locate lazy resources
/// </summary>
public IEnumerable<string> ModuleHints { get; set; } = Array.Empty<string>();

3 changes: 2 additions & 1 deletion src/AssemblyLoader/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@ namespace BlazorLazyLoading.Extensions
{
public static class EnumerableExtensions
{
public static IEnumerable<T> NotNull<T>(this IEnumerable<T?> source)
public static IEnumerable<T> NotNull<T>(
this IEnumerable<T?> source)
where T : class
{
return source.Where(i => i != null).Cast<T>();
4 changes: 2 additions & 2 deletions src/AssemblyLoader/Services/AssemblyDataLocator.cs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ namespace BlazorLazyLoading.Services
/// <summary>
/// Locates assembly DLLs based on context and hints
/// </summary>
public class AssemblyDataLocator : IAssemblyDataLocator
public sealed class AssemblyDataLocator : IAssemblyDataLocator
{
private readonly ILazyModuleHintsProvider _lazyModuleNamesProvider;

@@ -20,7 +20,7 @@ public AssemblyDataLocator(
}

/// <inheritdoc/>
public virtual IEnumerable<string> GetFindPaths(
public IEnumerable<string> GetFindPaths(
AssemblyName assemblyName,
AssemblyLoaderContext context)
{
61 changes: 47 additions & 14 deletions src/LazyComponents/LazyComponent/Lazy.cs
Original file line number Diff line number Diff line change
@@ -17,7 +17,13 @@ public class Lazy : ComponentBase
public string Name { get; set; } = null!;

[Parameter]
public RenderFragment? ChildContent { get; set; } = null;
public bool Required { get; set; } = false;

[Parameter]
public RenderFragment? Loading { get; set; } = null;

[Parameter]
public RenderFragment? Error { get; set; } = null;

[Parameter]
public Func<Lazy, Task>? OnBeforeLoadAsync { get; set; } = null;
@@ -35,8 +41,21 @@ public class Lazy : ComponentBase
[Inject]
private IManifestRepository _manifestRepository { get; set; } = null!;

private RenderFragment? _currentFallbackBuilder = null;

protected override void OnInitialized()
{
_currentFallbackBuilder = Loading;
base.OnInitialized(); // trigger initial render
}

protected override async Task OnInitializedAsync()
{
if (OnBeforeLoadAsync != null)
{
await OnBeforeLoadAsync(this);
}

await base.OnInitializedAsync().ConfigureAwait(false);

var allManifests = await _manifestRepository.GetAllAsync().ConfigureAwait(false);
@@ -71,20 +90,24 @@ protected override async Task OnInitializedAsync()
}));

var bestMatches = manifests
.Where(i => i.Score > 0)
.GroupBy(i => i.Score)
.OrderByDescending(i => i.Key)
.FirstOrDefault()
?.ToList();

if (bestMatches == null || !bestMatches.Any())
{
Debug.WriteLine($"Unable to find lazy component '{Name}'");
DisplayErrorView();
ThrowIfRequired($"Unable to find lazy component '{Name}'. Required: {(Required ? "true" : "false")}");
return;
}

if (bestMatches.Count > 1)
{
throw new NotSupportedException($"Multiple matches for Component with name '{Name}': '{string.Join(";", bestMatches.Select(m => m.Match.TypeFullName))}'");
DisplayErrorView();
ThrowIfRequired($"Multiple matches for Component with name '{Name}': '{string.Join(";", bestMatches.Select(m => m.Match.TypeFullName))}'");
return;
}

var bestMatch = bestMatches.First();
@@ -97,12 +120,13 @@ protected override async Task OnInitializedAsync()
})
.ConfigureAwait(false);

if (OnBeforeLoadAsync != null)
Type = componentAssembly?.GetType(bestMatch.Match.TypeFullName);

if (Type == null)
{
await OnBeforeLoadAsync(this);
DisplayErrorView(false);
}

Type = componentAssembly?.GetType(bestMatch.Match.TypeFullName);
StateHasChanged();
}

@@ -125,16 +149,25 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

private void BuildFallbackComponent(RenderTreeBuilder builder)
{
if (ChildContent != null)
_currentFallbackBuilder?.Invoke(builder);
}

private void DisplayErrorView(bool render = true)
{
_currentFallbackBuilder = Error;
if (render) StateHasChanged();
}

private void ThrowIfRequired(string errorMessage)
{
if (Required)
{
ChildContent.Invoke(builder);
return;
throw new NotSupportedException(errorMessage);
}
else
{
Debug.WriteLine(errorMessage);
}

builder.OpenElement(0, "div");
builder.AddAttribute(1, "class", "bll-loading");
builder.AddContent(2, "Loading...");
builder.CloseElement();
}
}
}
3 changes: 3 additions & 0 deletions src/ManifestReader/Abstractions/IManifestLocator.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

namespace BlazorLazyLoading.Abstractions
{
/// <summary>
/// Locates _lazy.json manifests
/// </summary>
public interface IManifestLocator
{
/// <summary>
5 changes: 5 additions & 0 deletions src/ManifestReader/Services/ManifestLocator.cs
Original file line number Diff line number Diff line change
@@ -4,16 +4,21 @@

namespace BlazorLazyLoading.Services
{
/// <summary>
/// Locates _lazy.json manifests based on hints
/// </summary>
public sealed class ManifestLocator : IManifestLocator
{
private readonly ILazyModuleHintsProvider _moduleHintsProvider;

/// <inheritdoc/>
public ManifestLocator(
ILazyModuleHintsProvider moduleHintsProvider)
{
_moduleHintsProvider = moduleHintsProvider;
}

/// <inheritdoc/>
public IEnumerable<string> GetManifestPaths()
{
return _moduleHintsProvider.ModuleNameHints
12 changes: 9 additions & 3 deletions src/ManifestReader/Services/ManifestRepository.cs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using BlazorLazyLoading.Abstractions;
using BlazorLazyLoading.Extensions;
using BlazorLazyLoading.Models;
using Newtonsoft.Json.Linq;

@@ -70,7 +71,7 @@ private async Task FetchAllManifests()
private async Task ReadAllManifests()
{
var manifestDataTasks = new Dictionary<string, Task<byte[]?>>();
var manifestPaths = _manifestLocator.GetManifestPaths();
var manifestPaths = _manifestLocator.GetManifestPaths().Except(_loadedPaths.Keys);

foreach (var path in manifestPaths)
{
@@ -79,11 +80,16 @@ private async Task ReadAllManifests()

await Task.WhenAll(manifestDataTasks.Values.ToArray()).ConfigureAwait(false);

var manifestData = manifestDataTasks
var pathData = manifestDataTasks
.Select(i => new { Path = i.Key, Data = i.Value.Result })
.ToList();

var manifestModels = manifestData
foreach (var path in pathData)
{
_loadedPaths.TryAdd(path.Path, path.Data);
}

var manifestModels = pathData
.Where(i => i.Data != null)
.ToDictionary(i => i.Path, i => JObject.Parse(Encoding.UTF8.GetString(i.Data!)));