Skip to content

Introducing LazyNameAttribute #23

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 8 commits into from
Apr 24, 2020
Merged
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
4 changes: 2 additions & 2 deletions demo/Logger/Logger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ItemGroup Condition="!$(BLLUseLocalNugetPackages)">
<ProjectReference Include="..\..\nuget\BlazorLazyLoading.Module\BlazorLazyLoading.Module.csproj" PrivateAssets="all" />
</ItemGroup>
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />

</Project>
3 changes: 3 additions & 0 deletions demo/ModulesHost/Components/ComponentWithName.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@attribute [LazyName("NamedComponent")]

<h3>NAMED COMPONENT (LAZY)</h3>
7 changes: 7 additions & 0 deletions demo/ModulesHost/Components/SimplePage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@page "/simple-page"

<h3>SimplePage</h3>

@code {

}
4 changes: 2 additions & 2 deletions demo/ModulesHost/ModulesHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<ItemGroup Condition="!$(BLLUseLocalNugetPackages)">
<ProjectReference Include="..\..\nuget\BlazorLazyLoading.Module\BlazorLazyLoading.Module.csproj" PrivateAssets="all" />
</ItemGroup>
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
<Import Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.props" />
<Import Condition="!$(BLLUseLocalNugetPackages)" Project="..\..\nuget\BlazorLazyLoading.Module\build\BlazorLazyLoading.Module.targets" />

</Project>
7 changes: 7 additions & 0 deletions demo/ModulesHost/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@using System.Net.Http
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.JSInterop

@using BlazorLazyLoading
6 changes: 5 additions & 1 deletion demo/WasmHost/Pages/Counter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<Lazy Name="Component1" />
<Lazy Name="SimpleComponent">
<div>LOADING SIMPLECOMPONENT...</div>
</Lazy>

<Lazy Name="NamedComponent" />

@code {
private int currentCount = 0;
Expand Down
10 changes: 5 additions & 5 deletions nuget/BlazorLazyLoading.Module/BlazorLazyLoading.Module.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
<ProjectReference Include="..\..\src\ManifestGenerator\ManifestGenerator.csproj" />
</ItemGroup>

<PropertyGroup>
<IncludeSymbols>false</IncludeSymbols>
<NuspecFile>$(MSBuildProjectName).nuspec</NuspecFile>
Expand All @@ -15,11 +20,6 @@
<Description>Build tools for creating a Blazor Lazy Loadable module</Description>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\ManifestGenerator\ManifestGenerator.csproj" />
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Remove="build\bin\**" />
<EmbeddedResource Remove="build\bin\**" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<title>$title$</title>
<description>$description$</description>
<authors>$author$</authors>
<dependencies>
<dependency id="BlazorLazyLoading.Components" version="$version$" />
</dependencies>
</metadata>
<files>
<file src="build\*.*" target="build" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>Library</OutputType>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AssemblyLoader.Server\AssemblyLoader.Server.csproj" />
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
<ProjectReference Include="..\..\src\AssemblyLoader.Server\AssemblyLoader.Server.csproj" />
</ItemGroup>

<PropertyGroup>
Expand Down
5 changes: 4 additions & 1 deletion nuget/BlazorLazyLoading.Wasm/BlazorLazyLoading.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AssemblyLoader.Wasm\AssemblyLoader.Wasm.csproj" />
<ProjectReference Include="..\BlazorLazyLoading.Components\BlazorLazyLoading.Components.csproj" />
<ProjectReference Include="..\..\src\AssemblyLoader.Wasm\AssemblyLoader.Wasm.csproj" />
</ItemGroup>

<PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion nuget/NugetCommon.props
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<Copyright>Ivan Sanz Carasa (isc30)</Copyright>
</PropertyGroup>

<Target Name="SetNuspecProperties" BeforeTargets="Build">
<Target Name="SetNuspecProperties" BeforeTargets="Build;Pack;Restore">
<CallTarget Targets="GetVersion" />
<Message Importance="normal" Text="Stamping MsBuild vars into Nuspec" />
<PropertyGroup>
Expand Down
19 changes: 9 additions & 10 deletions src/LazyComponents/LazyComponent/Lazy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Lazy : ComponentBase
public string Name { get; set; } = null!;

[Parameter]
public Type? Fallback { get; set; } = null;
public RenderFragment? ChildContent { get; set; } = null;

[Inject]
public IAssemblyLoader AssemblyLoader { get; set; } = null!;
Expand All @@ -44,21 +44,20 @@ protected override async Task OnInitializedAsync()
TypeFullName = o.Value<string?>("TypeFullName"),
Name = o.Value<string?>("Name"),
})
.Where(i => !string.IsNullOrWhiteSpace(i.Name) && !string.IsNullOrWhiteSpace(i.TypeFullName))
.Where(i => !string.IsNullOrWhiteSpace(i.TypeFullName))
.Select(o => new
{
TypeFullName = o.TypeFullName!,
Name = o.Name!,
Name = o.Name,
})
.Where(n => n.Name.EndsWith(Name) || n.TypeFullName == Name)
.Select(n => new
{
Match = n,
Score = n.TypeFullName == Name
? 3
: n.Name == Name
? 2
: n.Name.EndsWith(Name)
: n.TypeFullName.EndsWith(Name)
? 1
: 0,
Manifest = m,
Expand All @@ -78,7 +77,7 @@ protected override async Task OnInitializedAsync()

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

var bestMatch = bestMatches.First();
Expand All @@ -97,6 +96,8 @@ protected override async Task OnInitializedAsync()

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);

if (Type == null)
{
BuildFallbackComponent(builder);
Expand All @@ -110,11 +111,9 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)

private void BuildFallbackComponent(RenderTreeBuilder builder)
{
if (Fallback != null)
if (ChildContent != null)
{
builder.OpenComponent(0, Fallback);
builder.CloseComponent();

ChildContent.Invoke(builder);
return;
}

Expand Down
15 changes: 15 additions & 0 deletions src/LazyComponents/LazyComponent/LazyNameAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace BlazorLazyLoading
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
public class LazyNameAttribute : Attribute
{
public string ComponentName { get; }

public LazyNameAttribute(string componentName)
{
ComponentName = componentName;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@ public Dictionary<string, object> GenerateManifest(Assembly assembly)
var componentTypes = assembly.GetTypes()
.Where(t => t.BaseType.FullName == "Microsoft.AspNetCore.Components.ComponentBase");

var components = componentTypes
.Select(t => new ComponentManifest(t.FullName))
.ToList();
var components = new List<ComponentManifest>();

foreach (var component in componentTypes)
{
var lazyNameAttribute = component.GetCustomAttributesData()
.SingleOrDefault(a => a.AttributeType.FullName == "BlazorLazyLoading.LazyNameAttribute");

if (lazyNameAttribute == null)
{
components.Add(new ComponentManifest(component.FullName, null));
continue;
}

var lazyName = (string)lazyNameAttribute.ConstructorArguments[0].Value;
components.Add(new ComponentManifest(component.FullName, lazyName));
}

return new Dictionary<string, object>
{
Expand All @@ -25,13 +38,14 @@ private sealed class ComponentManifest
{
public string TypeFullName { get; }

public string Name { get; }
public string? Name { get; }

public ComponentManifest(
string typeFullName)
string typeFullName,
string? lazyName)
{
TypeFullName = typeFullName;
Name = typeFullName;
Name = lazyName;
}
}
}
Expand Down