Skip to content
Merged
Show file tree
Hide file tree
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
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;
}
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