Skip to content

Commit a2c9f1c

Browse files
Copilotjaviercn
andcommitted
Address PR review comments - remove overloads, use null instead of default reason, implement singleton pattern
Co-authored-by: javiercn <[email protected]>
1 parent 4439a8c commit a2c9f1c

14 files changed

+93
-88
lines changed

src/Components/Components/src/DefaultPersistenceReason.cs

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Components/Components/src/PersistComponentStateRegistration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ namespace Microsoft.AspNetCore.Components;
66
internal readonly struct PersistComponentStateRegistration(
77
Func<Task> callback,
88
IComponentRenderMode? renderMode,
9-
IReadOnlyList<IPersistenceReasonFilter>? reasonFilters = null)
9+
IReadOnlyList<IPersistenceReasonFilter> reasonFilters)
1010
{
1111
public Func<Task> Callback { get; } = callback;
1212

1313
public IComponentRenderMode? RenderMode { get; } = renderMode;
1414

15-
public IReadOnlyList<IPersistenceReasonFilter>? ReasonFilters { get; } = reasonFilters;
15+
public IReadOnlyList<IPersistenceReasonFilter> ReasonFilters { get; } = reasonFilters ?? Array.Empty<IPersistenceReasonFilter>();
1616
}

src/Components/Components/src/PersistentComponentState.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal void InitializeExistingState(IDictionary<string, byte[]> existingState)
4343
/// <param name="callback">The callback to invoke when the application is being paused.</param>
4444
/// <returns>A subscription that can be used to unregister the callback when disposed.</returns>
4545
public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback)
46-
=> RegisterOnPersisting(callback, null);
46+
=> RegisterOnPersisting(callback, null, Array.Empty<IPersistenceReasonFilter>());
4747

4848
/// <summary>
4949
/// Register a callback to persist the component state when the application is about to be paused.
@@ -53,7 +53,7 @@ public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> call
5353
/// <param name="renderMode"></param>
5454
/// <param name="reasonFilters">Filters to control when the callback should be invoked based on the persistence reason.</param>
5555
/// <returns>A subscription that can be used to unregister the callback when disposed.</returns>
56-
public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback, IComponentRenderMode? renderMode, IReadOnlyList<IPersistenceReasonFilter>? reasonFilters)
56+
public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback, IComponentRenderMode? renderMode, IReadOnlyList<IPersistenceReasonFilter> reasonFilters)
5757
{
5858
ArgumentNullException.ThrowIfNull(callback);
5959

@@ -85,7 +85,7 @@ public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> call
8585
throw new InvalidOperationException("Registering a callback while persisting state is not allowed.");
8686
}
8787

88-
var persistenceCallback = new PersistComponentStateRegistration(callback, renderMode);
88+
var persistenceCallback = new PersistComponentStateRegistration(callback, renderMode, Array.Empty<IPersistenceReasonFilter>());
8989

9090
_registeredCallbacks.Add(persistenceCallback);
9191

src/Components/Components/src/PersistentState/ComponentStatePersistenceManager.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,14 @@ public async Task RestoreStateAsync(IPersistentComponentStateStore store)
6161
_servicesRegistry?.Restore(State);
6262
}
6363

64-
/// <summary>
65-
/// Persists the component application state into the given <see cref="IPersistentComponentStateStore"/>.
66-
/// </summary>
67-
/// <param name="store">The <see cref="IPersistentComponentStateStore"/> to restore the application state from.</param>
68-
/// <param name="renderer">The <see cref="Renderer"/> that components are being rendered.</param>
69-
/// <returns>A <see cref="Task"/> that will complete when the state has been restored.</returns>
70-
public Task PersistStateAsync(IPersistentComponentStateStore store, Renderer renderer)
71-
=> PersistStateAsync(store, renderer, DefaultPersistenceReason.Instance);
72-
7364
/// <summary>
7465
/// Persists the component application state into the given <see cref="IPersistentComponentStateStore"/>.
7566
/// </summary>
7667
/// <param name="store">The <see cref="IPersistentComponentStateStore"/> to restore the application state from.</param>
7768
/// <param name="renderer">The <see cref="Renderer"/> that components are being rendered.</param>
7869
/// <param name="persistenceReason">The reason for persisting the state.</param>
7970
/// <returns>A <see cref="Task"/> that will complete when the state has been restored.</returns>
80-
public Task PersistStateAsync(IPersistentComponentStateStore store, Renderer renderer, IPersistenceReason persistenceReason)
71+
public Task PersistStateAsync(IPersistentComponentStateStore store, Renderer renderer, IPersistenceReason? persistenceReason = null)
8172
{
8273
if (_stateIsPersisted)
8374
{
@@ -186,10 +177,7 @@ private void InferRenderModes(Renderer renderer)
186177
}
187178
}
188179

189-
internal Task<bool> TryPauseAsync(IPersistentComponentStateStore store)
190-
=> TryPauseAsync(store, DefaultPersistenceReason.Instance);
191-
192-
internal Task<bool> TryPauseAsync(IPersistentComponentStateStore store, IPersistenceReason persistenceReason)
180+
internal Task<bool> TryPauseAsync(IPersistentComponentStateStore store, IPersistenceReason? persistenceReason = null)
193181
{
194182
List<Task<bool>>? pendingCallbackTasks = null;
195183

@@ -213,21 +201,21 @@ internal Task<bool> TryPauseAsync(IPersistentComponentStateStore store, IPersist
213201
}
214202

215203
// Evaluate reason filters to determine if the callback should be executed for this persistence reason
216-
if (registration.ReasonFilters != null)
204+
if (registration.ReasonFilters.Count > 0)
217205
{
218206
var shouldPersist = EvaluateReasonFilters(registration.ReasonFilters, persistenceReason);
219207
if (shouldPersist.HasValue && !shouldPersist.Value)
220208
{
221209
// Filters explicitly indicate not to persist for this reason
222210
continue;
223211
}
224-
else if (!shouldPersist.HasValue && !persistenceReason.PersistByDefault)
212+
else if (!shouldPersist.HasValue && !(persistenceReason?.PersistByDefault ?? true))
225213
{
226214
// No filter matched and default is not to persist
227215
continue;
228216
}
229217
}
230-
else if (!persistenceReason.PersistByDefault)
218+
else if (!(persistenceReason?.PersistByDefault ?? true))
231219
{
232220
// No filters defined and default is not to persist
233221
continue;
@@ -306,8 +294,14 @@ static async Task<bool> AnyTaskFailed(List<Task<bool>> pendingCallbackTasks)
306294
}
307295
}
308296

309-
private static bool? EvaluateReasonFilters(IReadOnlyList<IPersistenceReasonFilter> reasonFilters, IPersistenceReason persistenceReason)
297+
private static bool? EvaluateReasonFilters(IReadOnlyList<IPersistenceReasonFilter> reasonFilters, IPersistenceReason? persistenceReason)
310298
{
299+
if (persistenceReason is null)
300+
{
301+
// No reason provided, can't evaluate filters
302+
return null;
303+
}
304+
311305
foreach (var reasonFilter in reasonFilters)
312306
{
313307
var shouldPersist = reasonFilter.ShouldPersist(persistenceReason);

src/Components/Components/src/PublicAPI.Shipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ Microsoft.AspNetCore.Components.IHandleEvent
167167
Microsoft.AspNetCore.Components.IHandleEvent.HandleEventAsync(Microsoft.AspNetCore.Components.EventCallbackWorkItem item, object? arg) -> System.Threading.Tasks.Task!
168168
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager
169169
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.ComponentStatePersistenceManager(Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager!>! logger) -> void
170-
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.PersistStateAsync(Microsoft.AspNetCore.Components.IPersistentComponentStateStore! store, Microsoft.AspNetCore.Components.RenderTree.Renderer! renderer) -> System.Threading.Tasks.Task!
170+
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.PersistStateAsync(Microsoft.AspNetCore.Components.IPersistentComponentStateStore! store, Microsoft.AspNetCore.Components.RenderTree.Renderer! renderer, Microsoft.AspNetCore.Components.IPersistenceReason? persistenceReason = null) -> System.Threading.Tasks.Task!
171171
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.RestoreStateAsync(Microsoft.AspNetCore.Components.IPersistentComponentStateStore! store) -> System.Threading.Tasks.Task!
172172
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.State.get -> Microsoft.AspNetCore.Components.PersistentComponentState!
173173
Microsoft.AspNetCore.Components.InjectAttribute

src/Components/Components/src/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Microsoft.AspNetCore.Components.Routing.NotFoundEventArgs.NotFoundEventArgs(stri
1010
Microsoft.AspNetCore.Components.Routing.NotFoundEventArgs.Path.get -> string!
1111
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.ComponentStatePersistenceManager(Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager!>! logger, System.IServiceProvider! serviceProvider) -> void
1212
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.SetPlatformRenderMode(Microsoft.AspNetCore.Components.IComponentRenderMode! renderMode) -> void
13-
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.PersistStateAsync(Microsoft.AspNetCore.Components.IPersistentComponentStateStore! store, Microsoft.AspNetCore.Components.RenderTree.Renderer! renderer, Microsoft.AspNetCore.Components.IPersistenceReason! persistenceReason) -> System.Threading.Tasks.Task!
1413
Microsoft.AspNetCore.Components.Infrastructure.RegisterPersistentComponentStateServiceCollectionExtensions
1514
Microsoft.AspNetCore.Components.SupplyParameterFromPersistentComponentStateAttribute
1615
Microsoft.AspNetCore.Components.SupplyParameterFromPersistentComponentStateAttribute.SupplyParameterFromPersistentComponentStateAttribute() -> void
@@ -21,7 +20,7 @@ Microsoft.AspNetCore.Components.IPersistenceReasonFilter.ShouldPersist(Microsoft
2120
Microsoft.AspNetCore.Components.PersistReasonFilter<TReason>
2221
Microsoft.AspNetCore.Components.PersistReasonFilter<TReason>.PersistReasonFilter(bool persist) -> void
2322
Microsoft.AspNetCore.Components.PersistReasonFilter<TReason>.ShouldPersist(Microsoft.AspNetCore.Components.IPersistenceReason! reason) -> bool?
24-
Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting(System.Func<System.Threading.Tasks.Task!>! callback, Microsoft.AspNetCore.Components.IComponentRenderMode? renderMode, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.IPersistenceReasonFilter!>? reasonFilters) -> Microsoft.AspNetCore.Components.PersistingComponentStateSubscription
23+
Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting(System.Func<System.Threading.Tasks.Task!>! callback, Microsoft.AspNetCore.Components.IComponentRenderMode? renderMode, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.IPersistenceReasonFilter!>! reasonFilters) -> Microsoft.AspNetCore.Components.PersistingComponentStateSubscription
2524
Microsoft.Extensions.DependencyInjection.SupplyParameterFromPersistentComponentStateProviderServiceCollectionExtensions
2625
static Microsoft.AspNetCore.Components.Infrastructure.RegisterPersistentComponentStateServiceCollectionExtensions.AddPersistentServiceRegistration<TService>(Microsoft.Extensions.DependencyInjection.IServiceCollection! services, Microsoft.AspNetCore.Components.IComponentRenderMode! componentRenderMode) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!
2726
static Microsoft.AspNetCore.Components.Infrastructure.ComponentsMetricsServiceCollectionExtensions.AddComponentsMetrics(Microsoft.Extensions.DependencyInjection.IServiceCollection! services) -> Microsoft.Extensions.DependencyInjection.IServiceCollection!

src/Components/Endpoints/src/Rendering/EndpointHtmlRenderer.PrerenderingState.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public async ValueTask<IHtmlContent> PrerenderPersistedStateAsync(HttpContext ht
5151
if (store != null)
5252
{
5353
IPersistenceReason persistenceReason = IsProgressivelyEnhancedNavigation(httpContext.Request)
54-
? new PersistOnEnhancedNavigation()
55-
: new PersistOnPrerendering();
54+
? PersistOnEnhancedNavigation.Instance
55+
: PersistOnPrerendering.Instance;
5656
await manager.PersistStateAsync(store, this, persistenceReason);
5757
return store switch
5858
{
@@ -84,8 +84,8 @@ public async ValueTask<IHtmlContent> PrerenderPersistedStateAsync(HttpContext ht
8484
store = new CompositeStore(server, auto, webAssembly);
8585

8686
IPersistenceReason persistenceReason = IsProgressivelyEnhancedNavigation(httpContext.Request)
87-
? new PersistOnEnhancedNavigation()
88-
: new PersistOnPrerendering();
87+
? PersistOnEnhancedNavigation.Instance
88+
: PersistOnPrerendering.Instance;
8989
await manager.PersistStateAsync(store, this, persistenceReason);
9090

9191
foreach (var kvp in auto.Saved)

src/Components/Server/src/Circuits/CircuitPersistenceManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task PauseCircuitAsync(CircuitHost circuit, bool saveStateToClient
2828
collector.PersistRootComponents,
2929
RenderMode.InteractiveServer);
3030

31-
await persistenceManager.PersistStateAsync(collector, renderer, new PersistOnCircuitPause());
31+
await persistenceManager.PersistStateAsync(collector, renderer, PersistOnCircuitPause.Instance);
3232

3333
if (saveStateToClient)
3434
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components.Web;
5+
6+
/// <summary>
7+
/// Represents persistence when a circuit is paused.
8+
/// </summary>
9+
public sealed class PersistOnCircuitPause : IPersistenceReason
10+
{
11+
/// <summary>
12+
/// Gets the singleton instance of <see cref="PersistOnCircuitPause"/>.
13+
/// </summary>
14+
public static readonly PersistOnCircuitPause Instance = new();
15+
16+
private PersistOnCircuitPause() { }
17+
18+
/// <inheritdoc />
19+
public bool PersistByDefault => true;
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components.Web;
5+
6+
/// <summary>
7+
/// Represents persistence during enhanced navigation.
8+
/// </summary>
9+
public sealed class PersistOnEnhancedNavigation : IPersistenceReason
10+
{
11+
/// <summary>
12+
/// Gets the singleton instance of <see cref="PersistOnEnhancedNavigation"/>.
13+
/// </summary>
14+
public static readonly PersistOnEnhancedNavigation Instance = new();
15+
16+
private PersistOnEnhancedNavigation() { }
17+
18+
/// <inheritdoc />
19+
public bool PersistByDefault => false;
20+
}

0 commit comments

Comments
 (0)