Skip to content
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
8 changes: 6 additions & 2 deletions aspnetcore/blazor/webassembly-lazy-load-assemblies.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,13 +596,15 @@ The assembly is assigned to <xref:Microsoft.AspNetCore.Components.Routing.Router

@code {
private List<Assembly> lazyLoadedAssemblies = new();
private bool grantImaharaRobotControlsAssemblyLoaded;

private async Task OnNavigateAsync(NavigationContext args)
{
try
{
if (args.Path == "robot")
if ((args.Path == "robot") && !grantImaharaRobotControlsAssemblyLoaded)
{
grantImaharaRobotControlsAssemblyLoaded = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT question on the location of this line ...

Because the component survives an exception on the next two lines, does it make sense to move this line (grantImaharaRobotControlsAssemblyLoaded = true;) to the end after the lazyLoadedAssemblies.AddRange(assemblies); line (i.e., it's only true if we know those two lines executed)?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to go ahead and move the line down to get this in. I'm about to shoot a bunch of PRs into the live branch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@guardrex Neither of the two alternatives is perfect. I usually prefer to set the flag before an awaited operation - because with the await there, another navigation might occur while waiting for the assembly to load, leaving the flag still false for the second run.

In this case, since the awaited assembly load won’t cause a critical failure if called repeatedly, I agree that your version is better.

var assemblies = await AssemblyLoader.LoadAssembliesAsync(
new[] { "GrantImaharaRobotControls.{FILE EXTENSION}" });
lazyLoadedAssemblies.AddRange(assemblies);
Expand Down Expand Up @@ -648,13 +650,15 @@ The assembly is assigned to <xref:Microsoft.AspNetCore.Components.Routing.Router

@code {
private List<Assembly> lazyLoadedAssemblies = new List<Assembly>();
private bool grantImaharaRobotControlsAssemblyLoaded;

private async Task OnNavigateAsync(NavigationContext args)
{
try
{
if (args.Path == "robot")
if ((args.Path == "robot") && !grantImaharaRobotControlsAssemblyLoaded)
{
grantImaharaRobotControlsAssemblyLoaded = true;
var assemblies = await AssemblyLoader.LoadAssembliesAsync(
new[] { "GrantImaharaRobotControls.{FILE EXTENSION}" });
lazyLoadedAssemblies.AddRange(assemblies);
Expand Down