Skip to content

Commit a9f4f9c

Browse files
committed
Feedback
1 parent fc01201 commit a9f4f9c

11 files changed

+35
-32
lines changed

src/Components/Components/src/IPersistentComponentStateStore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using System.Threading.Tasks;
44

5-
namespace Microsoft.AspNetCore.Components.Lifetime
5+
namespace Microsoft.AspNetCore.Components
66
{
77
/// <summary>
88
/// Manages the storage for components and services that are part of a Blazor application.

src/Components/Components/src/Infrastructure/ComponentStatePersistenceManager.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
using System;
55
using System.Buffers;
66
using System.Collections.Generic;
7-
using System.IO.Pipelines;
8-
using System.Text.Json;
9-
using System.Text.Json.Serialization;
107
using System.Threading.Tasks;
11-
using Microsoft.AspNetCore.Components.Lifetime;
128
using Microsoft.AspNetCore.Components.RenderTree;
139
using Microsoft.Extensions.Logging;
1410

@@ -17,11 +13,11 @@ namespace Microsoft.AspNetCore.Components.Infrastructure
1713
/// <summary>
1814
/// Manages the persistent state of components in an application.
1915
/// </summary>
20-
public class ComponentStatePersistenceManager
16+
public class ComponentStatePersistenceManager : IDisposable
2117
{
2218
private bool _stateIsPersisted;
23-
private List<Func<Task>> _pauseCallbacks = new();
24-
private readonly Dictionary<string, PooledByteBufferWriter> _currentState = new();
19+
private readonly List<Func<Task>> _pauseCallbacks = new();
20+
private readonly Dictionary<string, PooledByteBufferWriter> _currentState = new(StringComparer.Ordinal);
2521
private readonly ILogger<ComponentStatePersistenceManager> _logger;
2622

2723
/// <summary>
@@ -70,17 +66,19 @@ async Task PauseAndPersistState()
7066
{
7167
await PauseAsync();
7268

73-
var data = new Dictionary<string, ReadOnlySequence<byte>>();
69+
var data = new Dictionary<string, ReadOnlySequence<byte>>(StringComparer.Ordinal);
7470
foreach (var (key, value) in _currentState)
7571
{
7672
data[key] = new ReadOnlySequence<byte>(value.WrittenMemory);
7773
}
7874

7975
await store.PersistStateAsync(data);
80-
foreach (var (key, value) in _currentState)
76+
77+
foreach (var value in _currentState.Values)
8178
{
8279
value.Dispose();
8380
}
81+
_currentState.Clear();
8482
}
8583
}
8684

@@ -142,5 +140,14 @@ static async Task Awaited(Task task, ILogger<ComponentStatePersistenceManager> l
142140
}
143141
}
144142
}
143+
144+
void IDisposable.Dispose()
145+
{
146+
foreach (var value in _currentState.Values)
147+
{
148+
value.Dispose();
149+
}
150+
_currentState.Clear();
151+
}
145152
}
146153
}

src/Components/Components/src/PersistentComponentState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public PersistingComponentStateSubscription RegisterOnPersisting(Func<Task> call
6565
/// <param name="key">The key used to persist the state.</param>
6666
/// <param name="value">The persisted state.</param>
6767
/// <returns><c>true</c> if the state was found; <c>false</c> otherwise.</returns>
68-
public bool TryTake(string key, [MaybeNullWhen(false)] out ReadOnlySequence<byte> value)
68+
public bool TryTake(string key, out ReadOnlySequence<byte> value)
6969
{
7070
if (key is null)
7171
{

src/Components/Components/src/PersistingComponentStateSubscription.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
namespace Microsoft.AspNetCore.Components
1010
{
1111
/// <summary>
12-
/// Represents a subscription to the OnPersisting callback that <see cref="ComponentStatePersistenceManager"/> will trigger
12+
/// Represents a subscription to the <c>OnPersisting</c> callback that <see cref="ComponentStatePersistenceManager"/> callback will trigger
1313
/// when the application is being persisted.
1414
/// </summary>
15-
public struct PersistingComponentStateSubscription : IDisposable
15+
public readonly struct PersistingComponentStateSubscription : IDisposable
1616
{
17-
private readonly List<Func<Task>> _callbacks;
18-
private Func<Task> _callback;
17+
private readonly List<Func<Task>>? _callbacks;
18+
private readonly Func<Task>? _callback;
1919

2020
internal PersistingComponentStateSubscription(List<Func<Task>> callbacks, Func<Task> callback)
2121
{
@@ -26,7 +26,10 @@ internal PersistingComponentStateSubscription(List<Func<Task>> callbacks, Func<T
2626
/// <inheritdoc />
2727
public void Dispose()
2828
{
29-
_callbacks?.Remove(_callback);
29+
if (_callback != null)
30+
{
31+
_callbacks?.Remove(_callback);
32+
}
3033
}
3134
}
3235
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ Microsoft.AspNetCore.Components.ErrorBoundaryBase.ErrorContent.set -> void
1212
Microsoft.AspNetCore.Components.ErrorBoundaryBase.MaximumErrorCount.get -> int
1313
Microsoft.AspNetCore.Components.ErrorBoundaryBase.MaximumErrorCount.set -> void
1414
Microsoft.AspNetCore.Components.ErrorBoundaryBase.Recover() -> void
15+
Microsoft.AspNetCore.Components.IPersistentComponentStateStore
16+
Microsoft.AspNetCore.Components.IPersistentComponentStateStore.GetPersistedStateAsync() -> System.Threading.Tasks.Task<System.Collections.Generic.IDictionary<string!, System.Buffers.ReadOnlySequence<byte>>!>!
17+
Microsoft.AspNetCore.Components.IPersistentComponentStateStore.PersistStateAsync(System.Collections.Generic.IReadOnlyDictionary<string!, System.Buffers.ReadOnlySequence<byte>>! state) -> System.Threading.Tasks.Task!
1518
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager
1619
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.ComponentStatePersistenceManager(Microsoft.Extensions.Logging.ILogger<Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager!>! logger) -> void
17-
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.PersistStateAsync(Microsoft.AspNetCore.Components.Lifetime.IPersistentComponentStateStore! store, Microsoft.AspNetCore.Components.RenderTree.Renderer! renderer) -> System.Threading.Tasks.Task!
18-
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.RestoreStateAsync(Microsoft.AspNetCore.Components.Lifetime.IPersistentComponentStateStore! store) -> System.Threading.Tasks.Task!
20+
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.PersistStateAsync(Microsoft.AspNetCore.Components.IPersistentComponentStateStore! store, Microsoft.AspNetCore.Components.RenderTree.Renderer! renderer) -> System.Threading.Tasks.Task!
21+
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.RestoreStateAsync(Microsoft.AspNetCore.Components.IPersistentComponentStateStore! store) -> System.Threading.Tasks.Task!
1922
Microsoft.AspNetCore.Components.Infrastructure.ComponentStatePersistenceManager.State.get -> Microsoft.AspNetCore.Components.PersistentComponentState!
20-
Microsoft.AspNetCore.Components.Lifetime.IPersistentComponentStateStore
21-
Microsoft.AspNetCore.Components.Lifetime.IPersistentComponentStateStore.GetPersistedStateAsync() -> System.Threading.Tasks.Task<System.Collections.Generic.IDictionary<string!, System.Buffers.ReadOnlySequence<byte>>!>!
22-
Microsoft.AspNetCore.Components.Lifetime.IPersistentComponentStateStore.PersistStateAsync(System.Collections.Generic.IReadOnlyDictionary<string!, System.Buffers.ReadOnlySequence<byte>>! state) -> System.Threading.Tasks.Task!
2323
Microsoft.AspNetCore.Components.PersistentComponentState
2424
Microsoft.AspNetCore.Components.PersistentComponentState.Persist(string! key, System.Action<System.Buffers.IBufferWriter<byte>!>! valueWriter) -> void
2525
Microsoft.AspNetCore.Components.PersistentComponentState.PersistAsJson<TValue>(string! key, TValue instance) -> void

src/Components/Components/test/Lifetime/ComponentApplicationLifetimeTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Linq;
88
using System.Threading.Tasks;
99
using Microsoft.AspNetCore.Components.Infrastructure;
10-
using Microsoft.AspNetCore.Components.Lifetime;
1110
using Microsoft.AspNetCore.Components.RenderTree;
1211
using Microsoft.Extensions.DependencyInjection;
1312
using Microsoft.Extensions.Logging;

src/Components/Server/src/Circuits/CircuitFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Security.Claims;
88
using System.Threading.Tasks;
99
using Microsoft.AspNetCore.Components.Infrastructure;
10-
using Microsoft.AspNetCore.Components.Lifetime;
1110
using Microsoft.AspNetCore.Components.Routing;
1211
using Microsoft.AspNetCore.DataProtection;
1312
using Microsoft.Extensions.DependencyInjection;

src/Components/Server/src/Microsoft.AspNetCore.Components.Server.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
<Compile Include="$(RepoRoot)src\Shared\Components\PrerenderComponentApplicationStore.cs" />
8787
<Compile Include="$(RepoRoot)src\Shared\Components\ProtectedPrerenderComponentApplicationStore.cs" />
8888
<Compile Include="$(RepoRoot)src\Shared\Components\PooledByteBufferWritter.cs" />
89-
9089
</ItemGroup>
9190

9291
<PropertyGroup>

src/Mvc/Mvc.TagHelpers/test/PersistComponentStateTagHelperTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
using System.Collections.Generic;
66
using System.Text.Encodings.Web;
77
using System.Threading.Tasks;
8-
using Microsoft.AspNetCore.Components;
98
using Microsoft.AspNetCore.Components.Infrastructure;
10-
using Microsoft.AspNetCore.Components.Lifetime;
119
using Microsoft.AspNetCore.Components.Rendering;
1210
using Microsoft.AspNetCore.DataProtection;
1311
using Microsoft.AspNetCore.Html;

src/Shared/Components/PooledByteBufferWritter.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ private void CheckAndResizeBuffer(int sizeHint)
136136

137137
var previousBuffer = oldBuffer.AsSpan(0, _index);
138138
previousBuffer.CopyTo(_currentBuffer);
139-
previousBuffer.Clear();
140-
ArrayPool<byte>.Shared.Return(oldBuffer);
139+
ArrayPool<byte>.Shared.Return(oldBuffer, clearArray: true);
141140
}
142141
}
143142
}

src/Shared/Components/PrerenderComponentApplicationStore.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Text.Json;
99
using System.Threading.Tasks;
1010
using Microsoft.AspNetCore.Components.Infrastructure;
11-
using Microsoft.AspNetCore.Components.Lifetime;
1211

1312
namespace Microsoft.AspNetCore.Components
1413
{
@@ -89,11 +88,11 @@ protected virtual PooledByteBufferWriter SerializeState(IReadOnlyDictionary<stri
8988
static void WriteMultipleSegments(Utf8JsonWriter jsonWriter, string key, ReadOnlySequence<byte> value)
9089
{
9190
byte[] unescapedArray = null;
92-
var valueLenght = (int)value.Length;
91+
var valueLength = (int)value.Length;
9392

9493
Span<byte> valueSegment = value.Length <= 256 ?
95-
stackalloc byte[valueLenght] :
96-
(unescapedArray = ArrayPool<byte>.Shared.Rent(valueLenght)).AsSpan().Slice(0, valueLenght);
94+
stackalloc byte[valueLength] :
95+
(unescapedArray = ArrayPool<byte>.Shared.Rent(valueLength)).AsSpan().Slice(0, valueLength);
9796

9897
value.CopyTo(valueSegment);
9998
jsonWriter.WriteBase64String(key, valueSegment);

0 commit comments

Comments
 (0)