-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[Blazor] Add ability to filter persistent component state callbacks based on persistence reason #62394
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
[Blazor] Add ability to filter persistent component state callbacks based on persistence reason #62394
Changes from 1 commit
bae3489
2519ee8
0fddad7
f938ea4
4439a8c
a2c9f1c
52e8488
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Components; | ||
|
||
/// <summary> | ||
/// Represents a reason for persisting component state. | ||
/// </summary> | ||
public interface IPersistenceReason | ||
{ | ||
/// <summary> | ||
/// Gets a value indicating whether state should be persisted by default for this reason. | ||
/// </summary> | ||
bool PersistByDefault { get; } | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Components; | ||
|
||
/// <summary> | ||
/// Filters component state persistence based on the reason for persistence. | ||
/// </summary> | ||
public interface IPersistenceReasonFilter | ||
{ | ||
/// <summary> | ||
/// Determines whether state should be persisted for the given reason. | ||
/// </summary> | ||
/// <param name="reason">The reason for persistence.</param> | ||
/// <returns><c>true</c> to persist state, <c>false</c> to skip persistence, or <c>null</c> to defer to other filters or default behavior.</returns> | ||
bool? ShouldPersist(IPersistenceReason reason); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
8000
|
@@ -5,9 +5,12 @@ namespace Microsoft.AspNetCore.Components; | |
|
||
internal readonly struct PersistComponentStateRegistration( | ||
Func<Task> callback, | ||
IComponentRenderMode? renderMode) | ||
IComponentRenderMode? renderMode, | ||
IReadOnlyList<IPersistenceReasonFilter>? reasonFilters = null) | ||
{ | ||
public Func<Task> Callback { get; } = callback; | ||
|
||
public IComponentRenderMode? RenderMode { get; } = renderMode; | ||
|
||
public IReadOnlyList<IPersistenceReasonFilter>? ReasonFilters { get; } = reasonFilters; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Components; | ||
|
||
/// <summary> | ||
/// Base class for filtering component state persistence based on specific persistence reasons. | ||
/// </summary> | ||
/// <typeparam name="TReason">The type of persistence reason this filter handles.</typeparam> | ||
public abstract class PersistReasonFilter<TReason> : Attribute, IPersistenceReasonFilter | ||
where TReason : IPersistenceReason | ||
{ | ||
private readonly bool _persist; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PersistReasonFilter{TReason}"/> class. | ||
/// </summary> | ||
/// <param name="persist">Whether to persist state for the specified reason type.</param> | ||
protected PersistReasonFilter(bool persist) | ||
{ | ||
_persist = persist; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public bool? ShouldPersist(IPersistenceReason reason) | ||
{ | ||
if (reason is TReason) | ||
{ | ||
return _persist; | ||
} | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Components; | ||
|
||
/// <summary> | ||
/// Represents persistence during prerendering. | ||
/// </summary> | ||
public class PersistOnPrerendering : IPersistenceReason | ||
{ | ||
/// <inheritdoc /> | ||
public bool PersistByDefault { get; } = true; | ||
} | ||
|
||
/// <summary> | ||
/// Represents persistence during enhanced navigation. | ||
/// </summary> | ||
public class PersistOnEnhancedNavigation : IPersistenceReason | ||
{ | ||
/// <inheritdoc /> | ||
public bool PersistByDefault { get; } | ||
} | ||
|
||
/// <summary> | ||
/// Represents persistence when a circuit is paused. | ||
/// </summary> | ||
public class PersistOnCircuitPause : IPersistenceReason | ||
{ | ||
/// <inheritdoc /> | ||
public bool PersistByDefault { get; } = true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,30 @@ internal void InitializeExistingState(IDictionary<string, byte[]> existingState) | |
public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback) | ||
=> RegisterOnPersisting(callback, null); | ||
|
||
/// <summary> | ||
/// Register a callback to persist the component state when the application is about to be paused. | ||
/// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes. | ||
/// </summary> | ||
/// <param name="callback">The callback to invoke when the application is being paused.</param> | ||
/// <param name="renderMode"></param> | ||
/// <param name="reasonFilters">Filters to control when the callback should be invoked based on the persistence reason.</param> | ||
/// <returns>A subscription that can be used to unregister the callback when disposed.</returns> | ||
public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> callback, IComponentRenderMode? renderMode, IReadOnlyList<IPersistenceReasonFilter>? reasonFilters) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IReadOnlyList? reasonFilters <= Make this mandatory There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made the reasonFilters parameter mandatory in the RegisterOnPersisting overload that accepts filters. Updated the legacy overloads to pass Array.Empty<IPersistenceReasonFilter>() when no filters are provided. (a2c9f1c) |
||
{ | ||
ArgumentNullException.ThrowIfNull(callback); | ||
|
||
if (PersistingState) | ||
{ | ||
throw new InvalidOperationException("Registering a callback while persisting state is not allowed."); | ||
} | ||
|
||
var persistenceCallback = new PersistComponentStateRegistration(callback, renderMode, reasonFilters); | ||
|
||
_registeredCallbacks.Add(persistenceCallback); | ||
|
||
return new PersistingComponentStateSubscription(_registeredCallbacks, persistenceCallback); | ||
} | ||
|
||
/// <summary> | ||
/// Register a callback to persist the component state when the application is about to be paused. | ||
/// Registered callbacks can use this opportunity to persist their state so that it can be retrieved when the application resumes. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.Components; | ||
|
||
namespace Microsoft.AspNetCore.Components.Web; | ||
|
||
/// <summary> | ||
/// Filter that controls whether component state should be persisted during prerendering. | ||
/// </summary> | ||
public class PersistOnPrerenderingFilter(bool persist = true) : PersistReasonFilter<PersistOnPrerendering>(persist); | ||
|
||
/// <summary> | ||
/// Filter that controls whether component state should be persisted during enhanced navigation. | ||
/// </summary> | ||
public class PersistOnEnhancedNavigationFilter(bool persist = true) : PersistReasonFilter<PersistOnEnhancedNavigation>(persist); | ||
|
||
/// <summary> | ||
/// Filter that controls whether component state should be persisted when a circuit is paused. | ||
/// </summary> | ||
public class PersistOnCircuitPauseFilter(bool persist = true) : PersistReasonFilter<PersistOnCircuitPause>(persist); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated PersistComponentStateRegistration.ReasonFilters property initialization to use the suggested pattern:
reasonFilters ?? Array.Empty<IPersistenceReasonFilter>()
. The property is now non-nullable and always returns a valid collection. (a2c9f1c)