Skip to content

Update ResourceCollectionProvider to take advantage of Declarative persistent component state #61748

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public static IRazorComponentsBuilder AddRazorComponents(this IServiceCollection
services.TryAddScoped<WebAssemblySettingsEmitter>();

services.TryAddScoped<ResourceCollectionProvider>();
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<ResourceCollectionProvider>(services, RenderMode.InteractiveWebAssembly);

// Form handling
services.AddSupplyValueFromFormProvider();
Expand Down
23 changes: 8 additions & 15 deletions src/Components/Shared/src/ResourceCollectionProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,29 @@ namespace Microsoft.AspNetCore.Components;

internal class ResourceCollectionProvider
{
private const string ResourceCollectionUrlKey = "__ResourceCollectionUrl";
private string? _url;

[SupplyParameterFromPersistentComponentState]
public string? ResourceCollectionUrl
{
get => _url;
set => _url = value;
}
Comment on lines +17 to +21
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public string? ResourceCollectionUrl
{
get => _url;
set => _url = value;
}
public string? ResourceCollectionUrl { get; set; }

private ResourceAssetCollection? _resourceCollection;
private readonly PersistentComponentState _state;
private readonly IJSRuntime _jsRuntime;

[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Strings are not trimmed")]
public ResourceCollectionProvider(PersistentComponentState state, IJSRuntime jsRuntime)
public ResourceCollectionProvider(IJSRuntime jsRuntime)
{
_state = state;
_jsRuntime = jsRuntime;
_ = _state.TryTakeFromJson(ResourceCollectionUrlKey, out _url);
}

[MemberNotNull(nameof(_url))]
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Strings are not trimmed")]
internal void SetResourceCollectionUrl(string url)
{
if (_url != null)
{
throw new InvalidOperationException("The resource collection URL has already been set.");
}
_url = url;
PersistingComponentStateSubscription registration = default;
registration = _state.RegisterOnPersisting(() =>
{
_state.PersistAsJson(ResourceCollectionUrlKey, _url);
registration.Dispose();
return Task.CompletedTask;
}, RenderMode.InteractiveWebAssembly);
}

internal async Task<ResourceAssetCollection> GetResourceCollection()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ internal void InitializeDefaultServices()
Services.AddSupplyValueFromPersistentComponentStateProvider();
Services.AddSingleton<IErrorBoundaryLogger, WebAssemblyErrorBoundaryLogger>();
Services.AddSingleton<ResourceCollectionProvider>();
RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<ResourceCollectionProvider>(Services, RenderMode.InteractiveWebAssembly);
Services.AddLogging(builder =>
{
builder.AddProvider(new WebAssemblyConsoleLoggerProvider(DefaultWebAssemblyJSRuntime.Instance));
Expand Down
Loading